vue实现无缝滚动

 vue非组件实现列表的无缝滚动问题(小编能力有限,如有更好方法还请大佬指点一二)

*原理:首先循环两遍数组,当容器滚去第一个数组高度的时候,第二个数组刚好填满容器,这时候将滚去高度设置为0则可以实现无缝滚动。

*简易原理图如下

话不多说直接上代码:

①采用js的方法实现

<template>
	<div>
		<div class="box">
			<div v-for="item in 2" class="item-box" :style="{transform:'translate(0,'+scrollTop+'px)'}">
				<div class="item" v-for="i in 9">{{i}}</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				scrollTop: 0,
			}
		},
		onLoad() {
			this.roll()
		},
		methods: {
			roll() {
				if (this.scrollTop == -300) {
					this.scrollTop = 0
				}
				this.scrollTop -= 1;
				setTimeout(() => {
					this.roll()
				}, 10)
			},
		}
	}
</script>
<style>
	.box {
		width: 320px;
		height: 300px;
		background-color: pink;
		overflow: hidden;
	}

	.box .item-box {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
	}

	.box .item-box .item {
		width: 29%;
		height: 29%;
		margin: 1%;
		background-color: paleturquoise;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 700;
	}
</style>

二、css动画实现

<template>
	<div>
		<div class="box">
			<div v-for="item in 2" class="item-box">
				<div class="item" v-for="i in 9">{{i}}</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {

		}
	}
</script>
<style>
	.box {
		width: 320px;
		height: 300px;
		background-color: pink;
		overflow: hidden;
	}

	.box .item-box {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
		animation: roll 5s linear infinite;
	}

	.box .item-box .item {
		width: 29%;
		height: 29%;
		margin: 1%;
		background-color: paleturquoise;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 700;
	}

	@keyframes roll {
		0% {
			transform: translate(0, 0px);
		}

		20% {
			transform: translate(0, -60px);
		}

		40% {
			transform: translate(0, -120px);
		}

		60% {
			transform: translate(0, -180px);
		}

		80% {
			transform: translate(0, -240px);
		}

		100% {
			transform: translate(0, -300px);
		}
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue.js是一种流行的JavaScript框架,用于构建用户界面。要实现无缝滚动效果,可以使用Vue.js的transition组件和CSS动画来实现。 首先,你需要在Vue组件中定义一个包含滚动内容的容器,并设置其样式为`overflow: hidden`,以隐藏超出容器的内容。然后,在容器内部创建一个滚动元素,设置其样式为`white-space: nowrap`,以使内容水平排列。 接下来,你可以使用Vue的transition组件来添加过渡效果。在滚动元素上添加一个`v-if`指令,根据条件来决定是否显示滚动元素。当需要滚动时,将滚动元素的内容复制一份,并将其追加到滚动元素的末尾,形成一个无限循环的效果。 然后,你可以使用CSS动画来实现滚动效果。通过给滚动元素添加一个`transform`属性,并使用`translateX`函数来实现水平位移。同时,使用`animation`属性来定义动画的持续时间、缓动函数等。 最后,在Vue组件中使用定时器或者其他方式来触发滚动效果。可以通过改滚动元素的`transform`属性的值来实现滚动效果。 下面是一个简单的示例代码: ```html <template> <div class="scroll-container"> <div class="scroll-content" v-if="isScrolling"> {{ content }} </div> </div> </template> <script> export default { data() { return { content: '这是滚动的内容', isScrolling: true }; }, mounted() { setInterval(() => { // 每隔一段时间改变滚动元素的transform属性值,实现滚动效果 }, 3000); } }; </script> <style> .scroll-container { width: 300px; height: 200px; overflow: hidden; } .scroll-content { white-space: nowrap; animation: scrollAnimation 10s linear infinite; } @keyframes scrollAnimation { 0% { transform: translateX(0); } 100% { transform: translateX(-100%); } } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值