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

优化了获取docSize 和 docScroll 方法 每次读取缓存,只有在resize 和 scroll 后才会更新缓存 #1225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
79 changes: 55 additions & 24 deletions src/utils/dom/document.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
export const docScroll = {
get top() {
return document.documentElement.scrollTop || document.body.scrollTop
},
get left() {
return document.documentElement.scrollLeft || document.body.scrollLeft
},
set top(value) {
document.documentElement.scrollTop = value
document.body.scrollTop = value
},
set left(value) {
document.documentElement.scrollLeft = value
document.body.scrollLeft = value
},
}

export const docSize = {
get width() {
return document.documentElement.clientWidth || document.body.clientWidth
},
get height() {
return document.documentElement.clientHeight || document.body.clientHeight
},
}
export const docScroll = (function() {
let top
let left
const getScroll = function(e) {
const doc = e.target
top = doc.documentElement.scrollTop || doc.body.scrollTop
left = doc.documentElement.scrollLeft || doc.body.scrollLeft
}
window.addEventListener('DOMContentLoaded', getScroll)
window.addEventListener('scroll', getScroll)
return {
get top() {
return top
},
get left() {
return left
},
set top(value) {
document.documentElement.scrollTop = value
document.body.scrollTop = value
},
set left(value) {
document.documentElement.scrollLeft = value
document.body.scrollLeft = value
},
remove() {
window.removeEventListener('DOMContentLoaded', getScroll)
window.removeEventListener('scroll', getScroll)
},
}
})()

export const docSize = (function() {
let width
let height
const getSize = function(e) {
const doc = e.type === 'resize' ? e.target.document : e.target
width = doc.documentElement.clientWidth || doc.body.clientWidth
height = doc.documentElement.clientHeight || doc.body.clientHeight
}
window.addEventListener('DOMContentLoaded', getSize)
window.addEventListener('resize', getSize, true)
return {
get width() {
return width
},
get height() {
return height
},
remove() {
window.removeEventListener('DOMContentLoaded', getSize)
window.removeEventListener('resize', getSize)
},
}
})()

export function addEventListener(target, eventType, cb, option) {
if (target.addEventListener) {
Expand Down