我有以下代码:
$(source)
.on('mouseenter', start)
.on('mouseleave', stop)
.on('mousemove', zoom.move);
这里我附上了几个鼠标事件监听器。当 'mouseenter' 事件发生时,将执行以下函数:
automove: function myAutomove() {
var xPos = rand(0, outerWidth);
var yPos = rand(0, outerHeight);
$(img).animate({
'top': (yPos - offset.top) * -yRatio + 'px',
'left': (xPos - offset.left) * -xRatio + 'px'
}, defaults.speed, myAutomove);
}
它现在很好用,但是当 'mousemove' 事件发生时,会执行以下操作:
move: function (e) {
var left = (e.pageX - offset.left),
top = (e.pageY - offset.top);
top = Math.max(Math.min(top, outerHeight), 0);
left = Math.max(Math.min(left, outerWidth), 0);
img.style.left = (left * -xRatio) + 'px';
img.style.top = (top * -yRatio) + 'px';
},
问题是当 mousemove 事件发生时我应该清除动画队列,但是我有过渡动画需要先完成。这两种效果都应用于同一个元素,所以如果我简单地编写以下内容:
$img.clearQueue().stop();
...不显示过渡动画。
您可以在以下小提琴中看到实时示例:http: //jsfiddle.net/SL8t7/