0

How can I change the following menu code to open/close when the mouse hovers, instead of when it is clicked on?

var w = 0;

$('.slide').children().each(function () {
    w += $(this).outerWidth();
});

$('.outer').width(w + 5);
$('.wrap').width(w);
$('.slide').css('left', w);

$('.open').toggle(function () {
    $('.slide').stop().animate({
        left: 0
    });
    $(this).html('close');
}, function () {
    $('.slide').stop().animate({
        left: w
    });
    $(this).html('open');
});

Please check this Demo

In Fiddle, you can see the animation works on click. I need to make it work on hover. Can you help with this ?

1
  • 1
    Will changing toggle to hover won't be enough? Commented Jan 14, 2014 at 7:07

5 Answers 5

1

Use .hover() api

Try this

$('.open').hover(function () {

instead of

$('.open').toggle(function () {
Sign up to request clarification or add additional context in comments.

Comments

1

Just

$('.open').toggle(function() {
    $('.slide').stop().animate({
        left: 0
    });
    $(this).html('close');
}

in this part just replace toggle with hover

Comments

1

Try using $('.wrap').hover. If $('.open').hover, you will not able to click the nav items.

Also, you can create another wrapper to just wrap div.outer and a.open only

$('.wrap').hover(function() {

    $('.slide').stop().animate({
        left : 0
    });

    //this is the .wrap right now
    $(this).find('a.open').html('close');

}, function() {

    $('.slide').stop().animate({
        left : w
    });

  //this is the .wrap right now
    $(this).find('a.open').html('open');
});

http://jsfiddle.net/rooseve/42sWB/2/

Comments

1

$('.open').on('mouseenter mouseleave', function(e) {

if(e.type === 'mouseleave') {
    $('.slide').stop().animate({
        left: w
    });
    $(this).html('open');
} else {
    $('.slide').stop().animate({
        left: 0
    });
    $(this).html('close');
}

});

Comments

0
var w = 0;
var isVisible = false;
$('.slide').children().each(function() {
    w += $(this).outerWidth();
});

$('.outer').width(w+5);
$('.wrap').width(w);
$('.slide').css('left', w);

$('.open').on("mouseover", function() {
    if(isVisible == false) {
        $('.slide').stop().animate({
            left: 0
        });
        $(this).html('close');
        isVisible = true;
    }
    else {
        $('.slide').stop().animate({
            left: w
        });
        $(this).html('open');
        isVisible = false;
    }
});

Firstly, when you hover the element - the div will be visible, until you hover the element for the second time.

Demo: http://jsfiddle.net/42sWB/3/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.