Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用fragment让页面不频繁layout-何俊演 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!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>
ul {
display: flex;
flex-flow: wrap;
}
li {
margin-top: 1px;
margin-left: 1px;
background: #8f1;
list-style: none;
}
</style>
</head>
<body>
<button class="btn--thrash">updateThrash</button>
<button class="btn--nothrash">updateNoThrash</button>
<p>初始状态</p>

<script>
let createEles = function() {
let ul = document.createElement('ul')
for (let i = 0; i < 1000; i++) {
let li = document.createElement('li')
li.style.width = '10px'
li.style.height = '10px'
ul.appendChild(li)
}
document.body.appendChild(ul)
}

// 直接增加li高度
let updateThrash = function(data) {
let lis = document.querySelectorAll('li')
for (let i = 0; i < lis.length; i++) {
lis[i].style.height = lis[i].getBoundingClientRect().height + 5 + 'px'
lis[i].style.background = '#f18'
}
}

// 使用fragment把ul脱离dom,再增加li高度
let updateNoThrash = function(data) {
let ul = document.querySelector('ul')
let fragment = document.createDocumentFragment()
fragment.appendChild(ul)
let lis = fragment.querySelectorAll('li')
for (let i = 0; i < lis.length; i++) {
lis[i].style.height = lis[i].style.height.replace('px', '') * 1 + 5 + 'px'
lis[i].style.background = '#f81'
}
document.body.appendChild(fragment)
}

let getTime = function(func) {
let start = Date.now()
func()
let end = Date.now()
return end - start
}

createEles()

const p = document.querySelector('p')
document.querySelector('.btn--thrash').addEventListener('click', function() {
p.innerHTML = `点击了updateThrash,小方块增加5px,耗时:${getTime(updateThrash)}ms`
})

document.querySelector('.btn--nothrash').addEventListener('click', function() {
p.innerHTML = `点击了updateNoThrash,小方块增加5px,耗时:${getTime(updateNoThrash)}ms`
})

</script>
</body>
</html>
Binary file added 不重复重排.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 初始.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 重复重排.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.