Using JS timer to move elements

Using JS timer to move elements

Use JS timer to make an element to make a method with moving effect. The implementation idea is: first declare a variable to store the distance of the element from the left side, then declare a variable to store the distance the element needs to move each time, and then give this method a completion time. It should be noted that if the obtained value is not numeric data, it needs to be converted, otherwise it cannot be judged. Then determine that after the element moves to a certain position, give it a negative step value and the element will move back.

You can also think about how to achieve the turning effect when the element moves to the left or right side.

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
   *{margin: 0;padding: 0;}
   body{position: relative;}
   #box{
    width: 120px;height: 120px;background: green;
    position: absolute;top: 100px;
    /* Here you can introduce a background image as the target of movement, */
    /* background: url(img/paobu_huaban.png) 0 0/100% 100%; */
   }
  </style>
 </head>
 <body>
  <button type="button" id="Button">Click me to move</button>
  <div id="box" style="left: 0px;"></div>
  
  <script type="text/javascript">
   var Button = document.getElementById("Button");
   var box = document.getElementById("box");
   //How many pixels to move each time, step represents the step length var step = 5;
   Button.onclick = function(){
    
    var timer = setInterval(function(){
     
     //Get the left value of the box and convert it into an integer. It must be converted into a numerical value before performing calculations.
     // parseInt means converting the obtained string into character type var o_left = parseInt(box.style.left);
     //If you want the element to move faster, you can increase the distance of each move or reduce the completion time. //But reducing the completion time will have a better effect. var n_left = o_left+step; //Move right 10px each time
     box.style.left = n_left+"px";
     if ( n_left>500) { //If the moving distance is greater than 400px, run back step = -5;
     }else if(n_left==0){
      step = 5;
     }; 
    },100);
   };
   
  </script>
 </body>
</html>

The results are as follows:

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:
  • Detailed explanation of JavaScript timers
  • JavaScript Timer Details
  • JavaScript timer to achieve limited time flash sale function
  • Several ways to implement 0ms delay timer in js
  • JavaScript timer to achieve seamless scrolling of pictures
  • Detailed explanation of the JavaScript timer principle

<<:  Analysis and description of network configuration files under Ubuntu system

>>:  Implementation code for operating mysql database in golang

Recommend

Deleting two images with the same id in docker

When I created a Docker container today, I accide...

JavaScript implements circular carousel

This article shares the specific code of JavaScri...

How to implement navigation function in WeChat Mini Program

1. Rendering2. Operation steps 1. Apply for Tence...

MySQL and sqlyog installation tutorial with pictures and text

1. MySQL 1.1 MySQL installation mysql-5.5.27-winx...

Implementation of importing and exporting vue-element-admin projects

vue-element-admin import component encapsulation ...

Implementation of element multiple form validation

In the project, form testing is often encountered...

MySQL learning notes help document

View system help help contents mysql> help con...

Detailed explanation of ES6 Promise usage

Table of contents What is a Promise? Usage of rej...

Best Practices for MySQL Upgrades

MySQL 5.7 adds many new features, such as: Online...

Analysis of three parameters of MySQL replication problem

Table of contents 01 sql_slave_skip_counter param...

How to make spaces have the same width in IE and FF?

body{font-size:12px; font-family:"宋体";}...

Summary of several common logs in MySQL

Preface: In the MySQL system, there are many diff...