-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: scroll bar hide effect page width
- Loading branch information
Showing
3 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
:-webkit-full-screen { | ||
background-color: white !important; | ||
background-color: white !important; | ||
} | ||
:-moz-full-screen { | ||
background-color: white !important; | ||
background-color: white !important; | ||
} | ||
:-ms-fullscreen { | ||
background-color: white !important; | ||
background-color: white !important; | ||
} | ||
:fullscreen { | ||
background-color: white !important; | ||
} | ||
background-color: white !important; | ||
} | ||
.scroll { | ||
&--lock { | ||
overflow: hidden !important; | ||
} | ||
&__wrap { | ||
overflow: auto; | ||
height: 100%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
let scrollBarWidth: number | ||
export const getScrollBarWidth = ({ target = document.body } = {}): number => { | ||
if (scrollBarWidth !== undefined) return scrollBarWidth | ||
|
||
const outer = document.createElement('div') | ||
outer.className = 'scroll__wrap' | ||
outer.style.visibility = 'hidden' | ||
outer.style.width = '100px' | ||
outer.style.position = 'absolute' | ||
outer.style.top = '-9999px' | ||
target.appendChild(outer) | ||
|
||
const widthNoScroll = outer.offsetWidth | ||
outer.style.overflow = 'scroll' | ||
|
||
const inner = document.createElement('div') | ||
inner.style.width = '100%' | ||
outer.appendChild(inner) | ||
|
||
const widthWithScroll = inner.offsetWidth | ||
outer.parentNode?.removeChild(outer) | ||
scrollBarWidth = widthNoScroll - widthWithScroll | ||
|
||
return scrollBarWidth | ||
} | ||
|
||
export const lockScroll = ({ target = document.body } = {}) => { | ||
let scrollBarWidth = 0 | ||
let originWidth = '0' | ||
|
||
const clockClass = 'scroll--lock' | ||
|
||
const cleanLock = () => { | ||
target && (target.style.width = originWidth) | ||
target.classList.remove(clockClass) | ||
} | ||
|
||
const hasHiddenClass = target.classList.contains(clockClass) | ||
if (!hasHiddenClass) { | ||
originWidth = target.style.width | ||
} | ||
scrollBarWidth = getScrollBarWidth({ target }) | ||
const hasOverflow = (target === document.body ? document.documentElement : target).clientHeight < target.scrollHeight | ||
const overflowY = window.getComputedStyle(target).overflowY | ||
// only when the scrollbar exists needs to reduce width | ||
if (scrollBarWidth > 0 && (hasOverflow || overflowY === 'scroll') && !hasHiddenClass) { | ||
target.style.width = `calc(100% - ${scrollBarWidth}px)` | ||
} | ||
target.classList.add(clockClass) | ||
|
||
return cleanLock | ||
} |