我有一个按钮,可以让图像旋转 3 秒。我希望在旋转过程中禁用按钮。但这是行不通的。下面是创建旋转的 js 函数和 css 类。
<script>
function spin() {
document.getElementById("spin_switch").disabled = true;
document.getElementById("image-map").classList.toggle("spin");
setTimeout(stop_spin, 3000);
document.getElementById("spin_switch").disabled = false;
}
function stop_spin() {
document.getElementById("image-map").classList.toggle("spin");
}
</script>
<style>
.spin {
transform: rotate(360deg);
webkit-transform: rotate(360deg);
overflow: hidden;
transition-duration: 3s;
transition-property: transform;
}
</style>
答案 0 :(得分:1)
你必须移动这条线
document.getElementById("spin_switch").disabled = false;
进入stop_spin
函数:
function spin() {
document.getElementById("spin_switch").disabled = true;
document.getElementById("image-map").classList.toggle("spin");
setTimeout(stop_spin, 3000);
}
function stop_spin() {
document.getElementById("image-map").classList.toggle("spin");
document.getElementById("spin_switch").disabled = false;
}
否则将立即再次启用 spin_switch。 setTimeout
不会停止整个脚本。它只是注册一个回调,在给定的超时到期后执行。