1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > js获取页面滚动条宽度

js获取页面滚动条宽度

时间:2021-06-29 19:44:41

相关推荐

js获取页面滚动条宽度

let cached;function getScrollBarSize(fresh){//fresh 是否重新获取, cached: 是否获取过if(fresh || cahced===undefined) {const inner = document.createElement('div');inner.style.width = '100%';inner.style.height = '200px';const outer = document.createElement('div');outer.style.position = 'absolute';outer.style.top = 0;outer.style.left = 0;outer.style.visibility = 'hidden';outer.style.width = '200px';outer.style.height = '150px'; //父元素高度比内部元素高度小outer.style.overflow = 'hidden';outer.appendChild(inner);document.body.appendChild(outer);const widthContained = inner.offsetWidth;outer.style.overflow = 'scroll'; //让滚动条显示出来let widthScroll = inner.offsetWidth; //offsetWidth: content+padding+borderif(widthContained === widthScroll){widthScroll = outer.clientWidth; //clientWidth: content + padding}document.body.removeChild(outer);/**const widthContained = outer.offsetWidth;const widthScroll = outer.clientWidth;document.body.removeChild(outer);**/cached = widthContained - widthScroll}return cached;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。