如何让 .stop 工作并确保标题在我单击时停止,而不是在我单击的位置停止
how do i get .stop to work and make sure that the heading stops when i click and not where i click
如何使 .stop 使标题停止在我单击的位置。 并且不会因动画而移动。 仅使用 JavaScript。我需要标题在我单击的位置停止,但我需要单击标题以使其停止,这样我就无法单击任何地方,并且标题移动到那里然后停止,当我单击标题所在的位置时停止。
<!DOCTYPE html>
<html>
<head>
<title>Interactive programming</title>
</head>
<body>
<h1 id="heading" style="position:absolute;">Watch the moving heading!</h1>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<script>
function move() {
$("h1").animate({
"left": "+=200px"
}, "slow")
.animate({
"top": "+=200px"
}, "slow")
.animate({
"left": "-=200px"
}, "slow")
.animate({
"top": "-=200px"
}, "slow", function() {
setInterval(move(),300)
});
}
setInterval(move(),300)
//this should stop it
$("h1").click(function() {
$("h1").stop();
})
</script>
</body>
</html>
"setInterval"将继续运行,因此您将在 300 毫秒后启动一个新动画,即使您调用"stop"。
解决方案是将"setInterval"分配给var,然后您可以调用"clearInterval"。
var interval = setInterval(move(),300);
});
}
var setinterval = setInterval(move(),300);
现在您可以停止间隔:
$("h1").click(function() {
clearInterval(interval);
$('h1').stop();
}
相关文章:
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 如何在显示侧边栏时禁用背景,单击除侧边栏之外的任何位置都会关闭侧边栏
- 在TWebBrowser(谷歌地图)中显示位置,而无需单击按钮
- 防止用户在jQuery Mobile中ajax加载页面时单击其他位置
- 如何查找离单击位置最近的子项
- JS-是否可以获得元素单击位置的确切宽度
- 在单击位置显示动画
- 创建存储按钮单击位置的数组
- 确定进度条上的单击位置
- 将对象附加到具有绝对位置的单击位置
- 获取鼠标单击位置坐标
- 在单击位置打开弹出窗口
- 检测画布中的鼠标单击位置
- 通过滚动主页获得不同的高度单击位置
- 如何在单击位置旁边输出文本
- 在缩放图像上获得正确的单击位置
- Jquery拖动框到鼠标单击位置
- 在css调整大小后单击画布中的实际单击位置
- 将图像缩小到鼠标单击位置的坐标?(jQuery)
- 确定鼠标单击位置的位置