如何固定滚动条在视窗底部?

  在使用elementui 表格组件时,会遇到一个问题,表格过大而无法显示完全,要把横向内容看完整就需要滚动到列表底部拉动横向滚动条,十分不方便。

  因此,我想自定义滚动条显示在视窗底部:

  <!--自定义滚动条-->
    <div class="out-box" ref="out-box">
      <img style="margin-left:-20px;position:fixed;bottom:0px;" src="http://img.s.youfenba.com/material_thumb/74NGnHkeQs.png">
      <div class="in_box" @mousedown="mouseDown" @mouseup="mouseUp" @mouseout="mouseUp"  :style="old_new" ref="in_box" > </div>
      <!--用ref对里边的灰色滚动条进行绑定,写上对应的事件-->
      <img style="position:fixed;bottom:0px;right:40px;" src="http://img.s.youfenba.com/material_thumb/WTzsMr6RSX.png">
    </div>
data(){
    return{
     //自定义滚动条数据设置
      newx: "",    //第一次的坐标
      oldx: "",     //第二次的坐标
      outBoxWidth: '',    //滚动条长度
      moveWidth: "",      //可移动的长度
      old_new:{
        //滚动条的偏移量
        left: 0
      },
      windowWidth: document.body.clientWidth -280,     //表格宽度(网页可见宽度-左侧菜单栏宽度)
      f:0,
      leftstr: '',
      //滚动条参数
    }
}
 //监听视窗大小
  mounted() {
    const that = this;
    window.onresize = function() {
      //监听浏览器窗口
      that.ifmove();
    };
    this.ifmove();       //页面进来先判断
  },
//滚动条事件
    ifmove(){
      let that = this;
      that.windowWidth = document.body.clientWidth - 1000;
      if (that.windowWidth < 1920) {
        that.$refs.out_box.style.display = "block";
        that.$refs.in_box.style.width =
                (that.windowWidth / 1920) * that.windowWidth + "px"; //显示区域占百分比,在滚动条显示
        that.moveWidth = (1 - that.windowWidth / 1920) * that.windowWidth; //可移动宽度
      } else {
        that.$refs.out_box.style.display = "none";
      }
    },

    //滚动条事件
    mouseDown(event) {
      let e = event || window.event;
      let _self = this;
      this.f = 1;
      this.newx = e.clientX;
      this.leftstr = this.old_new.left
      this.$refs.in_box.addEventListener("mousemove", function(event) {
        let e = event || window.event;
        _self.oldx = e.clientX;
        _self.moveWidth = (1 - _self.windowWidth / 1920) * _self.windowWidth-parseFloat(_self.leftstr); //模拟滚动条可移动长度
        if (_self.f) {
          _self.old_new.left =
                  _self.oldx - _self.newx <= -parseFloat(_self.leftstr)
                          ? "0px"
                          : _self.oldx - _self.newx >= _self.moveWidth
                          ? _self.moveWidth + parseFloat(_self.leftstr) + "px"
                          : _self.oldx - _self.newx + parseFloat(_self.leftstr) + "px"; //模拟滚动条偏移量
          _self.$refs.scroll.scrollLeft =
                  _self.oldx - _self.newx <= -parseFloat(_self.leftstr)
                          ? 0
                          : _self.oldx - _self.newx >= _self.moveWidth
                          ?(_self.moveWidth + parseFloat(_self.leftstr))* (1920 / _self.windowWidth)
                          : (_self.oldx - _self.newx + parseFloat(_self.leftstr) ) * (1920 / _self.windowWidth); //实际滚动条偏移量
        }
      });
    },
    mouseUp() {
      this.f = 0;
    },

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现表格横向滚动条固定底部,可以使用以下步骤: 1. 设置表格容器的高度,以便在表格内容过多时会出现纵向滚动条。 2. 设置表格的 scroll 属性,包括 x 和 y 两个参数,其中 x 用于控制横向滚动条的出现,y 用于控制纵向滚动条的出现。 3. 设置表格底部固定行,使用 Table 组件的 footer 属性,将 footer 设为一个函数,返回一个固定行的 JSX 元素。 4. 在固定行的 JSX 元素中,设置一个空白的占位符元素,高度与表格容器的高度相同,以便在表格内容不足时,固定行也能占据整个表格底部。同时,设置一个空白的 div 元素,宽度与横向滚动条的宽度相同,以便在表格内容过多时,横向滚动条能够正常出现。 下面是一个示例代码: ``` import { Table } from 'antd'; const dataSource = [ // 数据源 ]; const columns = [ // 表头列配置 ]; const fixedRowHeight = 50; // 固定行的高度 const scrollHeight = 400; // 表格容器的高度 const scrollWidth = 1200; // 表格内容的宽度,需要根据实际情况进行调整 function renderFooter() { return ( <tr> <td colSpan={columns.length}> <div style={{ height: `${scrollHeight - fixedRowHeight}px` }} /> <div style={{ width: `${scrollWidth}px`, height: `${fixedRowHeight}px` }} /> </td> </tr> ); } function App() { return ( <Table dataSource={dataSource} columns={columns} scroll={{ x: scrollWidth, y: scrollHeight }} footer={renderFooter} /> ); } ``` 在上面的代码中,我们先设置了固定行的高度和表格容器的高度,然后在 renderFooter 函数中,返回一个包含空白占位符和横向滚动条占位元素的 JSX 元素。最后,在 Table 组件中,将 scroll 属性设置为包含横向滚动条宽度和表格容器高度的对象,将 footer 属性设置为 renderFooter 函数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值