uniapp中使用swiper轮播以及解决自动,手动切换页面抖动问题

本文介绍了如何在 uniapp 中使用 swiper 实现轮播效果,并着重讲解了如何解决自动和手动切换时页面抖动的问题。通过设置 swiper 的属性,如 indicator-dots、autoplay、interval 和 duration,以及在 swiperChange 方法中进行判断,确保只有在自动切换或触摸滑动时才会更新当前页,从而避免了不必要的抖动现象。
摘要由CSDN通过智能技术生成

uniapp中使用swiper轮播以及解决自动,手动切换页面抖动问题


在这里插入图片描述

<template>
	<view class="banner-wrap">
		<swiper class="banner-swiper" 
		         :current="current"
		         :indicator-dots="indicatorDots" 
				 :autoplay="autoplay" 
				 :duration="duration" 
				 :interval="interval" 
				 :circular="circular" 
				 :previous-margin="previousMargin"
				 :next-margin="nextMargin"
				 @change="swiperChange">
			<swiper-item v-for="(item,index) in bannerList" :key="index">
				<view class="banner-swiper-item">
					<image :src="item.image" mode="aspectFill" class="banner-swiper-img" :class="current == index?'':'active'"></image>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		name:"index-swiper",
		props:{
			bannerList:{
				type:Array,
				default:()=>{
					return []
				}
			}
		},
		data() {
			return {
				current:1,//当前图片
				indicatorDots: false,//是否显示面板指示点
				autoplay: true,//是否自动切换
				interval: 4000,//自动切换时间间隔
				duration: 500,//滑动动画时长
				circular:true,//是否采用衔接滑动,即播放到末尾后重新回到开头
				previousMargin:"17px",//前边距
				nextMargin:"17px",//后边距
			};
		},
		methods:{
			swiperChange(e){
				let {current, source} = e.detail
				//只有页面自动切换,手动切换时才轮播,其他不允许
				if(source === 'autoplay' || source === 'touch'){
					 this.current = current;
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.banner-wrap{
		width: 100%;
		margin: auto;
		.banner-swiper{
			height: 175px;
			.banner-swiper-item{
				border-radius: 8px;
				width: 100%;
				height: 175px;
				overflow: hidden;
			}
			
			.banner-swiper-img{
				display: block;
				margin: auto;
				width: 98%;
				height: 100%;
				border-radius: 8px;
			}
			.active{
				height:88.57%;
				margin-top:20px;
				transition: all .1s ease-in 0s;
				transform: translateY(0);
			}
		}
	}
</style>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值