10000 add scroll bar offset function · AKAChing/fed-code-snippets@b74abb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b74abb9

Browse files
authored
add scroll bar offset function
1 parent 7993a9c commit b74abb9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

ScrollBarOffset/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function addScrollBarOffsetWidth(className) {
2+
const isTouchDevice = "ontouchstart" in window
3+
if (!isTouchDevice) { // 如果是触摸屏则滚动条不占据空间 不进行操作
4+
let observer = new MutationObserver(mutations => {
5+
const flag = mutations.some(mutation => mutation.target.className == className) // 判断是否含有目标属性
6+
if (flag) {
7+
document.querySelector('body').style.paddingRight = 17 + 'px' // 有overflow属性则给body添加padding-right
8+
} else {
9+
document.querySelector('body').style.paddingRight = 0 + 'px' // 没有则归零
10+
}
11+
})
12+
observer.observe(document.querySelector('body'), { attributes: true, attributeFilter: ['class'] })
13+
}
14+
}

0 commit comments

Comments
 (0)
0