gpt4 book ai didi

javascript - 按钮 onclick 使其他脚本停止运行

转载 作者:行者123 更新时间:2023-11-28 03:29:46 25 4
gpt4 key购买 nike

我有以下代码:

$(function() {
$("#buttonID").click( function() {
// stop funcName() running
});
});

$(function funcName(){
// some scripts will run on page load
});

我的问题是当点击 #buttonID 时如何停止 funcName() 运行。

谢谢大家。

编辑:这是将在页面加载时运行的脚本:https://jsfiddle.net/1t8z3Ls2/1/ (我在 Google 上找到了这个脚本。这个脚本将使 div 子级粘在其父级 div 上。)

我想通过单击按钮使该脚本停止运行。

再次感谢。

最佳答案

在下面的代码中:
- 你有一个红色 div
- 您只需一个按钮
- 在 CSS 中,您声明了一个动画但未使用
- 当页面加载时,第一个JS block 代码将自动执行,该代码块将动画'moveRight'分配给div'player'
- 动画将使玩家div向右移动一秒
- 如果用户点击“停止动画”按钮,将执行 stopMoving() 函数来停止播放器 div 移动
祝你好运!

<!DOCTYPE html>
<html>

<head>
</head>

<body>
<style>
#player {
min-width: 80px;
min-height: 80px;
background-color: red;
position: absolute;
top: 10%;
left: 0%;
}

@keyframes moveRight {
0% {
top: 10%;
left: 0%;
}
20% {
top: 10%;
left: 20%;
}
40% {
top: 10%;
left: 40%;
}
60% {
top: 10%;
left: 60%;
}
80% {
top: 10%;
left: 80%;
}
100% {
top: 10%;
left: 100%;
}
}
</style>
<button onclick="stopMoving()">Stop Animation</button>
<div id="player"></div>

<script>
//This code will be executed on page load.
//It will give the div 'player' an animation to make it move to the right
let player = document.getElementById("player");
player.style.animationName = "moveRight";
player.style.animationDuration = "60s";
player.style.animationFillMode = "forwards";



function stopMoving() {
//This function will be executed when user click on the button.
//This function will stop the div from moving to the right
player.style.animationName = "";
}
</script>
</body>

</html>

关于javascript - 按钮 onclick 使其他脚本停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58275799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com