"/> Document
">

Home  >  Article  >  Web Front-end  >  Use js to specify the step size to achieve uniform motion in one direction

Use js to specify the step size to achieve uniform motion in one direction

巴扎黑
巴扎黑Original
2017-07-20 13:34:561899browse

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id=&#39;box&#39;></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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn