3

I want to toggle a dropdown, animate down on click and then back up on another click. It's animating down but not back up. It's just disappearing.

Here's my codepen:

http://codepen.io/omarel/pen/YZPyOg

JQUERY

$(".openNav").click(function() {
    $('.navdropdown').toggleClass("slidenavdown");
}); 

HTML

<a href="javascript:;" class="openNav">open</a>

<div class="navdropdown">
          <div class="holdcenter">
            <ul>
              <li><a href="#buildingcontainer">hello</a></li>

            </ul>
          </div>
        </div>

CSS

.openNav {
  position:fixed;
  top:0px;
  left:0px;
  z-index:1000
}

.navdropdown  {
        position: fixed;
        top:-100%;
        width:100%;
        height:100vh;
        background-color: #000;
        z-index:5;
    }
    .navdropdown .holdcenter {
        position: relative;
        display: flex;
    justify-content: center;
    align-items: center;
        height: 100%; 
        overflow-y: auto;
    }
    .navdropdown ul > li {
        display: block;
        position: relative;
        text-align: center;
        font-size:38px;
        letter-spacing: 9.5px;
        margin:27px auto;
    }
    .navdropdown.on  {
        height:100vh;
    }

        .slidenavdown {
          -webkit-animation: slidenavdown; 
          -moz-animation: slidenavdown; 
          -o-animation: slidenavdown; 
          animation:slidenavdown;
        }
        .slidenavdown {
            animation-iteration-count: 1;
          -webkit-animation-iteration-count: 1;
          animation-fill-mode: both;
          -webkit-animation-fill-mode: both;
        }
        .x  {
          -webkit-animation-direction: alternate-reverse;
        animation-direction: alternate-reverse; 
        }


    /* --- DELAYS --- */

        .x {
          -webkit-animation-delay: .2s; 
        animation-delay:  .2s;
        }

    /* --- SPEED --- */

        .slidenavdown {
                -webkit-animation-duration: 0.8s; 
            animation-duration: 0.8s;
            }


    /* --- EASING --- */

        .slidenavdown  { 
        -webkit-animation-timing-function: cubic-bezier(.86,.03,.53,1.01);
        animation-timing-function: cubic-bezier(.86,.03,.53,1.01);
        }

    /* --- TRANSITIONS --- */

        .x {
            -o-transition: .5s;
        -ms-transition: .5s;
        -moz-transition: .5s;
        -webkit-transition: .5s;
        transition: .5s;
        }

    /* --- KEYFRAMES --- */

        @keyframes slidenavdown {
          0% {
            top:-100%;
          }
          100% {
            top:0px;
          }
        }
2
  • If you're already using jQuery, then why not let it do the animation instead of trying to do it within the CSS? You wouldn't have to maintain all that code. Commented Feb 24, 2017 at 18:06
  • I'd like to work with these CSS animations instead. Commented Feb 24, 2017 at 18:07

2 Answers 2

2

Changed your code a bit, See if it helps:

$(".openNav").click(function() {
  $('.navdropdown').toggleClass("slidenavdown");

});
.openNav {
  position: fixed;
  top: 0px;
  left: 0px;
  z-index: 1000
}

.navdropdown {
  position: fixed;
  top: -100%;
  width: 100%;
  height: 100vh;
  background-color: #000;
  z-index: 5;
  transition: top 2s;
}

.navdropdown .holdcenter {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  overflow-y: auto;
}

.navdropdown ul>li {
  display: block;
  position: relative;
  text-align: center;
  font-size: 38px;
  letter-spacing: 9.5px;
  margin: 27px auto;
}

.navdropdown.on {
  height: 100vh;
}

.slidenavdown {
  top: 0px;
}


/*
		.slidenavdown {
		  -webkit-animation: slidenavdown; 
		  -moz-animation: slidenavdown; 
		  -o-animation: slidenavdown; 
		  animation:slidenavdown;
		}
		.slidenavdown {
			animation-iteration-count: 1;
		  -webkit-animation-iteration-count: 1;
		  animation-fill-mode: both;
		  -webkit-animation-fill-mode: both;
		}
		.x  {
		  -webkit-animation-direction: alternate-reverse;
  		animation-direction: alternate-reverse; 
		}
*/


/* --- DELAYS --- */

