Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help building a jQuery dropdown menu
    primarykey
    data
    text
    <p>I'm just starting out with jQuery. I want to make a simple 'ul' dropdown menu to improve my understanding. The basic flow is this:</p> <p>'ul.menu li ul' has display:none >> on hover of li, get &amp; store height of 'this' ul >> set height of 'this' ul to 0 >> set display to block >> animate height to original stored height</p> <p>I know there are already some great plugins for dropdowns, but I really want to roll my own to get a better understanding of jQuery.</p> <p>So far I have managed the following, very badly done (see live version here - <a href="http://jsbin.com/eduvi" rel="nofollow noreferrer">http://jsbin.com/eduvi</a>):</p> <hr> <pre><code>var $j = jQuery.noConflict(); $j(document).ready(function(){ // Get height of current hidden ul $j("ul.menu li").hover(function() { getHeight($j("ul", this).height()); }); // Set height to 0px $j('ul.menu li').hover(function() { $j("ul", this).css({"height":"0px"}); }); // Set display to block $j('ul.menu li').hover(function() { $j("ul", this).css({"display":"block"}); }); // Animate height to stored height $j('ul.menu li').hover(function getHeight(h) { $j('ul:first', this).stop().animate({ "height" : "100%" } , 400 ); }); // Display height of current hidden ul function getHeight(h) { $j("div.test").text("The height for the hidden ul is " + h + "px."); } }); </code></pre> <hr> <p>I'd like to know how do I get it to use the stored original height, in place of that 100%.</p> <p>And secondly, I'm sure this can all be condensed down to just a few lines, but don't really have a clue how to do that yet.</p> <p>Any help very much appreciated!</p> <p>UPDATE: OK, so I'm ALMOST there. I used the code Marve posted as a base and worked in the other bits. The only thing that's not working is I need to reset the height of the hidden UL, at the end of everything, to it's original height, and set it's display to none, so it's ready to be hovered again. Any ideas why it won't work?</p> <hr> <pre><code>$j('ul.menu &gt; li').hover( function() { var ulHeight = $j('ul', this).height(); $j(this).children('ul').css({height: 0}).stop().animate({height: ulHeight}, 400); }, function() { $j(this).children('ul').stop().animate({height: 0}, 400).css({height: ulHeight, display:none}); } ); </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload