-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
当点击图片时,滚动条会被隐藏导致整体页面重拍;现在已经修复
- Loading branch information
master1lan
committed
Feb 19, 2023
1 parent
6f4788a
commit 26f8a26
Showing
3 changed files
with
90 additions
and
0 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
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,71 @@ | ||
| /** | ||
| * 给元素style添加属性。 | ||
| */ | ||
| export const ObjectToHtmlStyle = ( | ||
| obj: React.CSSProperties, | ||
| ele: HTMLElement | ||
| ) => { | ||
| Object.entries(obj).forEach(([key, value]) => { | ||
| if (value) { | ||
| //@ts-ignore | ||
| ele.style[key] = value; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| let _ScrollBarWidthCached: number | null = null; | ||
|
|
||
| function hasScrollbar(): boolean { | ||
| return ( | ||
| document.body.scrollHeight > | ||
| (window.innerHeight || document.documentElement.clientHeight) | ||
| ); | ||
| } | ||
| export function getScrollBarSize(): number { | ||
| if (!hasScrollbar()) { | ||
| _ScrollBarWidthCached = 0; | ||
| } | ||
| if (typeof _ScrollBarWidthCached === "number") { | ||
| return _ScrollBarWidthCached; | ||
| } | ||
| const inner = document.createElement("div"); | ||
| ObjectToHtmlStyle( | ||
| { | ||
| width: "100%", | ||
| height: "200px", | ||
| }, | ||
| inner | ||
| ); | ||
| const outer = document.createElement("div"); | ||
| ObjectToHtmlStyle( | ||
| { | ||
| position: "absolute", | ||
| top: "0", | ||
| left: "0", | ||
| pointerEvents: "none", | ||
| visibility: "hidden", | ||
| width: "200px", | ||
| height: "150px", | ||
| overflow: "hidden", | ||
| }, | ||
| outer | ||
| ); | ||
| outer.appendChild(inner); | ||
| document.body.appendChild(outer); | ||
| const widthContained = inner.offsetWidth; | ||
| ObjectToHtmlStyle( | ||
| { | ||
| overflow: "scroll", | ||
| }, | ||
| outer | ||
| ); | ||
| let widthScroll = inner.offsetWidth; | ||
|
|
||
| if (widthContained === widthScroll) { | ||
| widthScroll = outer.clientWidth; | ||
| } | ||
|
|
||
| document.body.removeChild(outer); | ||
| _ScrollBarWidthCached = widthContained - widthScroll; | ||
| return _ScrollBarWidthCached; | ||
| } |
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