3

I am making a sliding menu using jQuery and the bounce jQuery UI effect. The problem is that the hover event doesn't activate even once when I hover over the first li element. Here is my JavaScript code:

$(document).ready(function(){
    $("#topbar > ul > li:has(ul) > ul").hide().each(function(){
        var parent = $(this).parent();
        var height = parent.outerHeight();
        $(this).css({
            position: "absolute",
            top: height,
            zIndex: 1000
        });
    });
    $("#topbar > ul > li:has(ul)").hover(function(){
        $("ul", this).slideDown(500, "easeOutBounce");
    }, function(){
        $("ul", this).slideUp(500, "easeOutBounce");
    });
});

And here is my HTML code:

<nav id="topbar">
    <ul>
        <li><a href=""><i class="fa fa-plus"></i>  New</a><ul><li>T</li><li>R</li></ul></li
        ><li><a href="">View</a></li>
    </ul>
</nav>

I am always using the latest version of jQuery provided from jQuery's CDN (http://code.jquery.com/jquery.min.js) and I am using version 1.11.4 of jQuery UI.

Edit: Instead of the .hover method, I also tried the following:

$("#topbar > ul > li:has(ul)").on("mouseover mouseout", function(){
    $("ul", this).slideToggle(500, "easeOutBounce");
});

Edit: JSFiddle Here (Old JSFiddle was wrong.)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jacques Marais
  • 2,666
  • 14
  • 33

1 Answers1

1

Your only real issue seems to be that the background for the UL your are trying to display is transparent.

Add a class like the below to your element:

<ul class="drop"><li>T</li><li>R</li></ul>

Then add the below CSS rule:

.drop{
    background-color:#ccc;
}

As A.Wolf points out changing your code to the below would be better for performance and makes the whole effect a good bit soother.

$(this).find("ul").stop().slideToggle(500, "easeOutBounce");

Try this jsFiddle you will see that the ul is displayed and hidden on hover, and that the animation is now cleaner.

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • @JacquesMarais You're quite welcome, you probably should tweak the height / width of the hovered element to 100% of the container size too. I noticed it's a bit finicky now due to that area being so small, happy coding – Wesley Smith Nov 22 '15 at 13:37
  • 1
    @DelightedD0D You don't need to pass any empty callback, this is perfectly valid: `.slideToggle(500, "easeOutBounce");` – A. Wolff Nov 22 '15 at 13:58
  • @A.Wolff Thanks, looking at [the source for .speed](http://james.padolsey.com/jquery/#v=1.11.2&fn=jQuery.speed) I see you are correct. That's not at all how I read the meaning of the docs that's my bad, Ill update – Wesley Smith Nov 22 '15 at 14:37
  • 1
    As a side note, it should be: `$(this).find("ul").stop().slideToggle(500, "easeOutBounce");` More readable, more performant (if we care about few ms) and better behaviour :) – A. Wolff Nov 22 '15 at 14:39
  • @JacquesMarais Please see AWolfs comments and my update, I was wrong before :) – Wesley Smith Nov 22 '15 at 14:53
  • @DelightedD0D Thanks :) – Jacques Marais Nov 22 '15 at 15:27