When writing animations with JS, layout conversion is often used, that is, converting relative positioning to absolute positioning before movement, and then executing the animation function. Here is a demo of layout conversion implemented by native JS. The effect is as follows: The following is the code implementation, everyone is welcome to copy, paste and comment. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Native JS implements layout conversion in animation</title> <style> * { margin: 0; padding: 0; } #ul1 { width: 366px; margin: 0 auto; position: relative; } #ul1 li { list-style: none; width: 100px; height: 100px; background: #CCC; border: 1px solid black; float: left; margin: 10px; z-index: 1; } </style> <!-- Motion Frame --> <script> // Get the value of the specified style function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, false)[attr]; } }; //Motion function function startMove(obj, json, fn) { clearInterval(obj.timer); obj.timer = setInterval(function () { 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 (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 (bStop) { clearInterval(obj.timer); if (fn) { fn(); } } }, 30) } </script> <!-- Add event --> <script> window.onload = function () { var oUl = document.getElementById('ul1'); var aLi = oUl.getElementsByTagName('li'); var iMinZindex = 2; var i = 0; // 1. Layout conversion for (i = 0; i < aLi.length; i++) { //aLi[i].innerHTML='left:'+aLi[i].offsetLeft+', top:'+aLi[i].offsetTop; aLi[i].style.left = aLi[i].offsetLeft + 'px'; aLi[i].style.top = aLi[i].offsetTop + 'px'; } // Two loops are required for (i = 0; i < aLi.length; i++) { aLi[i].style.position = 'absolute'; //In the first loop, the offset value has already calculated the original margin value, so it needs to be cleared here aLi[i].style.margin = '0'; } // 2. Add event for (i = 0; i < aLi.length; i++) { aLi[i].onmouseover = function () { //Let the current zIndex continue to increase to prevent stacking this.style.zIndex = iMinZindex++; startMove(this, { width: 200, height: 200, marginLeft: -50, marginTop: -50 }); }; aLi[i].onmouseout = function () { startMove(this, { width: 100, height: 100, marginLeft: 0, marginTop: 0 }); }; } }; </script> </head> <body> <ul id="ul1"> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </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:
|
<<: Tutorial for installing MySQL 8.0.18 under Windows (Community Edition)
>>: Summary of Linux file basic attributes knowledge points
This article shares the specific code of JS to ac...
The Linux seq command can generate lists of numbe...
Let's take a look at my error code first. htm...
In a word: if you buy a cloud server from any maj...
An absolute URL is used to represent all the conte...
If this is the first time you install MySQL on yo...
This article mainly introduces the analysis of My...
This article shares the specific code of JavaScri...
Git is integrated in vscode, and many operations ...
Table of contents What is the Linux system that w...
Table of contents text 1. Prepare the machine 2. ...
First we need to install some dependencies npm i ...
Table of contents 1. Introduction to calculator f...
Table of contents Components - Timeline Custom no...
In the process of making web pages, it is inevita...