【uniapp vue实现轮播图跑马灯】

我把这个封装成一个组件的形式,方便大家使用,以下为swiper组件代码

<template>
	<view class="swiper" ref="swiper">
		<view class="strip" ref="strip">
			<view class="strip-item" v-for="(item,index) in list" :key="index" @touchstart="touchStart($event,index)"
				@touchmove="touchMove($event,index)" @touchend="touchEnd($event,index)">
				<image class="cover" :src="item.image" mode=""></image>
				<image class="button" src="https://file.yuanyinfu.com/front-end-lib/play.png" mode="" @tap="handGo(item.url)"></image>
			</view>
		</view>
		<view class="page">
			<view v-for="(item,index) in list" :key="index" :class="['page-item',activeIndex===index?'active':'']">
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "mySwiper",
		data() {
			return {
				width: 0,
				height: 0,
				startX: 0,
				moveX: 0,
				startOffset: 0,
				loading: false, //动画运行中不允许再操作
				activeIndex: 0
			};
		},
		props: {
			list: {
				required: true,
				type: Array,
				description: `[
					{
						image:'https://xxxx....',
						url:'https://xxxxxxxx'  点击
					}
				]
				`
			},
			showPage: {
				default: true,
				description: '是否显示小点'
			}
		},
		methods: {
			// 轮播图大小为父盒子的宽高
			init() {
				this.width = this.$refs.strip.$parent.$parent.$parent.$el.offsetWidth
				this.height = this.$refs.strip.$parent.$parent.$parent.$el.offsetHeight
				this.$refs.swiper.$el.style.width = this.width + 'px'
				this.$refs.swiper.$el.style.height = this.height + 'px'
				this.$refs.strip.$children.forEach(item => {
					item.$el.style.width = this.width + 'px'
					item.$el.style.height = this.height + 'px'
				})
			},
			touchStart(e, index) {
				if (this.loading) return false
				this.startX = e.changedTouches[0].pageX
				this.moveX = e.changedTouches[0].pageX
				this.startOffset = this.$refs.strip.$el.offsetLeft //滑动条相对视口的距离
				// console.log('start', this.startX, index)
			},
			touchMove(e, index) {
				if (this.loading) return false
				// console.log('move', e.changedTouches[0].pageX, index)
				this.$refs.strip.$el.style.left = this.$refs.strip.$el.offsetLeft + (e.changedTouches[0].pageX - this
					.moveX) + 'px'
				this.moveX = e.changedTouches[0].pageX
			},
			touchEnd(e, index) {
				if (this.loading) return false

				// console.log('end', e.changedTouches[0].pageX, index)
				const distanceX = e.changedTouches[0].pageX - this.startX //正数向右划 负数向左划
				this.$refs.strip.$el.style.transition = 'left 0.3s'
				if (distanceX > 0) {
					if (index === 0) {
						// 左边没图了  回正
						this.$refs.strip.$el.style.left = this.startOffset + 'px' //回正
					} else {
						this.$refs.strip.$el.style.left = this.startOffset + this.width + 'px'
						this.activeIndex = index - 1
					}
				} else if (distanceX < 0) {
					if (this.list.length === index + 1) {
						this.$refs.strip.$el.style.left = this.startOffset + 'px'
					} else {
						this.$refs.strip.$el.style.left = this.startOffset - this.width + 'px'
						this.activeIndex = index + 1
					}

				} else {
					this.$refs.strip.$el.style.left = this.startOffset + 'px'
				}

				this.loading = setTimeout(() => {
					this.$refs.strip.$el.style.transition = 'none'
					this.loading = false
				}, 300)
			},
			handGo(url) {
				console.log(url)
			}
		},
		mounted() {
			this.init()
		}
	}
</script>

<style lang="scss">
	.swiper {
		// 视口
		position: relative;
		overflow: hidden;
		// width: 628rpx;
		// height: 362rpx;
		margin: auto;

		.strip {
			// 滚动的长条
			position: absolute;
			top: 0;
			left: 0;
			display: flex;
			align-items: center;

			.strip-item {
				// 每一张图
				// width: 628rpx;
				// height: 362rpx;
				position: relative;

				.cover {
					width: 100%;
					height: 100%;
				}

				.button {
					position: absolute;
					bottom: 26.8rpx;
					right: 20.8rpx;
					width: 96rpx;
					height: 96rpx;
				}
			}
		}

		.page {
			width: 100%;
			display: flex;
			align-content: center;
			justify-content: center;
			flex-wrap: wrap;
			position: absolute;
			bottom: 0;
			left: 0;
			z-index: 999;

			.page-item {
				border-radius: 4rpx;
				width: 30rpx;
				height: 8rpx;
				background-color: rgba(255, 255, 255, 0.3);
				margin: 10rpx;
			}

			.active {
				background-color: rgba(255, 255, 255, 0.8);
			}
		}
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一种流行的JavaScript框架,用于构建用户界面。它具有响应式数据绑定和组件化的特性,使得开发者可以更轻松地构建交互式的Web应用程序。 CSS跑马灯轮播图是一种常见的网页元素,用于展示多张图片或内容,并以动画的方式进行切换。在Vue3中,可以通过以下步骤来实现CSS跑马灯轮播图: 1. 创建一个Vue组件,命名为Carousel。 2. 在组件中定义一个data属性,用于存储轮播图的数据,例如图片的URL或者内容。 3. 在组件的模板中使用v-for指令,遍历轮播图数据,并生成对应的DOM元素。 4. 使用CSS样式设置轮播图容器的宽度和高度,并设置overflow属性为hidden,以隐藏超出容器范围的内容。 5. 使用CSS动画或过渡效果,实现轮播图的切换效果。可以使用@keyframes定义动画序列,或者使用transition属性设置过渡效果。 6. 在组件的生命周期钩子函数中,使用定时器或其他方式,控制轮播图的自动切换。 下面是一个简单的示例代码: ```html <template> <div class="carousel"> <div class="slide" v-for="(item, index) in carouselData" :key="index"> <!-- 轮播图内容 --> <img :src="item.image" alt="carousel image" /> </div> </div> </template> <script> export default { data() { return { carouselData: [ { image: 'image1.jpg' }, { image: 'image2.jpg' }, { image: 'image3.jpg' }, ], }; }, }; </script> <style> .carousel { width: 100%; height: 300px; overflow: hidden; } .slide { width: 100%; height: 100%; animation: carouselAnimation 10s infinite; } @keyframes carouselAnimation { 0% { transform: translateX(0); } 33% { transform: translateX(-100%); } 66% { transform: translateX(-200%); } 100% { transform: translateX(0); } } </style> ``` 这是一个简单的Vue3 CSS跑马灯轮播图实现示例。你可以根据自己的需求进行样式和动画的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值