给公司做网站多少钱,wordpress 查看访客,开发公司空置房物管费归口什么费用,手机商城源码【问题】表格横向太长#xff0c;表格横向滚动条位于最底部#xff0c;需将页面滚动至最底部才可左右拖动表格#xff0c;用户体验感不好 【需求】基于elment的el-table组件生成的表格#xff0c;使其可以横向拖拽滚动 【实现】灵感来源于这篇文章【Vue】表格可拖拽滚动表格横向滚动条位于最底部需将页面滚动至最底部才可左右拖动表格用户体验感不好 【需求】基于elment的el-table组件生成的表格使其可以横向拖拽滚动 【实现】灵感来源于这篇文章【Vue】表格可拖拽滚动作者已给出原理感兴趣的可以去了解 找到el-table元素对应的目标元素VUE3Element-Plus 首先给el-table添加ref并获取元素 templateel-table :datatableData reftableRef/el-table
/template
script langts setup
const tableRef ref(null);
console.log(tableRef);
/script我们要找的是包裹table的第一父元素 .el-scrollbar__wrap类名盒子就是我们要找的目标元素 这里解释一下为什么.el-scrollbar__wrap是父盒子 我们可以看到.el-scrollbar__view盒子里包裹的是table标签那么.el-scrollbar__view盒子的宽度就是整个表格的宽度.el-scrollbar__wrap盒子的宽度又是其父元素的宽度那么 .el-scrollbar__wrap就是.el-scrollbar__view的父盒子.el-scrollbar__view子盒子宽度有溢出.el-scrollbar__wrap父盒子则出现滚动条使得table可以横向滚动 因此我们要找的目标元素就是 .el-scrollbar__wrap templateel-table :datatableData reftableRef/el-table
/template
script langts setup
const tableRef ref(null);
//获取目标元素
console.log(tableRef.value.$refs.scrollBarRef.wrapRef);
/script
实现拖拽横向滚动 通过改变目标元素的scrollLeft值 需要捕获鼠标事件及拖拽事件并更改目标元素的scrollLeft值则可实现横向滚动 涉及到的事件包括mousedownmouseupmousemove, mouseleave, dragstart给表格元素添加相应的事件 mousedown 鼠标按下事件即鼠标按下但未释放的动作 1.可拖拽状态改为允许拖拽。2.记录鼠标位置。3.鼠标样式改为小手。 tableDataRef.value.$el.addEventListener(mousedown, (e: MouseEvent) {// 拿到目标元素tableBody tableDataRef.value.$refs.scrollBarRef.wrapRef;// 拖拽状态改为允许拖拽/鼠标样式修改公共方法setMouseFlag(true, tableBody);// 记录鼠标按下位置mouseStart e.clientX;// 记录元素当前scrollLeft值startX tableBody.scrollLeft;// 添加 dragstart 事件监听器document.addEventListener(dragstart, handleDragStart);
}); mousemove 鼠标移动事件即鼠标在元素内移动的动作 1.判断是否可拖拽。2.允许拖拽时记录鼠标移动距离。3.修改目标元素scrollLeft值。 tableDataRef.value.$el.addEventListener(mousemove, (e: MouseEvent) {if (mouseFlag) {let offset e.clientX - mouseStart;(tableBody as HTMLElement).scrollLeft startX - offset;}
}); mouseup 鼠标释放事件即鼠标按下后释放的动作 1.可拖拽状态改为禁止拖拽。2.鼠标样式恢复。 tableDataRef.value.$el.addEventListener(mouseup, () {// console.log(鼠标左键松开);setMouseFlag(false, tableBody);// 移除 dragstart 事件监听器document.removeEventListener(dragstart, handleDragStart);
}); 检测到禁止光标手势 监听拖动时判断是否出现禁止小手标识 如果你选中文字然后点击拖拽时在浏览器中鼠标光标就会变成红色禁止符号 const handleDragStart (e: DragEvent) {// console.log(禁止光标手势出现);// 取消默认的拖动效果e.preventDefault();setMouseFlag(false, tableBody);
}; 拖拽状态改为允许拖拽/鼠标样式修改公共方法 const setMouseFlag (flag: boolean, tableBody?: HTMLElement | null) {mouseFlag flag;if (tableBody) {tableBody.style.cursor flag ? grab : auto;}
}; 封装成统一函数dragTable并将其定义为全局变量 /src/utils/common.ts //设置el-table可进行鼠标左键按下左右拖动
interface TableDataRef {value: {$el: HTMLElement;$refs: {scrollBarRef: {wrapRef: HTMLElement;};};};
}export const dragTable (tableDataRef: TableDataRef) {let mouseFlag false,mouseStart 0,startX 0,tableBody: HTMLElement | null null;//鼠标按下事件即鼠标按下但未释放的动作。tableDataRef.value.$el.addEventListener(mousedown, (e: MouseEvent) {tableBody tableDataRef.value.$refs.scrollBarRef.wrapRef;setMouseFlag(true, tableBody);// mouseFlag true;mouseStart e.clientX;startX tableBody.scrollLeft;// tableBody.style.cursor grab;// 添加 dragstart 事件监听器document.addEventListener(dragstart, handleDragStart);});//鼠标释放事件即鼠标按下后释放的动作。tableDataRef.value.$el.addEventListener(mouseup, () {// console.log(鼠标左键松开);
/* mouseFlag false;(tableBody as HTMLElement).style.cursor auto; */setMouseFlag(false, tableBody);// 移除 dragstart 事件监听器document.removeEventListener(dragstart, handleDragStart);});//鼠标移动事件即鼠标在元素内移动的动作。tableDataRef.value.$el.addEventListener(mousemove, (e: MouseEvent) {if (mouseFlag) {let offset e.clientX - mouseStart;(tableBody as HTMLElement).scrollLeft startX - offset;}});//鼠标离开事件即鼠标移动到元素外触发这个事件。tableDataRef.value.$el.addEventListener(mouseleave, (e: MouseEvent) {handleDragStart((e as any))});// 检测到禁止光标手势const handleDragStart (e: DragEvent) {// console.log(禁止光标手势出现);// 取消默认的拖动效果e.preventDefault();setMouseFlag(false, tableBody);};const setMouseFlag (flag: boolean, tableBody?: HTMLElement | null) {mouseFlag flag;if (tableBody) {tableBody.style.cursor flag ? grab : auto;}};
}; main.ts import { dragTable } from /utils/common;
const app createApp(App);
// 全局方法挂载
app.config.globalProperties.$dragTable dragTable;使用全局变量 templateel-table :datatableData reftableRef/el-table
/template
script langts setup
import { ComponentInternalInstance, ref } from vue;
const { proxy } getCurrentInstance() as ComponentInternalInstance;
const tableRef ref(null);
nextTick(() {proxy?.$dragTable(tableRef)
})
/script
补充VUE2 Element 可参考这篇文章利用 bodyWrapper 实现表格拖动 el-table 中获取 bodyWrapper 的方法 注意可能存在Element版本差异建议下面的获取方式都试试 之前获取 bodyWrapper 方法是 this.$refs.myTable.bodyWrapper 现在上面这种方法获取不到了要更改为 this.$refs.myTable.$elTable.bodyWrapper
或
this.$refs.myTable.$refs.table.bodyWrapper
console.log(this.$refs.mytable) 实现效果 我用的的录制gif软件鼠标没有变成抓取手势实际是可以的 如果没有选中文字的需求可以在mousemove监听中的 if 判断里加上 e.preventDefault();这样拖动时就会很流畅不受到选中文字影响。 总结 元素可滚动的前提条件是元素的宽度或高度超出给定区域且开启了滚动条 拖拽需结合mousedownmousemovemouseupmouseleavedragstart事件实现 竖向滚动同理修改ScrollTop值即可
文章转载自: http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.wbns.cn.gov.cn.wbns.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn