Video_2024-04-23_161425
Vue3
<template>
<el-table ref="myTable" height="250" @mouseover.native="clearScroll" @mouseleave.native="createScroll"
:data="tableData">
<el-table-column prop="Id" label="编号" :sortable="'custom'" />
<el-table-column prop="Name" label="姓名" :sortable="'custom'" />
<el-table-column prop="Gender" label="性别" :sortable="'custom'" />
<el-table-column prop="IdCard" label="身份证号" :sortable="'custom'" />
<el-table-column prop="Address" label="籍贯" :sortable="'custom'" />
</el-table>
</template>
<script setup lang="js">
import { ref, onMounted, onUnmounted } from 'vue'
let tableData = ref([
{
"Name": "西门纽汪",
"Gender": "女",
"IdCard": "150622199311241915",
"Address": "内蒙古自治区-鄂尔多斯市-准格尔旗",
"Id": 1,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "冉眯流",
"Gender": "男",
"IdCard": "620122201301221619",
"Address": "甘肃省-兰州市-皋兰县",
"Id": 2,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "海喉拆屋",
"Gender": "女",
"IdCard": "522725201309211567",
"Address": "贵州省-黔南布依族苗族自治州-瓮安县",
"Id": 3,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "龚配旬",
"Gender": "女",
"IdCard": "610826201408131590",
"Address": "陕西省-榆林市-绥德县",
"Id": 4,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "巫马控絮",
"Gender": "女",
"IdCard": "530326200001031936",
"Address": "云南省-曲靖市-会泽县",
"Id": 5,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "常刃椒",
"Gender": "女",
"IdCard": "411671199505301393",
"Address": "河南省-周口市-河南周口经济开发区",
"Id": 16,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "羊舌性虫",
"Gender": "男",
"IdCard": "350213199004221307",
"Address": "福建省-厦门市-翔安区",
"Id": 17,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "太叔唉咳",
"Gender": "男",
"IdCard": "140222200504211555",
"Address": "山西省-大同市-天镇县",
"Id": 19,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "汲虚刚",
"Gender": "男",
"IdCard": "620981201601161438",
"Address": "甘肃省-酒泉市-玉门市",
"Id": 20,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "澹台另疤",
"Gender": "女",
"IdCard": "440981200906011864",
"Address": "广东省-茂名市-高州市",
"Id": 21,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
}
])
let timer = null
let myTable = ref(null)
const clearScroll = () => {
clearInterval(timer)
timer = null
}
const createScroll = () => {
clearScroll()
// 拿到 table
const table = myTable.value.layout.table.refs
// 拿到可以滚动的元素
const tableWrapper = table.bodyWrapper.firstElementChild.firstElementChild
timer = setInterval(() => {
tableWrapper.scrollTop += 1
// 判断是否滚动到底部,如果到底部了置为0(可视高度+距离顶部=整个高度)
if (tableWrapper.clientHeight + tableWrapper.scrollTop == tableWrapper.scrollHeight) {
tableWrapper.scrollTop = 0
}
}, 100)
}
onMounted(() => {
console.log(myTable.value)
createScroll()
})
onUnmounted(() => {
clearScroll()
})
</script>
vue2
<template>
<div>
<div class="app-container">
<el-table v-loading="loading" :data="tableData" :max-height="250" ref="scroll_Table"
@mouseenter.native="autoScroll(true)" @mouseleave.native="autoScroll(false)">
<el-table-column prop="Id" label="编号" :sortable="'custom'" />
<el-table-column prop="Name" label="姓名" :sortable="'custom'" />
<el-table-column prop="Gender" label="性别" :sortable="'custom'" />
<el-table-column prop="IdCard" label="身份证号" :sortable="'custom'" />
<el-table-column prop="Address" label="籍贯" :sortable="'custom'" />
</el-table>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loading: false,
tableData: [
{
"Name": "西门纽汪",
"Gender": "女",
"IdCard": "150622199311241915",
"Address": "内蒙古自治区-鄂尔多斯市-准格尔旗",
"Id": 1,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "冉眯流",
"Gender": "男",
"IdCard": "620122201301221619",
"Address": "甘肃省-兰州市-皋兰县",
"Id": 2,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "海喉拆屋",
"Gender": "女",
"IdCard": "522725201309211567",
"Address": "贵州省-黔南布依族苗族自治州-瓮安县",
"Id": 3,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "龚配旬",
"Gender": "女",
"IdCard": "610826201408131590",
"Address": "陕西省-榆林市-绥德县",
"Id": 4,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "巫马控絮",
"Gender": "女",
"IdCard": "530326200001031936",
"Address": "云南省-曲靖市-会泽县",
"Id": 5,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "常刃椒",
"Gender": "女",
"IdCard": "411671199505301393",
"Address": "河南省-周口市-河南周口经济开发区",
"Id": 16,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "羊舌性虫",
"Gender": "男",
"IdCard": "350213199004221307",
"Address": "福建省-厦门市-翔安区",
"Id": 17,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "太叔唉咳",
"Gender": "男",
"IdCard": "140222200504211555",
"Address": "山西省-大同市-天镇县",
"Id": 19,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "汲虚刚",
"Gender": "男",
"IdCard": "620981201601161438",
"Address": "甘肃省-酒泉市-玉门市",
"Id": 20,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
},
{
"Name": "澹台另疤",
"Gender": "女",
"IdCard": "440981200906011864",
"Address": "广东省-茂名市-高州市",
"Id": 21,
"CreateUser": null,
"CreateTime": null,
"IsDeleted": null,
"UpdateTime": null,
"UpdateUser": null
}
],
scrolltimer: '', // 自动滚动的定时任务
}
},
mounted() {
console.log(this.$refs.scroll_Table)
this.autoScroll()
},
beforeDestroy() {
this.autoScroll(true)
},
methods: {
// 设置自动滚动
autoScroll(stop) {
const table = this.$refs.scroll_Table.layout.table.refs
// 拿到表格中承载数据的div元素
const divData = table.bodyWrapper.firstElementChild.firstElementChild
// 拿到元素后,对元素进行定时增加距离顶部距离,实现滚动效果(此配置为每100毫秒移动1像素)
if (stop) {
//再通过事件监听,监听到 组件销毁 后,再执行关闭计时器。
window.clearInterval(this.scrolltimer)
} else {
this.scrolltimer = window.setInterval(() => {
// 元素自增距离顶部1像素
divData.scrollTop += 1
// 判断元素是否滚动到底部(可视高度+距离顶部=整个高度)
if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
// 重置table距离顶部距离
divData.scrollTop = 0
// 重置table距离顶部距离。值=(滚动到底部时,距离顶部的大小) - 整个高度/2
// divData.scrollTop = divData.scrollTop - divData.scrollHeight / 2
}
}, 150) // 滚动速度
}
},
}
};
</script>