元素躲在别人后面

时间:2018-03-15 03:26:02

标签: javascript html side-menu

我正在尝试创建一个html页面,其中包含主页面的地图和菜单的sidenav。问题是我无法将sidenav打开按钮显示在地图的正面或上方。看起来它总是在它后面,或者如果我搞乱我可以使地图消失,然后sidenav打开按钮可见并且工作正常。

  <!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8' />
  <title>Test</title>
  <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
  <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
  <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />

    <style>/*sidenav*/
      html, body {
          font-family: "Lato", sans-serif;
          margin:0;
          padding:0;
      }

      #map { position:absolute; top:0; bottom:0; width:100%; }

      .sidenav {
          height: 100%;
          width: 0;
          position: fixed;
          z-index: 1;
          top: 0;
          left: 0;
          background-color: #111;
          overflow-x: hidden;
          transition: 0.5s;
          padding-top: 30px;
      }

      .sidenav a, .dropdown-btn {
        padding: 6px 8px 6px 16px;
         text-decoration: none;
         font-size: 20px;
         color: #818181;
         display: block;
         border: none;
         background: none;
         text-align: left;
         cursor: pointer;
         transition: 0.3s;
      }

      .sidenav a:hover, .dropdown-btn:hover {
          color: #f1f1f1;
      }

      #myInput {
        font-size: 18px;
        padding: 6px 8px 6px 16px;
        border: none;
        border-bottom: 1px solid #ddd;
      }

      .main {
          margin-left: 10px;
          font-size: 20px;
          padding: 0px 10px;
      }

      .active {
          background-color: #111;
          color: white;
      }

      .dropdown-container {
          display: none;
          background-color: #262626;
          padding-left: 8px;
      }

      .fa-caret-down {
          float: right;
          padding-right: 8px;
      }

      .sidenav .closebtn {
          position: absolute;
          top: 0;
          right: 25px;
          font-size: 30px;
          margin-left: 50px;
      }

      @media screen and (max-height: 450px) {
        .sidenav {padding-top: 15px;}
        .sidenav a {font-size: 18px;}
      }
      </style>

</head>
<body>

  <div id='map'></div>
  <script>//this is the map stuff//
    mapboxgl.accessToken = 'pk.eyJ1IjoidHJveWIiLCJhIjoiY2pkb3F0cmJnMHA0YTJ4cXB3Mjc0Y3g0eCJ9.3Myq7AzC4yxR8hi9Ubchdw';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v9',
        center: [-88.75, 42.15],
        zoom: 8
    });


  </script>


<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">Home</a>
  <button class="dropdown-btn">Select Congregation
    <i class="fa fa-caret-down"></i>
  </button>
  <div id="myDropdown"  class="dropdown-container">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#Mt. Calm">Mt. Calm</a>
    <a href="#Newark">Newark</a>
    <a href="#Wildwood">Wildwood</a>
  </div>
  <a href="#">Contact</a>
  <a href="#">About</a>
</div>

<div class="main">

<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>

<script>
    var dropdown = document.getElementsByClassName("dropdown-btn");
    var i;

    for (i = 0; i < dropdown.length; i++) {
      dropdown[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var dropdownContent = this.nextElementSibling;
        if (dropdownContent.style.display === "block") {
          dropdownContent.style.display = "none";
        } else {
          dropdownContent.style.display = "block";
        }
      });
  }
  function filterFunction() {
      var input, filter, ul, li, a, i;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      div = document.getElementById("myDropdown");
      a = div.getElementsByTagName("a");
      for (i = 0; i < a.length; i++) {
          if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
              a[i].style.display = "";
          } else {
              a[i].style.display = "none";
          }
      }
  }
</script>
<script>
  function openNav() {
      document.getElementById("mySidenav").style.width = "260px";
  }
  function closeNav() {
      document.getElementById("mySidenav").style.width = "0";
  }
</script>
</body>

2 个答案:

答案 0 :(得分:0)

在第22行中,将宽度值更改为200px。你的sidenav看起来很好。检查您的代码。

答案 1 :(得分:0)

不要在导航菜单中设置z-index,而是尝试为地图本身提供z-index z-index: -1;

顺便说一句,而不是给谷歌司机,更好地直接在这里写行代码: https://meta.stackexchange.com/questions/51144/how-do-i-post-code-in-stackoverflow