views:

338

answers:

4

Hi. I am developing a mega menu for an ecommerce site. I have uploaded the current version here: http://www.nicklansdell.com/menu/. The menu works great with or without javascript at the moment. What I would really like is to improve the usability when the user have javascript enable by creating a short delay before the menu animates out. My jquery code so far is:

$(function() {
// If no JS CSS menu will still work
$("#menu").removeClass("cssonly");

// Find subnav menus and slide them down
$("#menu li a").hover(function(){ 
    $(this).parent().find("div.subnav").fadeIn(200);
    $(this).parent().hover(function() {
    }, function() {
        // On hovering out slide subnav menus back up
        $(this).parent().find("div.subnav").fadeOut(200);
    })
});

});

Can anyone please help me achieve the delay effect? Many thanks in advance.

+1  A: 

I'm assuming you mean that they need to hover over the item for some time before the menu animates. Use the hoverIntent plugin for this instead of hover -- does exactly what I've described.

tvanfosson
@tvanfosson - thats exactly what I want to do. I have had a go at using the hoverIntent plugin as you suggested but it has not worked I have up dated my code above, what am I doing wrong?
mtwallet
@mtwallet -- can you define "not work" a little more precisely? Are you getting a javascript error? Does it simply not fire as expected? Did you try removing the "hover" code from the `animateIn` function (can't tell if it's just your post or the actual code that's wrong)?
tvanfosson
@tvanfosson - I got it working in the end many thanks for your help I appreciate it
mtwallet
A: 

Delay the hover with setTimeout(), 2nd argument being delay in milliseconds

Chris Tek
@ChrisTek How would I do that?
mtwallet
@mtwallet -- do you just want it to delay the animation or delay before firing the animation so that it doesn't trigger unless you actually stay over the trigger element for a bit. Using a simple setTimeout() will not do the latter, you'd need to check if the mouse is still over the trigger element after the timeout expires -- this is essentially what the hoverIntent plugin does.
tvanfosson
@tvanfosson - thats exactly what I want to do. I have had a go at using the hoverIntent plugin as you suggested but it has not worked I have up dated my code above, what am I doing wrong?
mtwallet
A: 

As suggested by tvanfosson:

        $(document).ready(function(){
        $("#menu li a.link").hoverIntent({
            sensitivity: 3, 
            interval: 200, 
            over: animateOut, 
            timeout: 500, 
            out: animateIn
        });
    }); // close document.ready

    function animateOut(){  
        $(this).addClass("current");
        $(this).parent().find("div.subnav").fadeIn(200);
    }

    function animateIn(){
        $(this).parent().hover(function() {
        }, function() {
        // On hovering out fade subnav menus back in
        $(this).parent().find("div.subnav").fadeOut(200);
        $("#menu li a.link").removeClass("current");
        });
    }
mtwallet
A: 
jur