vue中实现简单切换图片效果

本文介绍如何在Vue项目中实现一个功能,当存在多于三张图片时,通过点击箭头图标来切换图片,并在切换过程中动态改变箭头的样式以反映其可用状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实例:

实现大于三张图片时,点击箭头图片切换,并有箭头置灰不可操作。

html

 

<div class="summary">
	<div class="gallery">
		<div class="product-img">
			<div @click="prevImg" class="prev-arrow" :class="{'prev-active': galleryList.length - 3 <= 0 || this.offsetCount < 1}">
				<img src="../assets/images/productDetail/left.png" alt="左箭头">
			</div>
			<ul>
				<li class="list-img">
					<div class="list-img-wrap" ref="carouser">
						<div @click="getCurImg(item.ImageCode, index)" v-for="(item,index) in galleryList" :key="index" class="single-img" :class="{'img-activce': item.ImageCode == curCode && imgActive}">
							<img :src="item.ImageCode" alt="缩略图">
						</div>
					</div>
				</li>
			</ul>
	            <div @click="nextImg" class="next-arrow" :class="{'next-active': galleryList.length - 3 <= 0 || offsetCount >= galleryList.length - 3 }">
			        <img src="../assets/images/productDetail/right.png" alt="右箭头">
		       </div>
		</div>
	</div>
</div>

 

 

js:

data中-------> offsetCount: 0,
prevImg () {
	if (this.galleryList.length - 3 > 0) {
		if (this.offsetCount > 0) {
			this.offsetCount --;					
			this.$refs.carouser.style.left = '-' + (8.37 * this.offsetCount) + 'rem';					
		} else {
			return false
		}			
	} else {
		return false				
	}
},
nextImg () {
	if (this.galleryList.length - 3 > 0) {
		if (this.offsetCount < this.galleryList.length - 3) {
			this.offsetCount ++;					
			this.$refs.carouser.style.left = '-' + (8.37 * this.offsetCount) + 'rem';					
		} else {
			return false
		}			
	} else {
		return false				
	}
},

css:

.product-img {
	display: flex;
	justify-content: flex-start;
	.prev-arrow,
	.next-arrow{
		cursor: pointer;		
	}
	.prev-active,
	.next-active{
		cursor: not-allowed;
	}
	ul {
		list-style: none;
		padding: 0;
		margin: 0;
		display: flex;
		justify-content: flex-start;
		.list-img {
			width: 25.11rem;
			height: 5.71rem;
			position: relative;  //最外层的宽度,父级定位
			overflow: hidden;						
			.list-img-wrap {
				position: absolute; //子级定位,dom操作偏移
				left: 0;
				top: 0;
				.single-img {
				    cursor: pointer;
					float: left;								
					width: 5.71rem;
					height: 5.71rem;
					margin: 0 1.33rem;							
					border: 1px solid #E5E5E5;	
				}
				.img-activce{
					border: 1px solid #C12022;
				}
			}
		}
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值