移动下拉菜单不会消失

时间:2016-03-30 11:49:57

标签: html css drop-down-menu

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

这是我的代码:

/* 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>

1 个答案:

答案 0 :(得分:0)

我刚刚遇到同样的问题并从此处找到了解决方案:
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中,在上一个</div>

之前添加此内容
<img class="close" src="ipad/trans.gif" />

我个人使用的是列表而不是按钮,因此可能需要进行一些调整。但我可以使用列表(基于<ul><li>的菜单)确认其有效。