前端性能優化--重繪與迴流

簡介

clipboard.png

clipboard.png

clipboard.png

clipboard.png

實際案例

clipboard.png

說明

performance:性能
layers: 圖層
recalculate style: 從新計算樣式
paint:重繪
layout:迴流
update layer tree: 更新圖層樹
composite layers: 合併圖層html

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>

clipboard.png

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>

clipboard.png

用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>

clipboard.png

clipboard.png

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>

clipboard.png

clipboard.png

一個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>
相關文章
相關標籤/搜索