This article shares with you a uniform motion implemented with native JS, the effect is as follows: It should be noted that this kind of motion effect is rarely used in actual development. Elastic motion and buffering motion are more commonly used. The following is the code implementation. You are welcome to copy, paste and comment. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Native JS realizes uniform motion of various sports</title> <style> #div1 { width: 100px; height: 100px; position: absolute; background: red; left: 0; top: 50px; } span { width: 1px; height: 300px; background: black; position: absolute; left: 300px; top: 0; } ; </style> <script type="text/javascript"> var timer = null; function startMove(iTarget) { var oDiv = document.getElementById('div1'); clearInterval(timer); timer = setInterval(function () { var iSpeed = 0; if (oDiv.offsetLeft < iTarget) { iSpeed = 7; } else { iSpeed = -7; } //Has it reached the end point if (Math.abs(oDiv.offsetLeft - iTarget) < 7) { //Reach the end point clearInterval(timer); oDiv.style.left = iTarget + 'px'; } else { //Before arriving oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px'; } }, 30); } </script> </head> <body> <input type="button" value="Start movement" onclick="startMove(300)" /> <div id="div1"></div> <span></span> </body> </html> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Summary of discussion on nginx cookie validity period
>>: mysql 8.0.19 win10 quick installation tutorial
One sentence to introduce HOC What is a higher-or...
When doing database statistics, you often need to...
This tutorial shares the installation of mysql in...
When we work in a terminal or console, we may not...
mysql basic data types Overview of common MySQL d...
The project was tested these days, and the tester...
Fault description percona5.6, mysqldump full back...
Table of contents MySQL result sorting - Aggregat...
This article describes how to create multiple ins...
1. Introduction When we log in to MySQL, we often...
As a software developer, you must have a complete...
When it comes to bionic design, many people will t...
As an entry-level Linux user, I have used simple ...
Getting Started with Data Volumes In the previous...
Record the installation and configuration method ...