tags:

views:

111

answers:

2

I'm writing my own simple drop-down...and it's not going so well.

I need only the direct child of the list that's being hovered over to slide down.

Also, it needs to stay down if the user is over the link OR the sub-menu.

Thanks for the help!!!

My basic HTML:

            <ul>
                <li class="media drop-down"><a href="#">Link</a>
                    <ul class="sub-menu">
                     <li>
                     This is stuff!
                     </li>
                    </ul>
                </li>
            </ul>

My jQuery

$('.sub-menu').hide();
var menu = $('.drop-down').children('ul');
var down = function (x) {menu.stop().slideDown(); }
var up = function (x) {menu.stop().slideUp(); }
$("li.drop-down").hover(down, up);//show/hide buttons
+2  A: 

This should do it - note the context as the second parameter to $.

$('.sub-menu').hide();
var down = function (x) {$('ul.sub-menu', this).stop().slideDown(); }
var up = function (x) {$('ul.sub-menu', this).stop().slideUp(); }
$("li.drop-down").hover(down, up);//show/hide buttons
Greg
This is good. I just need the sub-menu to stay up when I move b/t the menu and sub-menu.
Kevin Brown
If you remove the .stop() it works better
Greg
Not sure exactly what's going on... it looks like after you call .stop() then next time it won't slideDown past the point you stopped at
Greg
A: 

This line

var menu = $('.drop-down').children('ul');

returns a set of all ul elements which are direct children of elements with the class 'drop-down'. What you probably want to do is include the following in each of your up and down functions instead:

var menu = $(this).children('ul');
Adam Bard
`var menu = $(this).children('ul);` in this context, $(this) does not mean what you want it to. It probably refers to the entire document right there.
idrumgood
Within a callback event such as hover, $(this) refers to the triggering element. I suggested he put that line within each of the functions, although maybe that wasn't clear.
Adam Bard