jQuery toggle animation doesn't work on new jQ

2020-02-07 10:38发布

问题:

I have problem with this toggle on jQuery 1.8.2 i works but on 1.11.0 no. Can you help me what is wrong?

$('.open').toggle(function () {
    $('.obj').animate({
        top: "0"
    }, 500);
},function () {
    $('.obj').animate({
        top: "-8%",
    }, 500);
});

回答1:

As mentioned in the comments you will need to do this using the click method. Here is an example that uses the element's data to store the state:

$('.open').on('click', function(){
    var isToggled = $(this).data('isToggled');
    if(isToggled){
        $('.obj').animate({
            top: "-8%",
        }, 500);
    } else {
      $('.obj').animate({
            top: "0"
        }, 500);
    } 

    $(this).data('isToggled', !isToggled)
});