element-ui有这么一个文件,在此做一个记录
    这个文件主要是用来计算当前打开组件的浏览器的滚动条宽度的,这样就可以原本的滚动条隐藏,然后自定义一个更好看的滚动条!!!

    1. import Vue from 'vue';
    2. let scrollBarWidth;
    3. export default function() {
    4. // 服务端
    5. if (Vue.prototype.$isServer) return 0;
    6. // 防止重复计算
    7. if (scrollBarWidth !== undefined) return scrollBarWidth;
    8. // 首先创建一个div,隐藏起来,放到视口以外
    9. const outer = document.createElement('div');
    10. outer.className = 'el-scrollbar__wrap';
    11. outer.style.visibility = 'hidden';
    12. outer.style.width = '100px';
    13. outer.style.position = 'absolute';
    14. outer.style.top = '-9999px';
    15. document.body.appendChild(outer);
    16. // 放入浏览器中后获取dom所占宽度
    17. const widthNoScroll = outer.offsetWidth;
    18. // 手动设置成滚动状态
    19. outer.style.overflow = 'scroll';
    20. // 创建一个内部的子dom
    21. const inner = document.createElement('div');
    22. inner.style.width = '100%';
    23. outer.appendChild(inner);
    24. // 放入到dom中后获取dom所占宽度
    25. const widthWithScroll = inner.offsetWidth;
    26. // 移除dom
    27. outer.parentNode.removeChild(outer);
    28. // 计算子和父dom宽度的差值,则是滚动条宽度
    29. scrollBarWidth = widthNoScroll - widthWithScroll;
    30. // 把计算好的滚动条返回
    31. return scrollBarWidth;
    32. };