v-for下定时给图片添加动画

50 篇文章 0 订阅
21 篇文章 1 订阅

需求:在vue中v-for遍历图片列表依据index时定时给图片添加动画!
效果图:
在这里插入图片描述
.VUE

<div class="a01_header">
	<img 
	   v-for="(items,index) in a01_header" 
	   :key="index" 
	   :class="selectIndex == index ? 'add_animation':''"
	   :src=items.img_url />	
</div>

.JS

data: {
    a01_header: [
		{img_url: './img/01/imgs/a01.png'},
		{img_url: './img/01/imgs/a02.png'},
		{img_url: './img/01/imgs/a03.png'},
		{img_url: './img/01/imgs/a04.png'},
		{img_url: './img/01/imgs/a05.png'}
	],
	selectIndex:0
},
mounted: function() {
	setInterval(() =>{
		this.getMethon();
	},2000)
},
getMethon(){
	const { a01_header } = this;
	let len = a01_header.length,
		index = this.selectIndex;
	if (index + 1 == len) {
		this.selectIndex = 0
	} else {
		this.selectIndex = index + 1;
	}
}

.CSS

.add_animation{
	animation: scaleBtn 2s infinite ease-in-out;
}
@keyframes scaleBtn {

  0% {
    transform: scale(1);
    opacity: 1;
  }

  50% {
    transform: scale(1.1);
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现图片无缝循环滚动,可以使用 Vue 的过渡动画和 CSS3 的 transform 属性。具体实现步骤如下: 1. 在 Vue 的模板中使用 v-for 指令渲染图片列表,并添加一个包裹元素用于容纳图片。 2. 使用 CSS3 的 transform 属性将图片容器平移,使得第一张图片刚好显示在容器中。 3. 使用 Vue 的过渡动画,在图片容器中添加一个过渡组件,用于实现图片的滑动效果。 4. 使用定时器或者 requestAnimationFrame() 方法,定时更新图片容器的 transform 属性,实现图片循环滚动。 5. 为图片容器添加 touchstart、touchmove 和 touchend 事件,实现手动滑动滚动的效果。 下面是一个简单的实现示例: ```html <template> <div class="image-carousel"> <transition-group name="carousel-slide" tag="div"> <div class="image-item" v-for="(image, index) in images" :key="index"> <img :src="image" alt=""> </div> </transition-group> </div> </template> <script> export default { data() { return { images: [ 'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg' ], currentIndex: 0, startX: 0, startY: 0, endX: 0, endY: 0 } }, mounted() { this.startCarousel() this.initTouchEvent() }, methods: { startCarousel() { setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.images.length }, 3000) }, initTouchEvent() { const imageCarousel = this.$el imageCarousel.addEventListener('touchstart', e => { if (e.touches.length === 1) { this.startX = e.touches[0].clientX this.startY = e.touches[0].clientY } }) imageCarousel.addEventListener('touchmove', e => { if (e.touches.length === 1) { this.endX = e.touches[0].clientX this.endY = e.touches[0].clientY e.preventDefault() } }) imageCarousel.addEventListener('touchend', e => { const dx = this.endX - this.startX const dy = this.endY - this.startY if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 20) { if (dx > 0) { // 向右滑动 this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length } else { // 向左滑动 this.currentIndex = (this.currentIndex + 1) % this.images.length } } }) } }, computed: { transformStyle() { const translateX = -this.currentIndex * 100 return { transform: `translateX(${translateX}%)` } } } } </script> <style> .image-carousel { position: relative; overflow: hidden; width: 100%; height: 300px; } .carousel-slide-enter-active, .carousel-slide-leave-active { transition: transform 0.5s; } .carousel-slide-enter, .carousel-slide-leave-to { transform: translateX(100%); position: absolute; } .image-item { display: inline-block; width: 100%; height: 100%; vertical-align: top; } </style> ``` 在上面的代码中,使用了一个计算属性 transformStyle 来动态计算图片容器的 transform 属性,实现了自动循环滚动的效果。同时,使用了 touchstart、touchmove 和 touchend 事件来实现手动滑动滚动的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值