利用vue框架如何做列表循环滚动的展示
Admin 2022-09-16 群英技术资讯 1026 次浏览
很多朋友都对“利用vue框架如何做列表循环滚动的展示”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!功能介绍:
在PC端网页,包括大数据、官网、后台管理平台开发中,可能会用到这种列表循环滚动的展示。
1、列表可以使用数组循环遍历;
2、每隔几秒中列表数据向上滚动一定距离,展示下一条数据;
3、滚动到最后一条数据时重新显示第一条开始的数据(类似走马灯、banner图的循环效果);
1、使用两个定时器嵌套实现;
2、需要两个相同容器存放同样内容,实现无缝衔接效果;

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<style>
/* 滚动表格最外层 */
.tableoOut {
margin: 100px auto;
width: 500px;
height: 400px;
background: pink;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.tableBox {
width: 100%;
background: #000;
overflow: hidden
}
.tableTit {
background: #000;
width: 100%;
height: 40px;
color: #858A84;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
}
.tableInner {
height: auto;
}
.box {
width: 100%;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
}
.box .time {
color: #858A84;
}
.tableoOut .addr, .tableoOut .time, .tableoOut .name {
box-sizing: border-box;
padding: 0 5px;text-align: center;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.tableoOut .addr {
width: calc(100% - 200px);
flex-shrink: 0;
}
.tableoOut .name, .tableoOut .time {
width: 100px;
flex-shrink: 0;
}
</style>
</head>
<body>
<div id="app">
<div class="tableoOut" ref="tableoOut">
<div class="tableTit">
<div class="name">姓名</div>
<div class="addr">地址</div>
<div class="time">入驻时间</div>
</div>
<div class="tableBox" ref="tableBox"
:style="{height: tableHei}">
<div class="tableInner" ref="tableInner">
<div class="box" v-for="item in 7" :key="item">
<div class="name">{{item}}</div>
<div class="addr">山东省山东省山东省山东省山东省山东省山东省山东省
山东省山东省山东省山东省山东省</div>
<div class="time">2022-05-26</div>
</div>
</div>
<div class="tableInner" v-if="size < 7">
<div class="box" v-for="item in 7" :key="item">
<div class="name">{{item}}</div>
<div class="addr">山东省山东省山东省山东省山东省山东省山东省山东省
山东省山东省山东省山东省山东省</div>
<div class="time">2022-05-26</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
tableHei: 'auto',
timer: null,
size: 0
},
mounted() {
this.getTable();
},
methods: {
getTable() {
const outHei = this.$refs.tableoOut.clientHeight - 60;
this.size = Math.floor(outHei / 50);
this.tableHei = this.size * 50 + 'px';
this.scrolls();
},
stepScroll() {
const step = 50;
let num = 0;
const tableBox = this.$refs.tableBox;
const stepTime = setInterval(function () {
num += 2;
if (num > step) {
num = 0;
clearInterval(stepTime);
} else {
tableBox.scrollTop += 2;
}
}, 20);
},
scrolls() {
const that = this;
const tableBox = this.$refs.tableBox;
const tableInner = this.$refs.tableInner;
clearInterval(this.timer);
this.timer = setInterval(function () {
if(tableBox.scrollTop === tableInner.scrollHeight) {
tableBox.scrollTop = 0;
}
that.stepScroll();
}, 2000);
},
}
})
</script>
</html>
setInterval踩坑:
发现这种方法实现的定时轮播,有一阵没访问页面,会出现卡停的情况,采用下面的解决方法:
<script>
new Vue({
el: '#app',
data: {
tableHei: 'auto',
timer: null,
size: 0,
stopSign: true, // 判断定时器是否停止标识
stepTime: null, // 改为全局定时器
},
mounted() {
const that = this;
// 增加浏览器激活状态判断。非激活状态为onblur
window.onfocus = function(e) {
const tableBox = that.$refs.tableBox;
const sT = tableBox.scrollTop;
console.log("激活状态!")
if (!that.stopSign) {
tableBox.scrollTop = Math.round(sT / 50) * 50;
clearInterval(that.stepTime);
}
}
this.getTable();
},
methods: {
getTable() {
const outHei = this.$refs.tableoOut.clientHeight - 60;
this.size = Math.floor(outHei / 50);
this.tableHei = this.size * 50 + 'px';
this.scrolls();
},
stepScroll() {
const that = this;
const step = 50;
let num = 0;
const tableBox = this.$refs.tableBox;
// 改为全局定时器,且在调用前先进行清空
clearInterval(this.stepTime);
this.stepTime = setInterval(function () {
that.stopSign = false;
num += 2;
if (num > step) {
num = 0;
clearInterval(that.stepTime);
that.stopSign = true;
} else {
tableBox.scrollTop += 2;
}
}, 1000 / 60);
},
scrolls() {
const that = this;
const tableBox = this.$refs.tableBox;
const tableInner = this.$refs.tableInner;
clearInterval(this.timer);
this.timer = setInterval(function () {
// 修改定时器结束判断条件
if(tableBox.scrollTop >= tableInner.scrollHeight) {
tableBox.scrollTop = 0;
}
that.stepScroll();
}, 2000);
},
}
})
</script>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章给大家带来了关于JavaScript的相关知识,其中主要整理了数组知识点的相关问题,包括了数组的概念、数组中的常用属性和方法等等内容,下面一起来看一下,希望对大家有帮助。
windows下安装Node出现报错2503怎么解决?有一些朋友就有遇到node安装失败2503报错的问题,那么究竟我们该怎么办呢?下文给大家分享了一个解决方法,操作如下,大家可以了解看看,希望能帮助到大家。
jQuery中有什么方法增加class的属性?对于jQuery中增加class的属性的方法,这里给大家介绍addClass()方法,这是 jquery提供一个添加class类的方法,接下来我们那就来具体的了解看看,addClass()方法怎样实现增加class。
草稿二:Node.jsChildProcess模块GitHub TOP ChildProcess模块来自《JavaScript标准参考教程(alpha)》,by阮一峰目录exec()execSync()execFile()spawn()fork()send()参考链接child_process模块用于新建子进程。子进程的运行结果储存在系
TypeScript中泛型是什么意思?怎样使用?对新手来说,可能对泛型和泛型的用法不是很了解,对此这篇文章给大家分享TypeScript中泛型的内容。小编觉得挺实用的,因此给大家做个参考,感兴趣的朋友接着往下看。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008