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

style(getScrollbarWidth ): 根据不同的容器dom进行获取scroll 的宽度 #1968

Merged
merged 2 commits into from
Oct 14, 2024
Merged
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
18 changes: 10 additions & 8 deletions js/utils/getScrollbarWidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export function getScrollbarWidthWithCSS() {
return scrollbarWidth;
}

// 获取 body 下滚动条宽度
export function getScrollbarWidth() {
const scrollDiv = document.createElement('div');
scrollDiv.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
document.body.appendChild(scrollDiv);
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
return scrollbarWidth;
/**
* @description: Calculate scroll bar width
* @param container Container used to calculate scrollbar width
* @default container: document.body
*/
export function getScrollbarWidth(container: HTMLElement = document.body) {
if (container === document.body) {
return window.innerWidth - document.documentElement.clientWidth;
}
return container.offsetWidth - container.clientWidth;
}