我有一个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示例:
谢谢!