猿问

如何用js使一个div框无限移动(要用计时器)?

<html>
<head>
 <title></title>
</head>
<body>
 <div id="zxc" style="background-color: red;width: 100px;height: 100px;margin-left:100px;"></div>
 <script type="text/javascript">
  var x=document.querySelector('.zxc');
  setInterval(function(){
x.style.left+=10+'px';
  }, 10)
 </script>

</body>
</html>


qq_满地祈愿长伴_0
浏览 1711回答 1
1回答

千秋此意

<html><head>    <meta charset="utf-8">    <title>test</title></head><body>    <div id="zxc" style="background-color:red; width:100px; height:100px; position:absolute; left:100px; top:100px;"></div>    <script type="text/javascript">    var x = document.querySelector('#zxc');    setInterval(function() {        x.style.left = (x.offsetLeft + 10) + 'px';    }, 16)    </script></body></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答