【问题标题】:Mobile dropdown menu won't go away移动下拉菜单不会消失
【发布时间】:2016-03-30 11:49:56
【问题描述】:

我有这个下拉菜单,用于我的移动网站。问题是,在下拉菜单之外单击时它不会消失。因此,当单击按钮时,除了从菜单中选择其他站点之一之外,没有办法再次摆脱菜单。是否有可能,当它显示时,如果你在它之外点击它会消失吗?

这是我的代码:

/* Dropdown Button */
.dropbtn {
    background-color: #948a69;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
    display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
    background-color: #9e9370;
}
<div style="position:absolute; top:15px; left:15px;">
<div class="dropdown">
  <button class="dropbtn"><b>MENU</b></button>
  <div class="dropdown-content">
    <a href="index.html">Velkommen</a>
    <a href="bock.html">Produkter</a>
    <a href="fadol.html">Fadøl</a>
    <a href="historie.html">Historie</a>
    <a href="kontakt.html">Kontakt </a>
  </div>
</div>
</div>

【问题讨论】:

    标签: html css drop-down-menu


    【解决方案1】:

    我刚刚遇到了同样的问题,并从这里找到了解决方案:
    http://www.cssplay.co.uk/menus/cssplay-ipad-dropdown-menu-fixed.html

    修复的中心是使用透明图像来强制执行我们想要的浏览器行为。添加这个 CSS:

    // For the Safari mobile browser
    div img.close {
      display: none;
      width: 100%;
      height: 100%;
      position: fixed;
      left: 0;
      top: 0;
      z-index: -1;
    }
    
    div button:hover + img {
      display: block;
    }
    

    然后将其添加到您的 HTML 中,在您最后一个 &lt;/div&gt; 之前:

    <img class="close" src="ipad/trans.gif" />
    

    我个人将它与列表而不是按钮一起使用,因此可能需要进行一些调整。但我可以确认它可以使用列表(基于&lt;ul&gt;&lt;li&gt; 的菜单)。

    【讨论】:

      猜你喜欢
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 2011-02-01
      相关资源
      最近更新 更多