ElementUI多个表格自动滚动数据(两个表格示例)
功能介绍:
实现表格动态循环滚动,当鼠标滑入暂停滚动,鼠标离开继续滚动(动图中不动的时候就是鼠标滑入)
模板部分代码:
<el-row :gutter="24">
<el-col :span="12">
<center style="font-size: 24px">待验收项目提醒</center>
<br/>
<el-table :data="tableData1" border stripe style="width: 100%" height="500" @mouseenter.native="a(tableData1)"
@mouseleave.native="a(tableData1)">
<el-table-column prop="name" label="项目名称" width="180"></el-table-column>
<el-table-column prop="code" label="项目编码" width="180"></el-table-column>
<el-table-column prop="PM" label="项目经理"></el-table-column>
<el-table-column prop="planTime" label="计划验收时间"></el-table-column>
</el-table>
</el-col>
<el-col :span="12">
<center style="font-size: 24px">待下单项目提醒</center>
<br/>
<el-table :data="tableData2" border stripe style="width: 100%" height="500" @mouseenter.native="b(tableData2)"
@mouseleave.native="b(tableData2)">
<el-table-column prop="name" label="项目名称" width="180"></el-table-column>
<el-table-column prop="code" label="项目编码" width="180"></el-table-column>
<el-table-column prop="PM" label="项目经理"></el-table-column>
<el-table-column prop="planTime" label="计划验收时间"></el-table-column>
</el-table>
</el-col>
</el-row>
data数据部分:
data(){
return{
tableData1:[],//待验收项目提醒页面展示数据
tableData2:[],//待下单项目提醒页面展示数据
ss1:[],//待验收项目提醒临时存储区
ss2:[], //待下单项目提醒临时存储区
autoRoll1:false,
autoRoll2:false,
}
},
methods方法部分:
methods:{
init(){
this.getData();
},
//每次鼠标滑入离开都会调用该方法(控制定时器开启关闭)
a(array){
console.log('hahh')
let self=this
self.ss1=[{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'张三',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/5/12"
},{
name:'小红',
code:'12358964 ',
PM:'里斯',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/4/12"
},{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'王二',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/12/12"
}]
let arr=self.ss1;
//定时器每隔1000毫秒调用一次此函数
function bb(){
//如果展示数组小于临时数组的长度,往展示数组添加元素
if (array.length<arr.length) {
array.unshift(arr[array.length])
} else {//否则代表已经把临时数组元素全部显示出来了,循环播放,把数组中元素删掉剩下一个,再次一条一条添加
array.splice(0,array.length-1)
}
}
if(self.autoRoll1==true){//鼠标滑入:autoRoll为true
clearInterval(self.timer1);
self.autoRoll1=false;
}else{//如果autoRoll为false代表没有启动定时器():初始化和鼠标离开为false
self.timer1 = setInterval(bb,1000);
self.autoRoll1=true;//初始化为false,然后为true,当鼠标滑入时为true
}
},
b(array){
let self=this
self.ss2=[{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'张三',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/5/12"
},{
name:'小红',
code:'12358964 ',
PM:'里斯',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/4/12"
},{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'王二',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/12/12"
}]
let arr=self.ss2;
function bb(){
if (array.length<arr.length) {
array.unshift(arr[array.length])
console.log(array.length)
} else {
array.splice(0,array.length-1)
}
}
if(self.autoRoll2==true){
clearInterval( self.timer2);
self.autoRoll2=false;
}else{
self.timer2 = setInterval(bb,1000);
self.autoRoll2=true;
}
},
//获取两个表单数据
getData(){
this.a(this.tableData1)
this.a(this.tableData2)
},
}
mounted方法部分:
mounted() {
this.init()
},
注意:
(1)多个表单创建多个定时器,不能共用一个定时器
(2)定时器创建时使用this:this.timer2 = setInterval(bb, 1000)
;或self:self.timer2 = setInterval(bb, 1000);
不然因为作用域问题,定时器会清除不掉
(3)判断两个表单鼠标滑入离开:需要两个标识符(共用标识符刚开始会错乱)
(4)以上相同的方法封装成一个函数
tableMove(array,index){
let self=this
/*const BaseUrl = this.$util.getAjaxUrl('type_2')*/
self.ss1=[{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'张三',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/5/12"
},{
name:'小红',
code:'12358964 ',
PM:'里斯',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/4/12"
},{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'王二',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/12/12"
}]
self.ss2=[{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'张三',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/5/12"
},{
name:'小红',
code:'12358964 ',
PM:'里斯',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/4/12"
},{
name:'小米',
code:'哈哈哈哈首先表示 ',
PM:'王二',
//planTime:moment(new Date()).format("YYYY-MM-DD"),//计划验收时间
planTime:"2021/12/12"
}]
if(index==1){
if(self.autoRoll1==true){
clearInterval( self.timer1);
self.autoRoll1=false;
}else{
let aa=self.ss1;
self.timer1 = setInterval( () =>{//这里定时器如果封装成一个函数传参,会出错
if (array.length<aa.length) {
array.unshift(aa[array.length])
} else {
array.splice(0,array.length-1)
}
},1000);
self.autoRoll1=true;
}
}else {
if (self.autoRoll2 == true) {
clearInterval(self.timer2);
self.autoRoll2 = false;
} else {
let aa=self.ss2;
self.timer2 = setInterval(() =>{
if (array.length<aa.length) {
array.unshift(aa[array.length])
} else {
array.splice(0,array.length-1)
}
}, 1000);
self.autoRoll2 = true;
}
}
},