计算浏览器滚动条在各浏览器的宽度

轩陌

分类: 解决问题 3849 3

博客有段时间没有更新了,也比较忙,今天遇见一个需要计算浏览器滚动条的功能,之前在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;
  • 15人 Love
  • 1人 Haha
  • 1人 Wow
  • 5人 Sad
  • 0人 Angry
计算浏览器滚动条宽度

作者简介: 轩陌

打赏

生命的意义在于折腾,一直努力成长中,期待梦想实现的那刻。

共 3 条评论关于 “计算浏览器滚动条在各浏览器的宽度”

Loading...