How to implement multi-object movement in JS (detailed tutorial)
This article mainly introduces the method of JS to realize multi-object movement. It analyzes the principles and related operation techniques of JavaScript to realize multi-object movement in detail in the form of examples. Friends in need can refer to this article
The example analyzes the method of realizing multi-object movement in JS. Share it with everyone for your reference, the details are as follows:
Basic steps
1. Get the elements for multi-object movement through getElementsByTagName
2. Then the for loop traverses the elements and adds events
3. Define the startMove function, which requires two parameters, the current "doing exercise" element, and the target value target
Note: In multi-object motion, everything cannot be shared;
<script> window.onload = function(){ var liTags = document.getElementsByTagName('li'); // 第一步 for(var i=0;i<liTags.length;i++){ // 第二步 liTags[i].onmouseover = function () { startMove(this,400); } liTags[i].onmouseout = function () { startMove(this,200); } } } var timer = null; function startMove(obj,iTarget) { // 第三步,2个参数 clearInterval(timer); timer = setInterval(function () { var iSpeed = (iTarget - obj.offsetWidth)/10; iSpeed = iSpeed>0 ?Math.ceil(iSpeed):Math.floor(iSpeed);// 缓冲运动注意取整处理 if(obj.offsetWidth == iTarget){ clearInterval(timer); }else{ obj.style.width = obj.offsetWidth+iSpeed+'px'; } },30); } </script>
Problem:
When the moving in and out speed is relatively fast , it will appear that some li cannot return to its original state and is stuck in the middle; This is because all li share a timer;
When the mouse moves into the first When a li is reached, call startMove to start a timer; when the mouse removes the li, a timer also needs to be started to return the li to its original position. When the li reaches halfway, we move into the second li, and the timer will be cleared first. At this time, the first li got stuck halfway.
Solve this problem: Let each li have its own timer to control their changes, and define it for each li during the for loop One of your own timer
liTags[i].timer = null;// 给每个li都添加一个timer
Then the timer used every time in starMove is the current li's own, so there will be no interference with each other.
The previous timer here has been changed to obj.timer
(the current object’s own timer); there is no problem here.
The complete code is as follows:
<body> <ul> <li></li> <li></li> <li></li> <li></li> </ul> </body>
Add dot style:
<style> ul{list-style: none;} ul li{ margin: 10px; width: 200px;height: 50px; background: lightblue; } </style>
Complete js code
<script> window.onload = function(){ var liTags = document.getElementsByTagName('li'); for(var i=0;i<liTags.length;i++){ liTags[i].timer = null;// 给每个li都添加一个timer liTags[i].onmouseover = function () { startMove(this,400); } liTags[i].onmouseout = function () { startMove(this,200); } } } function startMove(obj,iTarget) { clearInterval(obj.timer); obj.timer = setInterval(function () { var iSpeed = (iTarget - obj.offsetWidth)/10; iSpeed = iSpeed>0 ?Math.ceil(iSpeed):Math.floor(iSpeed); if(obj.offsetWidth == iTarget){ clearInterval(obj.timer); }else{ obj.style.width = obj.offsetWidth+iSpeed+'px'; } },30); } </script>
Then look at an example: multi-object movement-changing transparency
<script> window.onload = function () { var aImgs = document.getElementsByTagName('img'); for(var i=0;i<aImgs.length;i++){ aImgs[i].timer = null; aImgs[i].alpha = 100; // 把公用的alpha改成每个img对象都有的属性 aImgs[i].onmouseover = function () { startMove(this,30); } aImgs[i].onmouseout = function () { startMove(this,100); } } } // var alpha = 100; 这里alpha在多物体运动里 不能公用 function startMove(obj,iTarget) { clearInterval(obj.timer); var iSpeed = (iTarget - obj.alpha)/10; iSpeed = iSpeed>0 ? Math.ceil(iSpeed): Math.floor(iSpeed); obj.timer = setInterval(function(){ if(obj.alpha == iTarget){ clearInterval(obj.timer); }else{ obj.alpha += iSpeed; obj.style.opacity =obj.alpha/100; obj.style.filter = 'alpha(opacity:'+obj.alpha+')'; } },30); } </script>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
What are the methods to add new properties of objects to the detection sequence in vue?
How to achieve the animation effect of bouncing off the edge in jQuery?
How to use sass in vue cli webpack (detailed tutorial)
How to change the P tag text value in jQuery
By creating tags in js dynamics and setting attribute methods (detailed tutorial)
Implement the method of adding and assigning tag sub-elements in jQuery
How to implement the commonly used message box in JS?
The above is the detailed content of How to implement multi-object movement in JS (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
