javascript - 各浏览器滚动条默认宽度是多少?
巴扎黑
巴扎黑 2017-04-11 09:45:54
[JavaScript讨论组]

现在各大浏览器滚动条的默认宽度是多少呢?有没有什么接口可以快速获取其宽度呢?

巴扎黑
巴扎黑

全部回复(8)
高洛峰

我记得也是17px

window.innerWidth - document.body.clientWidth //火狐下17px IE11下17px

chrome的滚动条 我给关了 所以没测试
window.innerWidth 浏览器可用宽度
document.body.clientWidth body的宽度
我这个式子是在body没有marginpaddingborder的情况下得到的
如果你要是有的话可以做减法

//margin
document.body.style.marginLeft 
document.body.style.marginRight
//padding
document.body.style.paddingLeft
document.body.style.paddingRight
//border
document.body.style.borderLeft
document.body.style.borderRight

另外科普一下:
screen.width屏幕分辨率宽度
document.body.scrollWidth 页面完整宽度
document.body.scrollHeight 页面完整宽度
document.body.offsetWidth 网页课件区域宽度
document.body.offsetHeight 网页课件区域高度

怪我咯

用兩層 p
外層先設定 overflow: scroll,量取內層 p 寬度
再將外層 p 設定 overflow: hidden,再量內層 p 寬度

兩個寬度差就是 scrollbar 的寬度

https://jsfiddle.net/fdamw8sb/


或是直接用 offsetWidthclientWidth 的差值也能算出來
ref: https://davidwalsh.name/detect-scrollbar-width

// Create the measurement node
var scrollp = document.createElement("p");
scrollp.style.overflow = 'scroll';
document.body.appendChild(scrollp);

// Get the scrollbar width
var scrollbarWidth = scrollp.offsetWidth - scrollp.clientWidth;
console.warn(scrollbarWidth); // Mac:  15

// Delete the p 
document.body.removeChild(scrollp);

PHP中文网

默认滚动条不记在body中

迷茫

在20px左右

天蓬老师

我记得是17px

怪我咯

得自己量量,有时候更新一次版本换一次

高洛峰
var cWidth = document.body.clientWidth || document.documentElement.clientWidth;//页面可视区域宽度
var iWidth = window.innerWidth;//浏览器窗口大小
console.log(iWidth - cWidth);//打印滚动条宽度
伊谢尔伦

在body无border-width的情况下
window.innerWidth - document.body.clientWidth

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号