javascript - Unable to retrieve Height from a toggleClass jQuery -


i want height of class toggled. when button clicked, class .category-menu-visible added. if class exists, want it's height. when alert menuheight, 0.

small scale jsfiddle example

actual code:

jquery

jquery('.topics-btn').click(function(){   jquery('.category-menu-wrap').toggleclass('category-menu-visible');    if (jquery('.category-menu-wrap').hasclass('category-menu-visible')){     var menuheight = jquery('.category-menu-visible').height();     alert(menuheight);     jquery('.sidebar .content-wrap').css('margin-top', menuheight);   } else {     jquery('.sidebar .content-wrap').css('margin-top', 0);   }  }); 

css:

.category-menu-wrap {   width:100%;   height:0px;   background-color:#f7d5b6;   overflow: hidden;   transition: height .5s cubic-bezier(.27,1.76,.95,1.19); } .category-menu-visible {   height: 70px;   transition: height .3s cubic-bezier(.27,1.76,.95,1.1); } 

why can't retrieve height?

you need wait till transition finishes. update: there useful event transitionend it:

jquery('.topics-btn').click(function(){    var $menu = jquery('.category-menu-wrap');    $menu.toggleclass('category-menu-visible');    $menu.on("transitionend", function(){      if (jquery('.category-menu-wrap').hasclass('category-menu-visible')){        var menuheight = jquery('.category-menu-visible').height();        alert(menuheight);        jquery('.sidebar .content-wrap').css('margin-top', menuheight);      } else {        jquery('.sidebar .content-wrap').css('margin-top', 0);      }    });    });
.category-menu-wrap {    height: 0;  }  .category-menu-visible {    height: 70px;    transition: height .3s cubic-bezier(.27,1.76,.95,1.1);  }
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>  <button class='topics-btn'>click</button>  <div class='category-menu-wrap'></div>


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -