jquery .animate() mouseover / mousout

jquery .animate() mouseover / mousout

本文关键字:mousout mouseover animate jquery      更新时间:2023-09-26

我有一个div,它可以在指定区域内灵活地改变位置。这工作得很好。现在我希望动画在鼠标悬停时暂停,在鼠标悬停时恢复,并在鼠标悬停时放大div,并在鼠标悬停时将其大小调整为先前的小尺寸。

我的代码如下所示:

function animateBubble(canvas, bubble){
    newB = newSize();
    newQ = newPosition(canvas, bubble);
    oldQ = $(bubble).offset();
    speed = calcSpeed([oldQ.top, oldQ.left], newQ);
    $(bubble).animate({ // initial animation start of bubble
        top: newQ[0],
        left: newQ[1],
        width: newB[0],
        height: newB[1]
    },
    {   duration: speed, // set the duration
        step: function( now, fx ) { // get the coordinates of the bubble dynamically
            var offset = $(this).offset();
            xPos = offset.left; // now position
            yPos = offset.top;
            nowWidth = $(this).width();
            nowHeight = $(this).height();
        },
        complete: function(){ // do the whole animation again upon completion
            animateBubble(canvas, bubble);
        }
    }).mouseover(function(){ // pause animation on mouseover
        $(bubble).stop();
        $(bubble).height(230);
        $(bubble).width(230);
        }).mouseleave(function(){ // restart animation on mouseout
            //alert('hello');
            $(this).height(nowHeight);
            $(this).width(nowWidth);
            $(this).start();
            animateBubble(canvas, bubble); // doesn't want to start again
        });
}

似乎在鼠标退出时,我可以在不调整div大小的情况下恢复动画,或者在不恢复动画的情况下调整div大小。

有人能帮我一下吗?

这里是一个工作的js提琴

http://jsfiddle.net/29cwcx04/

谢谢

问题,我认为,是你的切割宽度和高度动画之前,它完成重新调整圆圈的大小回到正常的大小。这里有一个快速修复:

...mouseleave(function(){ // restart animation on mouseout
            //alert('hello');
            $(this).height(nowHeight);
            $(this).width(nowWidth);
            setTimeout(function(){animateBubble(canvas, bubble);},1000); 
        });