使el-table通过操控鼠标滚轮横向滚动

1.创建directive文件夹,里面创建directive.js文件

复制代码
import Vue from 'vue';

Vue.directive('scroll-x',{
  inserted:function(el){
    let domClass = el.getAttribute('class')
    if(domClass.indexOf('el-table')<0){
      return false
    }
    const scrollDiv = el;
    if(scrollDiv==null){
      return false
    }
    scrollDiv.addEventListener('mousewheel', handler, false)
    const that = this
    function handler(event) {
      const detail = event.wheelDelta || event.detail;
      const moveForwardStep = 1;
      const moveBackStep = -1;
      let step = 0;
      if (detail < 0) {
        step = moveForwardStep * 100;
      } else {
        step = moveBackStep * 100;
      }
      let d = scrollDiv.querySelector('.el-table__body-wrapper')
      d.scrollLeft += step
    }
  }
})

2.在main.js中全局引入

import './directive/directives'
3.在vue页面中使用 v-scroll-x

<el-table

v-scroll-x

> </el-table>

相关推荐
Ashley_Amanda几秒前
SAP调用Web Service全流程详解
java·前端·数据库
Dreamy smile2 分钟前
css :nth-child() 完全用法指南
前端·css
Southern Wind4 分钟前
从零开始封装一个优雅的图片上传组件 - 二次改装 Layui-Upload 的教程(附完整封装代码)
前端·javascript·html·layui·css3
小白菜学前端5 分钟前
Vue3 + TS 解决 ESLint 与 Prettier 格式化冲突
前端·javascript·vue.js
第二只羽毛11 分钟前
搜索引擎项目
大数据·前端·c++·搜索引擎·vim
The_era_achievs_hero14 分钟前
封装api方法(全面)
前端·javascript·uni-app·api·封装接口
一殊酒17 分钟前
【前端开发】Vue项目多客户配置自动化方案【二】
javascript·vue.js·自动化
Mr Xu_19 分钟前
深入解析 getBoundingClientRect 与 offsetTop:解决 Vue 平滑滚动偏移误差问题
前端·javascript·vue.js
Mr-Wanter19 分钟前
vue 解决img图片路径存在但图片无法访问时显示错误的问题
前端·vue·img