簡體   English   中英

即使隱藏在移動菜單中,下拉列表也會出現(懸停時)

[英]Dropdown list appears (on hover) even when it's hidden inside the mobile menu

在我的導航欄中,我有一個 li 項目,當有人將鼠標懸停在它上面時會打開一個下拉菜單。 在小屏幕中,我有一個隱藏它的漢堡菜單。

問題在於,在移動視圖中,即使 li 項目在菜單中“隱藏”時,當有人將鼠標懸停在其在屏幕上的位置上時,它也會出現(即使它隱藏在菜單中)。

我認為這可能是因為我設置了“可見性:隱藏;” 但我不知道該怎么做。

我的意思是當有人將它懸停在桌面視圖上時我想下拉,但在移動設備上我只想在有人打開菜單然后將鼠標懸停在它上面(或者單擊它)時才下拉。

為了節省空間,我只在下面包含了我認為更相關的代碼。

 $(function() { $(".toggle").on("click", function() { if ($(".item").hasClass("active")) { $(".item").removeClass("active"); } else { $(".item").addClass("active"); } }); });
 nav { background: #3e3e40; padding: 0 20px; } ul { list-style-type: none; } a { color: white; text-decoration: none; } a:hover{ color: rgb(189, 169, 136); } nav a:focus{ outline: 0; } .logo a:hover { text-decoration: none; } .menu li { font-size: 16px; padding: 15px 5px; white-space: nowrap; } .logo img{ width: 50px; height: 40px; } .logo{ padding: 0; flex: 1; } .toggle a { font-size: 20px; } nav, ul, li{ margin: 0; padding-top: 0; padding-bottom: 0; } /* Mobile menu */ .menu { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; } .toggle { order: 1; } .cart{ order:1 } .item { width: 100%; text-align: center; order: 3; display: none; } .login-info{ order:2 } .item.active { display: block; } /* Navbar Toggle */ .toggle { cursor:pointer; } .bars { background: #999; display: inline-block; height: 2px; position: relative; transition: background .2s ease-out; width: 18px; } .bars:before, .bars:after { background: #999; content: ''; display: block; height: 100%; position: absolute; transition: all .2s ease-out; width: 100%; } .bars:before { top: 5px; } .bars:after { top: -5px; } /*---------------dropdown li css-------------------*/ .dropdwn { position: relative; display: inline-block; color: white; } .dropdwn-content { visibility: hidden; opacity: 0; position: absolute; background-color: #ffffff; padding: 12px 16px; right: 39.5%; z-index: 1; height: 0; } .dropdwn-content a{ color: #3e3e40; } .dropdwn-content li{ color: #3e3e40; } .dropdwn-content a:focus{ outline:0; } .dropdwn:hover .dropdwn-content { display: block; color: white; visibility: visible; opacity: 1; height: auto; transition: all 250ms ease-in; } /*------font awesome and pseudo classes navbar-----*/ .fa-lg{ color: rgb(255, 255, 255); font-size: 26px; position: relative; } .fa-lg::before{ content: ''; border: rgb(255, 255, 255); border-style: solid; border-width: 2px; border-radius: 50%; position: absolute; width: 43px; height: 43px; right:14px; top:-8px } .badge:after{ content:attr(value); font-size:18px; color: #fff; background: rgb(92, 148, 166); border-radius:50%; padding: 0 5px; position:relative; left:-2px; top:-15px; opacity:0.9; } .pseudo::before{ content:attr(username); display: block; font-size: 12px; width: auto; height: 6px; margin-bottom: 12px; } .circle{ border: #fff; border-style: solid; border-radius: 50%; width: 40px; height: 40px; } /* Tablet menu */ @media all and (min-width: 468px) { .menu { justify-content: center; } } /* Desktop menu */ @media all and (min-width: 768px) { .item { display: block; width: auto; } .toggle { display: none; } .item { order: 2; } .menu li { padding: 15px 10px; } .dropdwn { position: relative; display: inline-block; } .dropdwn-content { position: absolute; right: auto; padding: 12px 16px; z-index: 1; } .dropdwn:hover .dropdwn-content { display: block; color: white; } .signup{ padding-right: 2rem; } .pseudo::before{ width: 10px; margin-bottom: 10px; } .pseudo::after{ content:''; display: block; font-size: 13px; width: auto; height: 7px; margin-bottom: 10px; } }
 <!DOCTYPE HTML> <html lang="en-gb"> <head> <meta charset="utf-8"> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="max-age=604800" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title></title> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> </head> <body> <nav> <ul class="menu"> <li class="logo"><a href="{%url 'home'%}"><img src="" alt="Company's logo"><a/></li> </li> <li class="item"> <a href="">Home</a> </li> <li class="item"> <a href="">Store</a> </li> <li class="dropdwn item"> <span class="item">Categories</span> <ul class="dropdwn-content"> <li><a href="{{category.get_url}}">Product1</a></li> <li><a href="{{category.get_url}}">Product2</a></li> </ul> </li> <li class="item"> <a href="" class="pseudo" username="Welcome Guest">Login</a> </li> <li class="item"> <a href=""class="signup">Signup </a> </li > <li class="item"> <form action="" method='GET' > <input type="text" placeholder="Search.." name="keyword"> <button type="submit" > </button> </form> </li> <li class="toggle"><span class="bars"></span></li> </ul> </nav> </body>

我使用媒體查詢解決了它

調整您想要的任何像素大小

代碼筆鏈接:

點擊這里


<!DOCTYPE HTML>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="max-age=604800" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title></title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<nav>
  <ul class="menu">
    <li class="logo"><a href="{%url 'home'%}"><img src="" alt="Company's logo"><a/></li>
    
    
    </li>
    <li class="item">
      <a href="">Home</a>
    </li>
    <li class="item">
      <a href="">Store</a>
    </li>
<!--     dropdwn -->
    <li class=" item dropdwn">
      <span class="item">Categories</span>
        
        <ul class="dropdwn-content disapear">
         
          <li class="disapear"><a href="{{category.get_url}}">Product1</a></li>
          <li class="disapear"><a href="{{category.get_url}}">Product2</a></li>
        </ul>
      </li>
   
    <li class="item">
      <a href="" class="pseudo" username="Welcome Guest">Login</a>
    </li>
    <li class="item">
      <a href=""class="signup">Signup </a>
    </li >

    <li class="item">
      <form action=""  method='GET' >
        
        <input type="text" placeholder="Search.." name="keyword">
        <button type="submit" >
          
        </button>
      </form>
    </li>
    <li class="toggle"><span class="bars"></span></li>
  </ul>
</nav>
</body>
nav {
  background: #3e3e40;
  padding: 0 20px;
 
}
@media all and (max-width:800px){
  .disapear{
    display: none;
  }
}

ul {
  list-style-type: none;
}
a {
  color: white;
  text-decoration: none;
}
a:hover{
  color: rgb(189, 169, 136);
}
nav a:focus{
outline: 0;
}
.logo a:hover {
  text-decoration: none;

}
.menu li {
  font-size: 16px;
  padding: 15px 5px;
  white-space: nowrap;
}
.logo img{
  width: 50px;
  height: 40px;

}

.logo{
  padding: 0;
  flex: 1;
}
.toggle a {
  font-size: 20px;
}
nav, ul, li{
  margin: 0;
  padding-top: 0;
  padding-bottom: 0;
}

/* Mobile menu */
.menu {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
}
.toggle {
order: 1;
}
.cart{
  order:1
}
.item {
  width: 100%;
  text-align: center;
  order: 3;
  display: none;
}
.login-info{
  order:2
}
.item.active {
  display: block;
  
}

/* Navbar Toggle */
.toggle {
  cursor:pointer;
}
.bars {
  background: #999;
  display: inline-block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}
.bars:before,
.bars:after {
  background: #999;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}
.bars:before {
  top: 5px;
}
.bars:after {
  top: -5px;
}


/*---------------dropdown li css-------------------*/
.dropdwn {
  position: relative;
  display: inline-block;
  color: white;
}

.dropdwn-content {
  opacity: 0;
  position: absolute;
  background-color: #ffffff;
  
  padding: 12px 16px;
  right: 39.5%;
  z-index: 1;
 height: 0;
}
.dropdwn-content a{
  color: #3e3e40;
}
.dropdwn-content li{
  color: #3e3e40;
}
.dropdwn-content a:focus{
 outline:0;
}


  
  @media all and (min-width: 800px){
    .dropdwn:hover .dropdwn-content {
  display: block;
  color: white;
  opacity: 1;
  height: auto;
  transition: all 250ms ease-in;
  }
}
/*------font awesome and pseudo classes navbar-----*/
.fa-lg{
  color: rgb(255, 255, 255);
  font-size: 26px;
  position: relative;
}
.fa-lg::before{
  content: '';
  border: rgb(255, 255, 255);
  border-style: solid;
  border-width: 2px;
  border-radius: 50%;
  position: absolute;
  width: 43px;
  height: 43px;
  right:14px;
  top:-8px
 
}
.badge:after{
  content:attr(value);
  font-size:18px;
  color: #fff;
  background: rgb(92, 148, 166);
  border-radius:50%;
  padding: 0 5px;
  position:relative;
  left:-2px;
  top:-15px;
  opacity:0.9;
}
.pseudo::before{
  content:attr(username);
  display: block;
  font-size: 12px;
  width: auto;
  height: 6px;
  margin-bottom: 12px;
  }


.circle{
  border: #fff;
  border-style: solid;
  border-radius: 50%;
  width: 40px;
  height: 40px;
 
}

/* Tablet menu */
@media all and (min-width: 468px) {
  .menu {
      justify-content: center;
  }
}

/* Desktop menu */
@media all and (min-width: 768px) {
  .item {
      display: block;
      width: auto;
  }
  .toggle {
      display: none;
  }

  .item {
      order: 2;
  }

  .menu li {
      padding: 15px 10px;
  }

 .dropdwn {
  position: relative;
  display: inline-block;
}

.dropdwn-content {
  position: absolute;
  right: auto;
  padding: 12px 16px;
  z-index: 1;

}

.dropdwn:hover .dropdwn-content {
  display: block;
  color: white;

}
.signup{
  padding-right: 2rem;
}


.pseudo::before{
  width: 10px;
  margin-bottom: 10px;
}
.pseudo::after{
  content:'';
  display: block;
  font-size: 13px;
  width: auto;
  height: 7px;
  margin-bottom: 10px;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM