开发者

Jquery dropdown remove class after disappears

开发者 https://www.devze.com 2023-01-15 17:36 出处:网络
I have this code for a dropdown and I need toremove the class \'hovered\' on \'#products\' after the menu slides up and has disappeared. How do I do this? Currently it disappears straight away onmouse

I have this code for a dropdown and I need to remove the class 'hovered' on '#products' after the menu slides up and has disappeared. How do I do this? Currently it disappears straight away onmouseout

Thanksfor any help in resolv开发者_JAVA百科ing this! :-)

$(function() {
    var divTop = 168;
    $('#products div ul').css({'margin-top': '-' + divTop + 'px','float':'left'});

    $('#products > a,#products div').hover(function(){
        $('#products').addClass('hovered');
        $('#products div ul').show().stop().animate({'margin-top': '0'});
    },function(){
        $('#products').removeClass('hovered');
        $('#products div ul').show().stop().animate({'margin-top': '-' + divTop + 'px'});
    });
});


You can call .removeClass() in the .animate() callback, like this:

$(function() {
    var divTop = 168;
    $('#products div ul').css({'margin-top': '-' + divTop + 'px','float':'left'});

    $('#products > a,#products div').hover(function(){
        $('#products').addClass('hovered');
        $('#products div ul').show().stop().animate({'margin-top': '0'});
    },function(){            
        $('#products div ul').show().stop().animate({'margin-top': '-' + divTop + 'px'}, function() {
          $('#products').removeClass('hovered');
        });
    });
});


You could probably put

function(){
$('#products').removeClass('hovered');
}

as the callback of the animate() function


If I understand you properly, it seems like you just need to make use of the callback function of .animate() (REF: http://api.jquery.com/animate/).

$(function() {
    var divTop = 168;
    $('#products div ul').css({'margin-top': '-' + divTop + 'px','float':'left'});

    $('#products > a,#products div').hover(function(){
        $('#products').addClass('hovered');
        $('#products div ul').show().stop().animate({'margin-top': '0'});
    },function(){
        $('#products div ul').show().stop().animate({'margin-top': '-' + divTop + 'px'}, function(){
            $('#products').removeClass('hovered');
        });
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消