Home > Web Front-end > JS Tutorial > body text

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

巴扎黑
Release: 2017-07-20 13:34:56
Original
1917 people have browsed it

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!