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

Docker Tutorial: Using Containers (Simple Example)

If you’re new to Docker, take a look at some of t...

How to create an index on a join table in MySQL

This article introduces how to create an index on...

Docker5 full-featured harbor warehouse construction process

Harbor is an enterprise-level registry server for...

Several ways to pass data from parent components to child components in Vue

I have been studying the source code of Vue recen...

A detailed analysis of the murder caused by a misplaced double quote in MySQL

1. Introduction Recently, I often encounter devel...

SSH port forwarding to achieve intranet penetration

The machines in our LAN can access the external n...

Introduction to nesting rules of html tags

There are many XHTML tags: div, ul, li, dl, dt, d...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

25 Examples of Using Circular Elements in Web Design

Today, this post lists some great examples of circ...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

Detailed explanation of invisible indexes in MySQL 8.0

Word MySQL 8.0 has been released for four years s...