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
If you’re new to Docker, take a look at some of t...
This article introduces how to create an index on...
Harbor is an enterprise-level registry server for...
I have been studying the source code of Vue recen...
1. Introduction Recently, I often encounter devel...
The machines in our LAN can access the external n...
There are many XHTML tags: div, ul, li, dl, dt, d...
1. First download from the official website of My...
Table of contents 1. Download JDK (take jdk1.8.0 ...
Table of contents Overview 0. JavaScript and Web ...
Table of contents Load Balancing Load balancing c...
Today, this post lists some great examples of circ...
Today at work, a friend I added temporarily asked ...
During this period of time, while working on a pr...
Word MySQL 8.0 has been released for four years s...