代码之家  ›  专栏  ›  技术社区  ›  Camilla8

按钮位置像游戏控制器css,反应灵敏

  •  0
  • Camilla8  · 技术社区  · 6 年前

    我想把这4个按钮的方式类似于游戏控制器。 我得到的结果如下。它们应该在屏幕的中间和底部,更小,而且必须是响应的。我要改变什么?我应该用吗 #controls { position:relative;} ?

    Controls

    HTML代码:

    <head><meta name="viewport" content="width=device-width, initial-scale=1.0"/>...</head>
    
    <body><div id="controls">
        <button id="keyboard_key_up" class="movements_control"><img src="public/img/keyboard_key_up.png"/></button>
        <button id="keyboard_key_left" class="movements_control"><img src="public/img/keyboard_key_left.png" /></button>
        <button id="keyboard_key_down" class="movements_control"><img src="public/img/keyboard_key_down.png" /></button>
        <button id="keyboard_key_right" class="movements_control"><img src="public/img/keyboard_key_right.png" /></button>
    
    </div>...</body>
    

    CSS代码:

    #keyboard_key_up {
        position: absolute;
        right:300px;
        bottom: 500px;
        border-style: solid;
        border-color: black;
        background: white;z
    
    }
    
    #keyboard_key_left {
        position: absolute;
        right:575px;
        bottom: 250px;
        border-style: solid;
        border-color: black;
    
    }
    
    #keyboard_key_down {
        position: absolute;
        right:300px;
        bottom: 250px;
        border-style: solid;
        border-color: black;
    
    }
    
    #keyboard_key_right {
        position: absolute;
        right:25px;
        bottom: 250px;
        border-style: solid;
        border-color: black;
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   msg    6 年前

    现在对flex和grid的支持应该相当不错,但是根据您的目标,这可能不起作用。

    body {
      margin: 0;
      padding: 0;
    }
    
    #wrapper {
      display: flex;
      height: 100vh;
      justify-content: center;
    }
    
    #controls {
      display: inline-grid;
      grid-column-gap: 0;
      align-self: end;
    }
    
    .movements_control {
      border: solid 1px black;  
      margin: 0;
      padding: 0;
    }
    
    .movements_control img {
      width: inherit;
      height: inherit;
    }
    
    #keyboard_key_up {
      grid-column-start: 2;
    }
    
    #keyboard_key_left {
      grid-column-start: 1;
    }
    
    #keyboard_key_down {}
    
    #keyboard_key_right {
      grid-column-start: 3;
    }
    
    @media (min-width: 721px) {
      .movements_control {
        width: 40px;
        height: 40px;
      }
    }
    
    @media (max-width: 720px) {
      .movements_control {
        width: 20px;
        height: 20px;
      }
    }
    <div id="wrapper">
      <div id="controls">
        <button id="keyboard_key_up" class="movements_control"><img src="public/img/keyboard_key_up.png"></button>
        <button id="keyboard_key_left" class="movements_control"><img src="public/img/keyboard_key_left.png"></button>
        <button id="keyboard_key_down" class="movements_control"><img src="public/img/keyboard_key_down.png"></button>
        <button id="keyboard_key_right" class="movements_control"><img src="public/img/keyboard_key_right.png"></button>
      </div>
    </div>
        2
  •  0
  •   Matheus Barem    6 年前

    试试这个CSS:

    #controls {
    position: relative;
    bottom: 10px;
    margin: 0 auto;
    }
    

    如果这不起作用,请您发送您的代码使用plunker或codepen?