利用 vue 过渡效果(transition)定时器 实现轮播

轮播图 相信都不会陌生,很多的网站都会有,本文就是用 vue 自带的 transition 动画过渡效果加上定时器来编写一个轮播图。

1、功能分析。

从轮播常用功能来说有四个功能:

  1. 自动的轮播的功能;
  2. 点击左右箭头(pre、next)进行的切换;
  3. 点击数字切换到对应的图片的功能;
  4. 鼠标悬停、移出控制轮播图的开始、暂停;

2、场景介绍、分析。

环境是 vue 组件的形式,轮播图模块是以组件的形式,内嵌在父组件中。这样的好处是轮播图功能,低耦合,复用性高,即插即用。这边我将轮播图的组件取名为 slide.vue,父组件为product.vue。

3、源码分析。

Step1. 父组件product.vue

<template>
	<div class="index-wrap">
		<div class="index-right">
			<slide-show :slides="slides" :inv="slideSpeed"></slide-show>
		</div>
	</div>
</template>

<script>
import slideShow from './slide.vue'
	export default {
		components:{
			slideShow
		},
		data () {
			return {
				slideSpeed:2000,
				slides:[
					{
						src:require('../assets/slide/pic1.jpg'),
						title:'xxx1',
						href:'https://www.baidu.com/'
					},
					{
						src:require('../assets/slide/pic2.jpg'),
						title:'xxx2',
						href:'https://www.baidu.com/'
					},
					{
						src:require('../assets/slide/pic3.jpg'),
						title:'xxx3',
						href:'https://www.baidu.com/'
					},
					{
						src:require('../assets/slide/pic4.jpg'),
						title:'xxx4',
						href:'https://www.baidu.com/'
					}

				]
			}
		}
	}
</script>

<style scoped>
.index-wrap {
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
}
.index-left {
  float: left;
  width: 300px;
  text-align: left;
}
.index-right {
  float: left;
  width: 900px;
}
.index-left-block {
  margin: 15px;
  background: #fff;
  box-shadow: 0 0 1px #ddd;
}
.index-left-block .hr {
  w
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现异性轮播的纵向叠层卡片,可以考虑使用 Vue.js 中的动画和过渡效果实现。 首先,需要将卡片按照层级进行分组,并在每个组内实现水平轮播。可以使用 CSS3 中的 transformtransition 属性来实现水平轮播效果。 接下来,使用 Vue.js 中的 transition 组件和动态绑定 class 的方式来实现纵向叠层效果。可以为每个卡片组定义一个类名,根据组内卡片的位置动态绑定该类名,从而实现卡片的纵向叠层效果。 最后,为了实现异性轮播效果,可以在组件中添加定时器,定时改变卡片组的位置。可以使用 Vue.js 中的 watch 属性来监听卡片组位置的变化,并在变化时触发动画效果,从而实现异性轮播效果。 下面是一个简单的示例代码: ```html <template> <div class="card-container"> <div v-for="(group, index) in groups" :key="index" :class="['card-group', 'group-' + index]"> <div v-for="(card, index) in group" :key="index" class="card"> <img :src="card.imageUrl" /> <h3>{{ card.title }}</h3> <p>{{ card.description }}</p> </div> </div> </div> </template> <script> export default { data() { return { groups: [ [ { imageUrl: 'https://picsum.photos/200/300', title: 'Card 1', description: 'Card 1 description', }, { imageUrl: 'https://picsum.photos/200/300', title: 'Card 2', description: 'Card 2 description', }, { imageUrl: 'https://picsum.photos/200/300', title: 'Card 3', description: 'Card 3 description', }, ], [ { imageUrl: 'https://picsum.photos/200/300', title: 'Card 4', description: 'Card 4 description', }, { imageUrl: 'https://picsum.photos/200/300', title: 'Card 5', description: 'Card 5 description', }, { imageUrl: 'https://picsum.photos/200/300', title: 'Card 6', description: 'Card 6 description', }, ], ], groupIndex: 0, cardIndex: 0, }; }, watch: { groupIndex() { this.animate(); }, }, mounted() { setInterval(() => { this.cardIndex++; if (this.cardIndex >= this.groups[this.groupIndex].length) { this.cardIndex = 0; this.groupIndex++; if (this.groupIndex >= this.groups.length) { this.groupIndex = 0; } } }, 3000); }, methods: { animate() { const cards = document.querySelectorAll('.card-group'); const currentGroup = cards[this.groupIndex]; const currentCard = currentGroup.querySelectorAll('.card')[this.cardIndex]; cards.forEach((card) => { card.classList.remove('current', 'prev', 'next'); }); currentGroup.classList.add('current'); const prevGroup = cards[this.groupIndex - 1] || cards[cards.length - 1]; prevGroup.classList.add('prev'); const nextGroup = cards[this.groupIndex + 1] || cards[0]; nextGroup.classList.add('next'); currentCard.classList.add('active'); currentCard.addEventListener('transitionend', () => { currentCard.classList.remove('active'); }); }, }, }; </script> <style scoped> .card-container { width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: hidden; } .card-group { display: flex; position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform: translateY(100%); transition: transform 0.5s ease-in-out; } .card-group.current { transform: translateY(0); } .card-group.prev { transform: translateY(-100%); } .card-group.next { transform: translateY(100%); } .card { position: relative; width: 300px; height: 400px; margin: 0 10px; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; background-color: #fff; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); overflow: hidden; opacity: 0; transition: opacity 0.5s ease-in-out; } .card.active { opacity: 1; } .card img { width: 100%; height: auto; object-fit: cover; margin-bottom: 10px; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值