Vue+element table+原生js +touch触摸事件实现移动端横向拖动文章来源:https://www.toymoban.com/news/detail-582120.html
今天做移动端项目时,想加个表格展示数据,但是调试的时候发现,表格数据很多时,想要触屏左右滑动内容,但是滑动不了,又不想引用插件实现,只好自己写个拖拽事件了文章来源地址https://www.toymoban.com/news/detail-582120.html
<el-card
shadow="never"
v-show="tableCardTitle"
class="table-wrapper"
>
<div slot="header" class="clearfix">
<span>{{ tableCardTitle }}</span>
</div>
<el-table :data="tableData" stripe >
<el-table-column
:prop="item.key"
:label="item.name"
v-for="(item, index) in tableLabel"
:key="index"
>
</el-table-column>
</el-table>
</el-card>
updated() {
this.addrag();
}
methods: {
addDrag() {
const tableWrapperEls = document.querySelector(".el-card__body");
console.log(tableWrapperEls);
const tableEls = document.querySelector(
".el-table--scrollable-x .el-table__body-wrapper"
);
console.log(tableEls);
let isDragging = false; // 是否正在拖动
let startX = 0; // 拖动开始的X坐标
let scrollLeft = 0; // 横向滚动的距离
tableWrapperEls.addEventListener("touchstart", (e) => {
isDragging = true;
startX = e.touches[0].pageX - tableWrapperEls.offsetLeft;
scrollLeft = tableEls.scrollLeft;
});
tableWrapperEls.addEventListener("touchmove", (e) => {
if (!isDragging) return;
e.preventDefault();//阻止默认行为
const x = e.touches[0].pageX - tableWrapperEls.offsetLeft;
const walk = (x - startX) * 2; // 控制拖动速度
tableEls.scrollLeft = scrollLeft - walk;
});
tableWrapperEls.addEventListener("touchend", () => {
isDragging = false;
});
},
}
<style scoped>
.table-wrapper {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
</style>
到了这里,关于Vue+element table+原生js +touch触摸事件实现移动端横向拖动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!