The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id='box'></div> <script>var oBox = document.getElementById("box");var maxLeft = utils.win('clientWidth')-oBox.offsetWidth;var step = 5;var timer = window.setInterval(function(){var curLeft = utils.css(oBox,"left");if(curLeft+step >= maxLeft){//边界判断utils.css(oBox,"left",maxLeft); window.clearInterval(timer);return; } curLeft+=step; utils.css(oBox,"left",curLeft); },10)//问题:当总距离/我们设定的步长 = 不是一个整数。少走一步还差点距离到目标位置,多走一步会超出目标的位置//解决:在进行边界判断的时候加上步长来进行处理</script> </body> </html>
The above is the detailed content of Use js to specify the step size to achieve uniform motion in one direction. For more information, please follow other related articles on the PHP Chinese website!