JS implements layout conversion in animation

JS implements layout conversion in animation

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:
  • Vue.js implements grid list layout conversion method

<<:  Tutorial for installing MySQL 8.0.18 under Windows (Community Edition)

>>:  Summary of Linux file basic attributes knowledge points

Recommend

JS realizes the automatic playback effect of pictures

This article shares the specific code of JS to ac...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...

How to open ports to the outside world in Alibaba Cloud Centos7.X

In a word: if you buy a cloud server from any maj...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

JavaScript implements front-end countdown effect

This article shares the specific code of JavaScri...

VSCode configuration Git method steps

Git is integrated in vscode, and many operations ...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Vue3.0 adaptive operation of computers with different resolutions

First we need to install some dependencies npm i ...

Element Timeline implementation

Table of contents Components - Timeline Custom no...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...