From 44f9c8a6f06d36d38c460a60aae9f9439affc205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BFCai?= Date: Mon, 14 Oct 2024 17:15:30 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=A0=B9=E6=8D=AE=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E5=AE=B9=E5=99=A8dom=E8=BF=9B=E8=A1=8C=E8=8E=B7?= =?UTF-8?q?=E5=8F=96scroll=20=E7=9A=84=E5=AE=BD=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/utils/getScrollbarWidth.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/js/utils/getScrollbarWidth.ts b/js/utils/getScrollbarWidth.ts index ede1cbd347..933d4ce93b 100644 --- a/js/utils/getScrollbarWidth.ts +++ b/js/utils/getScrollbarWidth.ts @@ -35,7 +35,14 @@ export function getScrollbarWidthWithCSS() { return scrollbarWidth; } -// 获取 body 下滚动条宽度 -export function getScrollbarWidth() { - return window.innerWidth - document.documentElement.clientWidth; +/** + * @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; }