Example code for multi-object movement in javascript
The content of this article is about the example code of multi-object movement in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Previously we used timers to implement single object movement. In projects, we often do not do single object movement, but multiple objects and multiple value changes.
Here we will realize the movement of multiple objects and arbitrary values by changing parameters. A motion framework that can change the object's width, height, borders, font size, transparency, and more.
Note: In the above chapters, we all use offsetWidth (offsetWidth includes border and padding, etc.) to set and obtain styles, because the usage is simple, but if the object contains border, padding, etc. When styling, an error will be reported, so here we use a more rigorous method currentStyle and getComputedStyle to obtain the style.
Note: We cannot really store some specific values in the computer. We store some approximate values, such as 0.07*100. The final result is not 7, so we will round and convert below. is an integer.
Note: When multiple objects are moving, the timer and some parameters must not be shared, otherwise the movement will be cleared before it is completed and other movements will be triggered, and some parameters cannot be shared. , which will cause some parameters to be re-operated before they reach fixed values. Therefore, the timers in the following examples are placed on elements.
The following is an example we made
<!DOCTYPE html> <html> <head> <title>运动改变宽度、高度、边框、字体、透明度</title> <style> div { width: 200px; height: 200px; background: red; margin: 10px; float: left; border:1px solid #000; font-size:14px; } div:nth-child(5) { filter: alpha(opacity:30); opacity:0.3; } </style> <script> window.onload = function() { var oDiv1 = document.getElementById("div1"); var oDiv2 = document.getElementById("div2"); var oDiv3 = document.getElementById("div3"); var oDiv4 = document.getElementById("div4"); var oDiv5 = document.getElementById("div5"); oDiv1.onmouseover=function(){ startMove(this,"height", 400) } oDiv1.onmouseout=function(){ startMove(this,"height", 200) } oDiv2.onmouseover=function(){ startMove(this,"width", 400) } oDiv2.onmouseout=function(){ startMove(this, "width",200) } oDiv3.onmouseover=function(){ startMove(this,"fontSize", 50) } oDiv3.onmouseout=function(){ startMove(this, "fontSize",14) } oDiv4.onmouseover=function(){ startMove(this,"borderWidth", 100) } oDiv4.onmouseout=function(){ startMove(this, "borderWidth",1) } oDiv5.onmouseover=function(){ startMove(this,"opacity", 100) } oDiv5.onmouseout=function(){ startMove(this, "opacity",30) } } function getStyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; } else{ return getComputedStyle(obj,false)[name]; } } function startMove(obj, attr, iTarget) { clearInterval(obj.timer); obj.timer = setInterval(function() { var cur=0; if(attr==="opacity"){ cur=Math.round(parseFloat(getStyle(obj,attr))*100);//有可能会出现误差0.07*100 } else{ cur=parseInt(getStyle(obj,attr)); } var speed = (iTarget - cur) / 6; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (cur === iTarget) { clearInterval(obj.timer); } else { if(attr==="opacity"){ obj.style.filter="alpha(opacity:"+cur+speed+")"; obj.style.opacity=(cur+speed)/100; }else{ obj.style[attr]=cur+speed+"px"; } } }, 30) } </script> </head> <body> <div id="div1">变宽</div> <div id="div2">变高</div> <div id="div3">文字变大</div> <div id="div4">borderwidth</div> <div id="div5">透明度</div> </body> </html>
Related recommendations:
javascript multi-object motion implementation method analysis_javascript skills
JS implementation of multi-object movement example sharing
The above is the detailed content of Example code for multi-object movement in javascript. 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 WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
