| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
[复位日记]
网站统计
·今日访问 : 22
·页面点击 : 26
·当前在线 : 8
2026年3月2日 星期一[阅读 57]
获取滚动条宽度的方法
function getScrollbarWidth() {
// 创建一个临时div来测量
const outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.overflow = 'scroll'; // 强制显示滚动条
outer.style.width = '100px';
document.body.appendChild(outer);
const inner = document.createElement('div');
inner.style.width = '100%';
outer.appendChild(inner);
// 计算滚动条宽度
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
// 清理
outer.parentNode.removeChild(outer);
return scrollbarWidth;
}
console.log('真实滚动条宽度:', getScrollbarWidth());
发表评论:

