2017-03-14 74 views
1

Example of finished DPAD如何使用僅CSS樣式的「D-PAD」控制器看起來像這樣?

body { 
 
    background-color: black; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow: hidden; 
 
} 
 
div#controller { 
 
    display: none; 
 
} 
 
div#instructions { 
 
    font-size: 20px; 
 
    color: white; 
 
    position: static; 
 
    text-align: center; 
 
} 
 

 
@media only screen and (max-width: 480px) { 
 
    div#instructions { 
 
     display: none; 
 
    } 
 
    div#controller { 
 
     display: flex; 
 
     position: relative; 
 
     background-color: grey; 
 
     opacity: 0.6; 
 
     height: 33%; 
 
     width: 80%; 
 
    } 
 
    div#controller.dpad { 
 
     width: 60%; 
 
    }  
 
} 
 

 
@media only screen and (max-width: 768px) { 
 
    div#instructions { 
 
     display: none; 
 
    } 
 
    div#controller { 
 
     display: flex; 
 
     position: relative; 
 
     top: 300px; 
 
     background-color: grey; 
 
     opacity: 0.6; 
 
     height: 50%; 
 
     width: 80%; 
 
    } 
 
    div#controller.dpad { 
 
     width: 33%; 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> 
 
     
 
     <!-- The CSS is missing! Your mission is to re-create it --> 
 
     <link rel="stylesheet" href="env3d.css" /> 
 
     
 
     <script src="http://css.operatoroverload.com/exercise/bundle.js"></script> 
 
     <script src="http://css.operatoroverload.com/exercise/game.js"></script>   
 
     
 
     <script type="text/javascript">   
 

 
     function playGame() { 
 
      console.log("playGame"); 
 
      var game = new Game(); 
 

 
      game.setup(); 
 
       
 
      var env; 
 
      if (game.env) { 
 
       env = game.env; 
 
      } else { 
 
       env = new env3d.Env(); 
 
      } 
 
      
 
      env.loop = game.loop.bind(game);    
 
      env.start(); 
 
     } 
 
      
 
     window.addEventListener('load', function() { 
 
      playGame(); 
 
     }); 
 
      
 
     document.addEventListener("contextmenu", function(e) { 
 
      e.preventDefault(); 
 
     });   
 
      
 
     </script> 
 
      
 
    </head> 
 
    <body> 
 
     <div id="controller"> 
 
      <div class="dpad"> 
 
       <button env3d-key="KEY_UP">UP</button> 
 
       <button env3d-key="KEY_LEFT">LEFT</button> 
 
       <button env3d-key="KEY_RIGHT">RIGHT</button> 
 
       <button env3d-key="KEY_DOWN">DOWN</button> 
 
      </div> 
 
      <button env3d-key="KEY_A">A</button> 
 
      <button env3d-key="KEY_Z">Z</button>    
 
     </div> 
 
     <div id="instructions">Use arrow keys to change camera angle, A to zoom in, Z to zoom out.</div> 
 
     <div id="env3d"></div>   
 
    </body> 
 
    
 
</html>

我有這個小的小項目來完成。但是,我完全停留在如何僅使用CSS對移動控制器進行編碼。我不能把它看起來像照片。

任何人都可以請我指出正確的方向去哪裏?

謝謝!對你的幫助表示感謝! :)

回答

1

老兄,我們在同一個網絡開發類。

哪部分需要幫助?

首先,您必須調整屏幕底部的#controller.dpad div。您需要使用positionwidthheight屬性。

然後,您必須將按鈕放在它們的容器div中(也可以使用position,widthheight)。您可以選擇單獨使用element>elementattribute=value每個按鈕,和/或:nth-child()選擇描述here.

在你需要它時,this頁解釋瞭如何垂直通過給它的屬性position: relative (or absolute);top: 50%;transform: translateY(-50%);中心的元素。 (水平居中將position: relative (or absolute);left: 50%;,並且transform: translateX(-50%);

希望這可以幫助您!

相關問題