开发者

How to stop jQuery queuing animation effects

开发者 https://www.devze.com 2023-02-25 19:29 出处:网络
I have a simple jQuery hover animation: jQuery(function($)开发者_StackOverflow { $(\'.category\').hover(function(){

I have a simple jQuery hover animation:

jQuery(function($)开发者_StackOverflow {
$('.category').hover(function(){
    $(this).find('.banner').fadeIn();
    },function(){
    $(this).find('.banner').fadeOut(400);
});
});

all this does is when someone hovers over the .category element, the banner div fades in and fades out. Simple.

My problem is that as I have about ten of the .category elements, if someone moves across them too fast or off and on the same one multiple times jQuery queues the animations and keeps making them appear, disappear until it's caught up.

Is there a simple way to stop this from happening?

I saw another question where it was suggested to add .stop, but this doesn't work. the animation stops working completely or comes in only half faded in if I move the mouse over and off too many times.

Thanks in advance


You can convert your fadeOut and fadeIn to use .animate() with options to stop animation queuing.

$('.category').hover(function() {
    $(this).find('.banner').show().animate({
        opacity: "show"
    }, {
        queue: false
    });
}, function() {
    $(this).find('.banner').animate({
        opacity: "hide"
    }, {
        queue: false,
        duration: 400
    });
});

Code example on jsfiddle


When I implemented the top answer, my vertical scroll animation just jittered back and forth..

This was helpful - W3 Schools Set Interval, syntax:

setInterval(function, milliseconds, param1, param2, ...)

Having my parameters of the form { duration: 200, queue: false } forced a duration of zero and it only looked at the parameters for guidance.

This worked for me:

var $scrollDiv = '#mytestdiv';
var $scrollSpeed = 1000;
var $interval = 800;

function configureRepeats() {
   window.setInterval(function () {
       autoScroll($scrollDiv, $scrollSpeed);
   }, $interval, { queue: false });
};

Where 'autoScroll' is:

    $($scrollDiv).animate({
        scrollTop: $($scrollDiv).get(0).scrollHeight
    }, { duration: $scrollSpeed });

    //Scroll to top immediately 
    $($scrollDiv).animate({
        scrollTop: 0
    }, 0);

Happy coding!

Also refer my original comment on this post: How to run two jQuery animations simultaneously?

0

精彩评论

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