jquery - Height to get this sub navigation to slide up on first click -


i have list items have sub navigation:

  <ul role="menu">     <li role="menuitem">       <a href="#" class="clearfix">top level</a>     </li>     <li class="slidedown open" role="menuitem">       <a href="#" class="clearfix">top level</a>       <span class="slidedown-toggle">show sub menu</span>       <!-- subnav -->       <ul class="list-reset nav-sub" role="menu">         <li role="menuitem">           <a href="#">second level</a>         </li>         <li role="menuitem">           <a href="#">second level</a>         </li>         <li role="menuitem">           <a href="#">second level</a>         </li>       </ul>     </li>   </ul> 

i have script, calculates height of subnav <ul> when .slidedown-toggle clicked , changes 0 depending on parent <li> having class 'open' toggled.

$(document).ready( function(){     var $slidedowntoggle = $('.nav-sidebar .slidedown-toggle');    $slidedowntoggle.click(function () {      var $slidedown = $(this).parent('.slidedown'); // container item     var $subnav = $(this).siblings('.nav-sub'); // ul needs slide up/down      var totalheight = 0; // calculate height required (in px) show <li>'s     $subnav.find('li').each(function() {       totalheight += $(this).outerheight(true);     });      // toggle 'open' class on parent <li>     $slidedown.toggleclass('open');      // set appropriate height     if ($slidedown.hasclass('open')) {       $subnav.css({height: totalheight + 'px'});     } else {       $subnav.css({height: '0px'});     }    });  }); 

the problem have class 'open' has been added <li class="slidedown"> because when on 'subnav' page want menu expended show are. because set height: auto in css .slidedown.open, height isn't animated (i'm aware of whole slideup using max-height , don't want that) when first click slidedown-toggle of 'open' items.

easiest way see i'm on @ jsbin. click second 'show menu' , it's slides , down nicely. click first 1 , first click doesn't slide (because of height auto). how can fix that? maybe set height on click first , set 0?

http://jsbin.com/poxiy/2/edit

it nasty hack, works... on ready handler assign height opened subnav

$('.nav-sidebar .slidedown.open .nav-sub').height(function(){     return $(this).height(); }) 

demo: fiddle

another solution discussed in below posts


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -