import Vue from 'vue';
// 全局添加table左右拖动效果的指令
Vue.directive('tableDrag', {
inserted: function (el) {
el = el.getElementsByClassName('el-table__body-wrapper')[0];
el.style.cursor = 'grab';
el.onmousedown = function () {
let gapX = event.clientX;
let startX = el.scrollLeft;
document.onmousemove = function (e) {
console.log(e.target.nodeName);
if (e.target.tagName == 'TD') {
let x = e.clientX - gapX;
el.scrollLeft = startX - x;
return false;
}
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
};
};
}
});
<el-table :data="tableData" ref="tableRef" style="width: 100%" v-tableDrag>