使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>

相关推荐
槁***耿2 分钟前
前端路由守卫
前端
百***35484 分钟前
前端视频处理开发
前端·音视频
顾安r17 分钟前
11.29 脚本游戏 单页面格斗游戏模板
前端·javascript·css·游戏·virtualenv
g***557518 分钟前
Redis 通用命令
前端·redis·bootstrap
爱睡觉的雨22 分钟前
跨域问题(前端)
前端
我叫张小白。22 分钟前
Vue3 路由:单页面应用的核心引擎
前端·javascript·vue.js·前端框架·vue3
y***866926 分钟前
前端PWA应用特性使用指南
前端
天空陪伴星辰a29 分钟前
表单校验问题
前端·javascript·表单校验
鱼鱼块1 小时前
《最小栈的巧妙设计:用辅助栈实现 O(1) 获取最小值》
javascript·算法·面试
San301 小时前
反转字符串与两数之和:两道简单题背后的 JavaScript 思维深度
javascript·算法·面试