javascript - jquery .animate() 鼠标悬停/鼠标悬停

标签 javascript jquery animation mouseover mouseout

我有一个 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
        });
}

似乎在 mouseout 上我可以在不调整 div 大小的情况下恢复动画,或者在不恢复动画的情况下调整 div 大小。

有人可以帮我解决这个问题吗?

这是一个有效的 js fiddle

http://jsfiddle.net/29cwcx04/

谢谢

最佳答案

我认为,问题在于您在完成将圆的大小重新调整回正常大小之前剪切了宽度和高度动画。这是一个快速修复:

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

关于javascript - jquery .animate() 鼠标悬停/鼠标悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33172273/

相关文章:

javascript - 保证触发 onmouseout 事件

javascript - 在响应式网站上无法将高度缩放到宽度

javascript - 通过 jQuery ajax 将 jQuery 构造的数组发送到 PHP

android - Android 中的按钮按下/点击动画

angularjs - 在angularjs中同步音频和动画

javascript - 在javascript中替代if,else if,else if,else if等

javascript - Bootstrap+HighCharts.js 圆环图中的响应式图像位置

javascript - 仅当文档中不存在提供者时才更新提供者数组并增加计数

javascript - 如何在 javascript 中拆分语言字符串(Wordpress qTranslate)

python - 安装 ffmpeg 时 Matplotlib 动画错误 : Requested MovieWriter (ffmpeg) not available,