当前位置: 首页> 娱乐> 影视 > Vue实现列表的无缝滚动功能

Vue实现列表的无缝滚动功能

时间:2025/7/15 2:27:58来源:https://blog.csdn.net/weixin_43972992/article/details/139239869 浏览次数:0次

Vue实现列表的无缝滚动功能

直接上代码
codePen地址

<template><div id="app"><div class="list" @mouseenter="mouseEnter" @mouseleave="mouseLeave"><divclass="list-box"ref="listRef":style="{ transform: `translateY(${scrollTop}px)` }"><div class="list-item" v-for="(item, index) in list" :key="index"><span :class="[`item-${index}`, 'item-name']">No.{{ index + 1 }}</span><span class="item-content">{{ item.text }}</span></div><div class="list-item" v-for="(item, index) in list" :key="index + 100"><span :class="[`item-${index}`, 'item-name']">No.{{ index + 1 }}</span><span class="item-content">{{ item.text }}</span></div></div></div></div>
</template><script>
export default {data() {return {list: [{text: "1-实时播报"},{text: "2-实时播报"},{text: "3-实时播报"},{text: "4-实时播报"},{text: "5-实时播报"},{text: "6-实时播报"},{text: "7-实时播报"},{text: "8-实时播报"},{text: "9-实时播报"},{text: "10-实时播报"}],scrollTop: 0,boxDomHeight: 0,clearInt: null};},mounted() {this.$nextTick(() => {this.handleLoop();});},methods: {// 自动滚动handleLoop() {const getItem = document.querySelector(".list-item");this.boxDomHeight = (getItem.clientHeight + 10) * this.list.length;this.handleInterval();},handleInterval() {this.clearInt = setInterval(() => {this.scrollTop -= 1;if (this.scrollTop * -1 >= this.boxDomHeight) {this.scrollTop = 0;}}, 10);},// 鼠标移入mouseEnter() {clearInterval(this.clearInt);},// 鼠标移出mouseLeave() {this.handleInterval();}},beforeUnmount() {clearInterval(this.clearInt);}
};
</script><style lang="scss">
.list {overflow: hidden;height: 300px;
}.list-item {margin-bottom: 10px;background-size: 100% 100%;font-size: 14px;display: flex;align-items: center;background-color: rgba(3, 42, 50, 0.4);border-radius: 6px;cursor: pointer;height: 40px;line-height: 40px;
}
.item-name {font-weight: 700;color: #ffffff;text-align: center;width: 50px;margin-right: 10px;background-color: #01707d;border-top-left-radius: 6px;border-bottom-left-radius: 6px;&.item-0 {background-color: #991a33;}&.item-1 {background-color: #d19e3d;}
}
.item-content {flex: 1;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 1;-webkit-box-orient: vertical;font-weight: 200;color: #fff;
}
</style>
关键字:Vue实现列表的无缝滚动功能

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: