前端机能优化--重绘与回流

简介

《前端机能优化--重绘与回流》

《前端机能优化--重绘与回流》

《前端机能优化--重绘与回流》

《前端机能优化--重绘与回流》

现实案例

《前端机能优化--重绘与回流》

申明

performance:机能
layers: 图层
recalculate style: 从新盘算款式
paint:重绘
layout:回流
update layer tree: 更新图层树
composite layers: 兼并图层

translate替代top

top

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            position: relative;
            top: 0;
            transform: translateY(0);
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').style.top = '100px';
        }, 2000)
    </script>
</body>

</html>

《前端机能优化--重绘与回流》

translate

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            transform: translateY(0);
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').style.transform = 'translateY(100px)';
        }, 2000)
    </script>
</body>

</html>

《前端机能优化--重绘与回流》

用opacity替代visibility

visibility

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').style.visibility = 'hidden';
        }, 2000)
    </script>
</body>

</html>

《前端机能优化--重绘与回流》

《前端机能优化--重绘与回流》

opacity

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: red;
            opacity: 1;
            transform: translateZ(0)
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').style.opacity = '0';
        }, 2000)
    </script>
</body>

</html>

《前端机能优化--重绘与回流》

《前端机能优化--重绘与回流》

一个class包括多个款式,替代多个款式修正

修正前

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            position: relative;
            will-change: transform;
            width: 100px;
            height: 100px;
            background-color: red;
            opacity: 1;
        }

    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').style.opacity = '0';
            document.querySelector('#box').style.width = '300px';
            document.querySelector('#box').style.height = '300px';
            document.querySelector('#box').style.left = '300px';
            document.querySelector('#box').style.top = '200px';
            document.querySelector('#box').style.opacity = '0';
        }, 2000)
    </script>
</body>

</html>

修正后

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            position: relative;
            will-change: transform;
            width: 100px;
            height: 100px;
            background-color: red;
            opacity: 1;
        }
        
        #box.action {
            width: 200px;
            height: 300px;
            left: 300px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        setTimeout(() => {
            document.querySelector('#box').className = "action";
        }, 2000)
    </script>
</body>

</html>
    原文作者:渣渣辉
    原文地址: https://segmentfault.com/a/1190000018303261
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