-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path关于页面重绘和重排.html
148 lines (129 loc) · 3.39 KB
/
关于页面重绘和重排.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style type="text/css">
html {
font: 14px Helvetica, sans-serif;
background: black;
color: white;
}
* {
box-sizing: border-box;
margin-bottom: 1rem;
}
h1,
p {
max-width: 400px;
}
h1 {
font-size: 3em;
//word-break: break-word;
-webkit-hyphens: auto;
}
div {
float: left;
width: 25%;
margin: 0;
background: white;
border: solid 2px black;
}
section {
overflow: hidden;
}
#speed {
font-size: 2.4em;
}
</style>
<body>
<h1>Prevent layout thrashing with requestAnimationFrame</h1>
<p>In this example we are carrying out the task of setting heights of four divs on the page based on their width. <strong>We've solved the problem in two ways:</strong></p>
<p><strong>The first</strong> is less performant because it causes the DOM to reflow 4 times by reading and writing in quick succession.</p>
<p><strong>The second</strong> makes use of requestAnimationFrame to schedule all DOM writes for the next frame. This means that all the reads happen together, followed by all the writes, and our layout doesn't get 'thrashed' :)</p>
<p>Run timeline in DevTools and observe the number of layouts event that occur for each solution.</p>
<section>
<button id="thrash">With layout thrash</button>
<button id="nothrash">Without layout thrash</button>
<section id="speed"></section>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</section>
<script type="text/javascript">
var divs = document.querySelectorAll('div');
var raf = window.requestAnimationFrame;
var each = [].forEach;
var now = function() {
return performance
? performance.now()
: Date.now();
};
/**
* Thrashing solution
*/
thrash.onclick = function() {
reset();
var start = now();
// Loop each div
each.call(divs, function(div) {
var width = div.clientWidth;
div.style.height = width + 'px';
});
// Render result
renderSpeed(now() - start);
};
/**
* Non-thrashing solution
*/
nothrash.onclick = function() {
reset();
var start = now();
// Loop each div
each.call(divs, function(div) {
var width = div.clientWidth;
// Schedule the write
// operation to be run
// in the next frame.
raf(function() {
div.style.height = width + 'px';
});
});
// Render result
raf(function() {
renderSpeed(now() - start);
});
};
// Resets the divs
function reset() {
each.call(divs, function(div) {
div.style.height = '';
div.offsetTop;
});
}
function renderSpeed(ms) {
speed.textContent = ms + 'ms';
}
</script>
</body>
</html>