共计 541 个字符,预计需要花费 2 分钟才能阅读完成。
博客有段时间没有更新了,也比较忙,今天遇见一个需要计算浏览器滚动条的功能,之前在 element-ui 见过一种写法,这里做一下笔记。
原理是通过两个父子关系的 div 的宽度相减得到一个差值,这个值就是浏览器滚动条的宽度,outerWidth - innerWidth = scrollBarWidth
。
const scrollContainer = document.createElement('div');
const scrollContent = document.createElement('div');
scrollContainer.style = 'position:fixed;z-index:-1;width:50px;height:50px;overflow:scroll;';
scrollContent.style = 'height:100px;';
scrollContainer.append(scrollContent);
document.body.append(scrollContainer);
const scrollBarWidth = scrollContainer.offsetWidth - scrollContent.offsetWidth;
文章来源: 计算浏览器滚动条在各浏览器的宽度
正文完