[英]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毫秒后開始播放新動畫。
解決方案是將“ setInterval”分配給變量,然后可以調用“ clearInterval”。
var interval = setInterval(move(),300);
});
}
var setinterval = setInterval(move(),300);
現在您可以停止間隔:
$("h1").click(function() {
clearInterval(interval);
$('h1').stop();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.