Kent Godfrey
Kent Godfrey

Reputation: 73

Why does my <a> element appear before its parent div is toggled

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/

I have tried a few obvious solutions within the CSS/Jquery such as settings the visibility to hidden or display none and toggling it. All of which has failed.

If anyone has a better method of creating the hamburger dropdown I would appreciate it.

Thanks

Upvotes: 0

Views: 62

Answers (5)

roberrrt-s
roberrrt-s

Reputation: 8210

Because your container contains an overflowing element:

.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);
}

Technically, your solution works, but if we use Chrome's DevTools, we see that the elements are overflowing from the 0 height parent container. Adding overflow: hidden to the parent hides them.

Working snippet:

$(".burger").click(function() {
  $(".nav").toggleClass("show", 900);
  $(".burger").toggleClass("change");
});
#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);
}
<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>

Upvotes: 1

Taha Paksu
Taha Paksu

Reputation: 15616

Do you want something like this?

Differences:

  1. Animated max-height instead of height.
  2. Added overflow: hidden to the .nav element.
  3. Added CSS transition duration and easing.

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

Upvotes: 1

CodeSmith
CodeSmith

Reputation: 3207

.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);
}

Maybe cause you display them? Toggle between display none and block?

Upvotes: 0

Renzo Calla
Renzo Calla

Reputation: 7706

just add

height:0px;
overflow:hidden;

to your .nav class fiddle

Upvotes: 1

Akshay Prabhakar
Akshay Prabhakar

Reputation: 479

Check this if it help at your end, this is different from your setup. I hope it help you at your end. Here is the complete code :

<!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> 

I hope it works for you. Thanks!

Upvotes: 1

Related Questions