鼠标移动事件发生时暂停动画
Pause animation when mousemove event occurs
我有以下代码:
$(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';
},
问题是,当鼠标移动事件发生时,我应该清除动画队列,但我有过渡动画需要先完成。这两种效果应用于同一个元素,所以如果我简单地写如下:
$img.clearQueue().stop();
…不显示过渡动画
您可以看到下面的示例:http://jsfiddle.net/SL8t7/
我建议将onmousemove监听器添加到"start"方法中的动画集中。此外,当"stop"方法触发时,我将删除onmousemove侦听器。
这样鼠标移动只在转换完成后才会被触发。
下面是我所建议的修改的概要
http://jsfiddle.net/XrKna/1/您将看到move事件现在等待转换完成。
我从这里移动了事件绑定…
$(source)
.on('mouseenter', start)
.on('mouseleave', stop);
这里 function start() {
zoom.init();
$img.stop().
fadeTo($.support.opacity ? settings.duration : 0, 1, function(){$img.on('mousemove', zoom.move)});
zoom.automove();
}
function stop() {
$img.off('mousemove', zoom.move);
$img.clearQueue().stop()
.fadeTo(settings.duration, 0);
manual_step = 0;
}
您可以创建自定义队列。例如:
$(img).animate({
// properties
},
{
duration: defaults.speed,
complete: myAutomove,
queue: 'autoMove' // here we attached this animation to a custom queue named 'autoMove'
}.dequeue('autoMove'); // for custom queues we have to start them manually
如果你想在自定义队列中停止动画,只需调用:
$(img).stop('autoMove', true); // the 'true' ensures the custom queue is cleared as well as stopped
我没有真正了解你想要停止的动画和你允许继续的动画所以我没有更新你的jsFiddle,但我认为你明白了
相关文章:
- 最小化时暂停Javascript动画
- Canvas+svg路径动画,在路径中的特定点暂停一段时间,然后继续
- CSS动画暂停并使用javascript播放
- 我如何暂停和恢复这个动画,它可以用纯CSS完成吗
- CSS 动画在元素隐藏时暂停
- 是否有“”的键盘快捷键;暂停动画”;Chrome开发工具中的功能?(F12)
- Safari在重定向/表单提交时暂停所有动画
- jQuery延迟/暂停动画
- 在 HTML 页面中嵌入动画 SVG,暂停(不启动动画)
- JS单击事件不适用于暂停CSS动画
- 在动画中添加播放/暂停
- 有没有办法防止用户交互暂停动画
- 如何在调用函数时暂停动画
- 在鼠标悬停/悬停时暂停动画
- Raphael JS -鼠标悬停时开始/继续动画.鼠标离开时暂停动画
- 将暂停动画's播放状态更改为点击链接运行
- 鼠标移动事件发生时暂停动画
- 暂停动画的执行
- jQuery -暂停动画
- 正确的方式暂停动画时,不在视图中