.x {
  -webkit-animation-delay: .2s;
  animation-delay: .2s;
}


/* --- SPEED --- */

.slidenavdown {
  -webkit-animation-duration: 0.8s;
  animation-duration: 0.8s;
}


/* --- EASING --- */

.slidenavdown {
  -webkit-animation-timing-function: cubic-bezier(.86, .03, .53, 1.01);
  animation-timing-function: cubic-bezier(.86, .03, .53, 1.01);
}


/* --- TRANSITIONS --- */

.x {
  -o-transition: .5s;
  -ms-transition: .5s;
  -moz-transition: .5s;
  -webkit-transition: .5s;
  transition: .5s;
}


/* --- KEYFRAMES --- */

@keyframes slidenavdown {
  0% {
    top: -100%;
  }
  100% {
    top: 0px;
  }
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<a href="javascript:;" class="openNav">open</a>
<div class="navdropdown">
  <div class="holdcenter">
    <ul>
      <li><a href="#buildingcontainer">hello</a></li>
    </ul>
  </div>
</div>

Updated: If you want to use animation instead of transition:

$(".openNav").click(function() {
  $('.navdropdown').show();
  $('.navdropdown').toggleClass("slidenavdown");
});
.openNav {
  position: fixed;
  top: 0px;
  left: 0px;
  z-index: 1000
}

.navdropdown {
  position: fixed;
  width: 100%;
  height: 100vh;
  background-color: #000;
  z-index: 5;
  -webkit-animation: slidenavup;
          animation: slidenavup;

  
  animation-iteration-count: 1;
  -webkit-animation-iteration-count: 1;
  animation-fill-mode: both;
  -webkit-animation-fill-mode: both;
  /* --- SPEED --- */
  -webkit-animation-duration: 0.8s;
  animation-duration: 0.8s;
  /* --- EASING --- */
  -webkit-animation-timing-function: cubic-bezier(.86, .03, .53, 1.01);
  animation-timing-function: cubic-bezier(.86, .03, .53, 1.01);
  top: -100%;
}

.navdropdown .holdcenter {
  position: relative;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  height: 100%;
  overflow-y: auto;
}

.navdropdown ul > li {
  display: block;
  position: relative;
  text-align: center;
  font-size: 38px;
  letter-spacing: 9.5px;
  margin: 27px auto;
}

.navdropdown.on {
  height: 100vh;
}

.slidenavdown {
  -webkit-animation: slidenavdown;
  animation: slidenavdown;
}
.slidenavdown {
  top: 0px;
}

/* --- DELAYS --- */

.x {
  -webkit-animation-delay: .2s;
  animation-delay: .2s;
}
/* --- SPEED --- */

.slidenavdown {
  -webkit-animation-duration: 0.8s;
  animation-duration: 0.8s;
}

/* --- TRANSITIONS --- */

.x {
  -webkit-transition: .5s;
  transition: .5s;
}


/* --- KEYFRAMES --- */

@-webkit-keyframes slidenavdown {
  0% {
    top: -100%;
  }
  100% {
    top: 0px;
  }
}

@keyframes slidenavdown {
  0% {
    top: -100%;
  }
  100% {
    top: 0px;
  }
}

@-webkit-keyframes slidenavup {
  0% {
    top: 0px;
  }
  100% {
    top: -100%;
  }
}

@keyframes slidenavup {
  0% {
    top: 0px;
  }
  100% {
    top: -100%;
  }
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<a href="javascript:;" class="openNav">open</a>

<div class="navdropdown" style="display: none;">
  <div class="holdcenter">
    <ul>
      <li><a href="#buildingcontainer">hello</a></li>

    </ul>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

9 Comments

I'm getting an error when I run it here. Did you only add this CSS? Still getting the same problem. .slidenavdown { top:0px; }
@Omar Fixed it, forgot to add JQuery
Im sorry what's the change that was done. I can't tell
@Omar Instead of animation I used transition
I see. Is there no way to do it similar while using the animation instead.
|
0

Add a 'transition' property to the rule for the original class, like so:

.navdropdown  {
        position: fixed;
        top:-100%;
        width:100%;
        height:100vh;
        background-color: #000;
        z-index:5;
/* Transition: */
        transition: .8s;
    }

1 Comment

same problem. I've added that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.