Element-UI / scrollbar-width.js

获取浏览器滚动条宽度,一般用于弹出层的时候,设置body的右边距,防止overflow: hidden的时候元素抖动。

scrollbar-width.js

import Vue from 'vue';

let scrollBarWidth;

export default function() {
  // 如果是服务器端渲染,则浏览器滚动条的宽度为0
  if (Vue.prototype.$isServer) return 0;
  if (scrollBarWidth !== undefined) return scrollBarWidth;

  const outer = document.createElement('div');
  outer.className = 'el-scrollbar__wrap';
  // 强制出现滚动条
  // .el-scrollbar__wrap {
  //   overflow: scroll;
  //   height: 100%
  // }
  outer.style.visibility = 'hidden';
  outer.style.width = '100px';
  outer.style.position = 'absolute';
  outer.style.top = '-9999px';
  document.body.appendChild(outer);
  // https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/offsetWidth
  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;
};

简单使用

let scrollBarWidth;

export default function() {
  const outer = document.createElement('div');
  outer.style.overflow = 'scroll';
  outer.style.visibility = 'hidden';
  outer.style.width = '100px';
  outer.style.height = '100%';
  outer.style.position = 'absolute';
  outer.style.top = '-9999px';
  document.body.appendChild(outer);
  // https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/offsetWidth
  const widthNoScroll = outer.offsetWidth;

  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;
};

弹窗弹出时如果需要锁屏(锁住弹窗下的页面不滚动),则加上下面这段代码,弹窗弹出时锁屏,弹窗关闭时再将其去除。

// 如果需要锁屏(锁住弹窗下的页面不滚动)
if (props.lockScroll) {
  // body 上面是否添加 'el-popup-parent--hidden'
  // .el-popup-parent--hidden {
  //     overflow: hidden
  // }
  this.withoutHiddenClass = !hasClass(document.body, 'el-popup-parent--hidden');
  if (this.withoutHiddenClass) {
    // 获取 body 的 paddingRight
    this.bodyPaddingRight = document.body.style.paddingRight;
    this.computedBodyPaddingRight = parseInt(getStyle(document.body, 'paddingRight'), 10);
  }
  // 获取滚动条宽
  scrollBarWidth = getScrollBarWidth();
  // body 是否出现滚动条
  let bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  // body 的 overflowY 设置值
  let bodyOverflowY = getStyle(document.body, 'overflowY');
  if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === 'scroll') && this.withoutHiddenClass) {
    // body 的 paddingRight 加上 滚动条宽度来实现防止页面抖动
    document.body.style.paddingRight = this.computedBodyPaddingRight + scrollBarWidth + 'px';
  }
  // body 添加 overflow: hidden
  addClass(document.body, 'el-popup-parent--hidden');
}

弹窗关闭时将弹窗打开时设置的值去除。

if (tthis.withoutHiddenClass) {
  document.body.style.paddingRight = this.bodyPaddingRight;
  removeClass(document.body, 'el-popup-parent--hidden');
}
this.withoutHiddenClass = true;

文中的一些方法可以查看Element-UI / dom.js 的学习

更多内容可查看我的个人博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 element-ui 表格的无限滚动,你可以使用 element-uiel-table 组件结合 el-scrollbar 组件来实现。下面是一个简单的示例代码: ```vue <template> <div> <el-scrollbar style="height: 400px;"> <el-table :data="tableData" style="width: 100%" :row-key="row => row.id" @row-dblclick="handleRowDblclick" :row-class-name="handleRowClassName" > <!-- 列定义 --> <el-table-column prop="id" label="ID" width="80"></el-table-column> <el-table-column prop="name" label="姓名" width="120"></el-table-column> <el-table-column prop="age" label="年龄" width="80"></el-table-column> <!-- 其他列... --> </el-table> </el-scrollbar> </div> </template> <script> export default { data() { return { tableData: [], // 表格数据 pageIndex: 1, // 当前页码 pageSize: 20, // 每页显示的数量 total: 0, // 总记录数 }; }, mounted() { // 初始化加载数据 this.loadData(); }, methods: { loadData() { // 模拟异步请求数据 setTimeout(() => { // TODO: 发起接口请求获取表格数据,根据 pageIndex 和 pageSize 参数获取对应的数据 // 示例代码:假设从后端返回的数据格式为 { list: [], total: 100 } const response = { list: [], total: 100 }; this.tableData = response.list; this.total = response.total; }, 500); }, handleRowDblclick(row) { // 双击行事件处理 console.log('双击行', row); }, handleRowClassName(row) { // 行样式处理 // 示例:偶数行添加样式 'even-row',奇数行添加样式 'odd-row' return row.id % 2 === 0 ? 'even-row' : 'odd-row'; }, }, }; </script> <style> .even-row { background-color: #f5f5f5; } .odd-row { background-color: #fff; } </style> ``` 在这个示例中,el-table 组件包裹在 el-scrollbar 组件中,通过设置 el-scrollbar 的高度来限制表格的高度,实现滚动效果。同时,使用 @row-dblclick 监听双击行事件,并使用 :row-class-name 设置行的样式。 注意,你还需要根据具体的业务需求,在 loadData 方法中发送请求获取数据,并根据 pageIndex 和 pageSize 参数来获取对应的数据。这里的 loadData 方法仅作为示例,你需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值