为什么我的<a> element appear before its parent div is toggled

时间:2017-10-05 13:15:19

标签: javascript jquery html css

Basically the elements within my nav dropdown menu are displaying before its parent div has been toggled on using JQuery.toggleClass().

This is a simple problem but for whatever reason Im just hitting my head against the wall.

HTML:

<div id="navCont">
     <div class="burger">
     <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
</div>
<div class="nav">
  <a href="#home">home</a>
  <a href="myWork.html">work</a>
  <a href="#contact">cont</a>
</div>

See JS fiddle: https://jsfiddle.net/ywkxyjx3/1/

我在CSS / Jquery中尝试了一些明显的解决方案,例如将可见性设置为隐藏或显示无和切换它。所有这些都失败了。

如果有人有更好的方法来制作汉堡包下拉菜单,我会很感激。

由于

5 个答案:

答案 0 :(得分:1)

因为您的容器包含溢出元素:

.nav {
  position: absolute;
  display: block;
  height: 0px;
  overflow: hidden; // Change this.
  float: right;
  top: 55px;
  opacity: 1;
  text-align: center;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

从技术上讲,您的解决方案有效,但如果我们使用Chrome的DevTools,我们会看到元素从0高度的父容器中溢出。将overflow: hidden添加到父级会隐藏它们。

工作片段:

&#13;
&#13;
$(".burger").click(function() {
  $(".nav").toggleClass("show", 900);
  $(".burger").toggleClass("change");
});
&#13;
#navCont {
  width: 47px;
  height: auto;
  float: right;
}


/*Dropdown Nav*/

.nav {
  position: absolute;
  display: block;
  overflow: hidden;
  height: 0px;
  float: right;
  top: 55px;
  opacity: 99;
  text-align: center;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.nav a {
  color: black;
  text-decoration: none;
}

.show {
  height: auto;
  max-height: 999px;
}

.burger {
  /* box around hamburger. Has onclick function*/
  display: inline-block;
  position: relative;
  float: right;
  padding: 5px;
  cursor: pointer;
}


/*Individual lines of burger*/

.bar1,
.bar2,
.bar3 {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background-color: #95449a;
  margin: 6px 0;
  /*seperate the lines*/
  transition: 1s;
  /*time taken for any animation to take place */
}


/*in html classlist.toggle(change)*/

.change .bar1 {
  -webkit-transform: translateY(10px) rotate(45deg);
  transform: translateY(10px) rotate(45deg);
}

.change .bar2 {
  opacity: 0;
  -webkit-transform: translateX(10px);
  transform: translateX(10px);
}

.change .bar3 {
  -webkit-transform: translateY(-10px) rotate(-45deg);
  transform: translateY(-10px) rotate(-45deg);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div id="navCont">
  <div class="burger">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
  </div>
  <div class="nav">
    <a href="#home">home</a>
    <a href="myWork.html">work</a>
    <a href="#contact">cont</a>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果它对您有帮助,请检查这一点,这与您的设置不同。 我希望它能帮助你。 这是完整的代码:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    font-family: "Lato", sans-serif;
}

.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: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover {
    color: #f1f1f1;
}

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

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

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<h2>Animated Sidenav Example</h2>
<p>Click on the element below to open the side navigation menu.</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>

<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}
</script>

</body>
</html> 

我希望它适合你。 谢谢!

答案 2 :(得分:1)

只需添加

height:0px;
overflow:hidden;

到你的.nav课程 fiddle

答案 3 :(得分:1)

你想要这样的东西吗?

差异:

  1. 动画最大高度而不是高度。
  2. 添加了溢出:隐藏到.nav元素。
  3. 添加了CSS转换持续时间和缓动。
  4. // first, get the max-height on page load
    $(document).ready(function(){
      $(".nav").css("max-height", $(".nav").height()).addClass("zero");
    });
    
    $(".burger").click(function() {
      // set max-height to 0 or remove the limitation
      $(".nav").toggleClass("zero");
      $(".burger").toggleClass("change");
    });
    #navCont {
      width: 47px;
      height: auto;
      float: right;
    }
    
    
    /*Dropdown Nav*/
    
    .nav {
      position: absolute;
      display: block;  
      float: right;
      top: 55px;
      opacity: 99;
      text-align: center;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      transition: max-height 0.9s linear;
      overflow: hidden;
    }
    
    .nav a {
      color: black;
      text-decoration: none;
    }
    
    .zero {
      max-height: 0 !important;  
    }
    
    .burger {
      /* box around hamburger. Has onclick function*/
      display: inline-block;
      position: relative;
      float: right;
      padding: 5px;
      cursor: pointer;
    }
    
    
    /*Individual lines of burger*/
    
    .bar1,
    .bar2,
    .bar3 {
      width: 36px;
      height: 4px;
      border-radius: 2px;
      background-color: #95449a;
      margin: 6px 0;
      /*seperate the lines*/
      transition: 1s;
      /*time taken for any animation to take place */
    }
    
    
    /*in html classlist.toggle(change)*/
    
    .change .bar1 {
      -webkit-transform: translateY(10px) rotate(45deg);
      transform: translateY(10px) rotate(45deg);
    }
    
    .change .bar2 {
      opacity: 0;
      -webkit-transform: translateX(10px);
      transform: translateX(10px);
    }
    
    .change .bar3 {
      -webkit-transform: translateY(-10px) rotate(-45deg);
      transform: translateY(-10px) rotate(-45deg);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="navCont">
      <div class="burger">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
      </div>
      <div class="nav">
        <a href="#home">home</a>
        <a href="myWork.html">work</a>
        <a href="#contact">cont</a>
      </div>
    </div>

答案 4 :(得分:0)

.nav {
  position: absolute;
  display: block;
  height: 0px;
  float: right;
  top: 55px;
  opacity: 99;
  text-align: center;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

也许会让你展示它们?在display none和block?之间切换?