日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript——获取浏览器滚动条(ScrollBar)宽度

發(fā)布時間:2024/10/5 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript——获取浏览器滚动条(ScrollBar)宽度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題描述

不同瀏覽器的滾動條寬度不相同,需要動態(tài)獲取瀏覽器滾動條寬度。

問題分析?

screen.width屏幕分辨率寬度

document.body.scrollWidth?頁面完整寬度

document.body.scrollHeight?頁面完整寬度

document.body.clientWidth?width+左右padding

document.body.clientHeight?height + 上下padding?

document.body.offsetWidth?網(wǎng)頁可見區(qū)域?qū)挾?/p>

document.body.offsetHeight?網(wǎng)頁可見區(qū)域高度

body中插入一個div,div中再嵌套一個div,設(shè)置外部的div的overflower為scroll,

可以出現(xiàn)滾動條軌道,然后使用外部div寬度值減去內(nèi)部div的寬度值即可?。

得到滾動條寬度之后,把添加的元素刪掉。

解決方案

方法一:簡單方法

var cWidth = document.body.clientWidth || document.documentElement.clientWidth;//頁面可視區(qū)域?qū)挾?var iWidth = window.innerWidth;//瀏覽器窗口大小 console.log(iWidth - cWidth);//打印滾動條寬度

方法二:iView的解決方案?

https://github.com/iview/iview/blob/2.0/src/utils/assist.js

// For Modal scrollBar hidden let cached; export function getScrollBarSize (fresh) {if (isServer) return 0;if (fresh || cached === undefined) {const inner = document.createElement('div');inner.style.width = '100%';inner.style.height = '200px';const outer = document.createElement('div');const outerStyle = outer.style;outerStyle.position = 'absolute';outerStyle.top = 0;outerStyle.left = 0;outerStyle.pointerEvents = 'none';outerStyle.visibility = 'hidden';outerStyle.width = '200px';outerStyle.height = '150px';outerStyle.overflow = 'hidden';outer.appendChild(inner);document.body.appendChild(outer);const widthContained = inner.offsetWidth;outer.style.overflow = 'scroll';let widthScroll = inner.offsetWidth;if (widthContained === widthScroll) {widthScroll = outer.clientWidth;}document.body.removeChild(outer);cached = widthContained - widthScroll;}return cached; }

方法三:Element UI的解決方案

https://github.com/ElemeFE/element/blob/dev/src/utils/scrollbar-width.js

import Vue from 'vue';let scrollBarWidth;export default function() {if (Vue.prototype.$isServer) return 0;if (scrollBarWidth !== undefined) return scrollBarWidth;const outer = document.createElement('div');outer.className = 'el-scrollbar__wrap';outer.style.visibility = 'hidden';outer.style.width = '100px';outer.style.position = 'absolute';outer.style.top = '-9999px';document.body.appendChild(outer);const widthNoScroll = outer.offsetWidth;outer.style.overflow = 'scroll';const inner = document.createElement('div');inner.style.width = '100%';outer.appendChild(inner);const widthWithScroll = inner.offsetWidth;outer.parentNode.removeChild(outer);scrollBarWidth = widthNoScroll - widthWithScroll;return scrollBarWidth; };

參考文章

http://www.caotama.com/53793.html

https://segmentfault.com/q/1010000004817695

https://my.oschina.net/wangch5453/blog/2967396

https://blog.csdn.net/qq_42089654/article/details/80515916

https://www.cnblogs.com/MakethingsEasy/archive/2011/12/08/2280661.html

總結(jié)

以上是生活随笔為你收集整理的JavaScript——获取浏览器滚动条(ScrollBar)宽度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。