MCPcopy Create free account
hub / github.com/XIU2/UserScript / getScrollBarWidth

Function getScrollBarWidth

RightClick_ScrollBar_BackToTop.user.js:40–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38
39 // 获取网页右侧滚动条宽度
40 function getScrollBarWidth() {
41 // 创建一个临时的 div 元素
42 const div = document.createElement('div');
43 div.style.overflow = 'scroll'; // 强制出现滚动条
44 div.style.visibility = 'hidden'; // 不要显示出来
45 //div.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps?
46 div.style.width = '100px'; // 设置宽度
47 div.style.height = '100px'; // 设置高度
48 document.body.appendChild(div); // 将 div 添加到文档
49
50 // 创建一个内嵌的 div
51 const innerDiv = document.createElement('div');
52 innerDiv.style.width = '100%'; // 设置宽度为 100%
53 innerDiv.style.height = '100%'; // 设置高度为 100%
54 div.appendChild(innerDiv); // 将内嵌 div 添加到外部 div
55
56 // 计算滚动条宽度
57 const scrollBarWidth = div.offsetWidth - innerDiv.offsetWidth;
58
59 // 移除临时 div
60 div.parentNode.removeChild(div);
61
62 return scrollBarWidth; // 返回滚动条宽度
63 }
64})();

Calls

no outgoing calls

Tested by

no test coverage detected