gpt4 book ai didi

javascript - 如何在每个给定的时间间隔连续移动

转载 作者:行者123 更新时间:2023-11-30 09:21:31 35 4
gpt4 key购买 nike

这是我的代码:

<!DOCTYPE html>
<html>
<head>

</head>

<body>
<div id="a">BELLA</div>

<script type ="text/javascript">
var myDiv = document.getElementById("a");
var myVar = setInterval(spostaDiv, 1000);
function spostaDiv () {

myDiv.style.marginLeft = "+10px";

}
</script>
</body>
</html>

问题是 div 只移动一次。为什么它不连续运行?

最佳答案

您需要增加边距值才能像这样移动 div -

var myDiv = document.getElementById("a");
var myVar = setInterval(spostaDiv, 1000);
var margin = 0;

function spostaDiv() {
margin += 10;
myDiv.style.marginLeft = margin + "px";
}
<div id="a">BELLA</div>

关于javascript - 如何在每个给定的时间间隔连续移动 <div> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330687/

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