This article shares with you a compound motion implemented with native JS. The so-called compound motion means that different attributes will change in the same interval. The effect is as follows: The implementation code is as follows. 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 compound motion of various motions</title> <style> #div1 { width: 100px; height: 100px; background: red; opacity: 0.3; } </style> <script> function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, false)[attr]; } } function startMove(obj, json, fn) { clearInterval(obj.timer); obj.timer = setInterval(function () { //Set the switch to prevent other values from stopping changing after a certain value is reached var bStop = true; for (var attr in json) { var iCur = 0; if (attr == 'opacity') { iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100); } else { iCur = parseInt(getStyle(obj, attr)); }; var iSpeed = (json[attr] - iCur) / 8; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //If a certain value has not been reached, bStop is false if (iCur != json[attr]) { bStop = false; }; if (attr == 'opacity') { obj.style.filter = 'alpha(opacity:' + (iCur + iSpeed) + ')'; obj.style.opacity = (iCur + iSpeed) / 100; } else { obj.style[attr] = iCur + iSpeed + 'px'; } } //If it is true in the last round of loop, the timer will be cleared if (bStop) { clearInterval(obj.timer); if (fn) { fn(); } } }, 30) } </script> <script> window.onload = function () { var oBtn = document.getElementById('btn1'); var oDiv = document.getElementById('div1'); oBtn.onclick = function () { startMove(oDiv, { width: 400, height: 200, opacity: 100 }); }; }; </script> </head> <body style="background:#0F0;"> <input id="btn1" type="button" value="Start exercise" /> <div id="div1"></div> </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:
|
<<: How to use Docker Swarm to build WordPress
>>: Detailed steps to install MySQL 5.7 via YUM on CentOS7
Table of contents 1. Download MySQL msi version 2...
Let's take a look at the situation where Secu...
introduction As usual, let's start with a sce...
1. Basic Concepts 1. Sitemesh is a page decoratio...
Effect demo.html <html> <head> <me...
Preface 1. The tools used in this article can be ...
This article shares the specific code of the WeCh...
Table of contents Introduction to WiFi Wireless T...
Table of contents Overview Problem Description Ci...
1. Error details Once when manually performing a ...
A style sheet describes how a document should be ...
What is an index? Why create an index? Indexes ar...
Table of contents Directory Structure bin directo...
There is a new feature that requires capturing a ...
Introduction Animation allows you to easily imple...