uniapp制作小程序产品详情页面swiper带缩略图点击小图切换大图

写在前面:老菜鸟这都是笨方法,高手和大神就不要看了。

uniapp里面的swiper默认是没有小图功能的,即使用了那个插件也只是可以自定义下面那些小圆点的形状而已。

而点击小图切换大图则经常用于PC端的网站上。有的还带放大镜的效果。

现在客户想在小程序上也具有这样的效果。该怎么处理呢?

经过一番摸索:我的实现思路是:在页面上放置两个swiper通过同步current的值来实现点击小图切换大图,和在切换大图的时候给对应的小图加高亮类。

下面我直接把整个代码贴出来吧。一段一段的贴也不像HTML那样容易理解。

<template>
	<view class="page">
		<!-- 幻灯 -->
		<view class="page-section swiper">
			<view class="page-section-spacing">
				<uni-swiper-dot :info="productPics" :current="current" :mode="mode" autoplay="true" :dots-styles="dotsStyles" field="content">
					<swiper class="swiper datu" @change="change" :current="current" autoplay="true">
						<swiper-item v-for="(ban,index) in productPics" :key="index">
							<view class="swiper-item uni-bg-red">
								<image :src="ban" class="pro_image"></image>
							</view>
						</swiper-item>
					</swiper>
				</uni-swiper-dot>
				<swiper class="swiper xiaotu" display-multiple-items="4" :current="xiaotu_current">
					<swiper-item v-for="(ban,index) in productPics" :key="index" @click="bigpic(index)">
						<view class="swiper-item uni-bg-red">
							<image :src="ban" class="pro_image" :class="[index == current ? 'active' : '']"></image>
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
	</view>
</template>

<script>
	import uniSwiperDot from "@/components/uni-swiper-dot/uni-swiper-dot.vue";
	import uniIcons from "@/components/uni-icons/uni-icons.vue";
	export default {
		data() {
			return {
				productPics: [],
				current: 0, //这里是关键
				xiaotu_current:0, //这里是关键
				mode: 'round',
				dotsStyles: {
					bottom: 20,
					backgroundColor: '#fff',
					border: 'none',
					selectedBackgroundColor: 'rgba(255,255,255,0.5)',
					selectedBorder: 'none'
				},
			}
		},
		onLoad(option) {
			uni.setNavigationBarTitle({
				title: "产品详情"
			});
			uni.request({
				url: '写上你的请求地址',
				method: 'GET',
				data: {},
				success: res => {
					console.log(res);
					this.productPics = res.data.proinfo.xiangce;
				},
				fail: () => {},
				complete: () => {}
			});
		
		},
		methods: {
			//大图变换控制小图当前类
			change(e) {
				this.current = e.detail.current;
				if(e.detail.current > 3){
					this.xiaotu_current = 1;
				}else if(e.detail.current > 6){
					this.xiaotu_current = 2;
				}else{
					this.xiaotu_current = 0;
				}
			},
			//点击小图切换指定大图
			bigpic(index){
				this.current = index;
			}
		},
		components: {
			uniSwiperDot,
			uniIcons
		}
	}
</script>

<style>
.datu{
	width: 100%;
	height:562upx;
}
.datu .pro_image{
	width: 100%;
	height: 562upx;
}
.xiaotu{
	padding: 10px 10px 10px 5px;
	height:140upx;
}
.xiaotu .pro_image{
	width:160upx;
	height: 120upx;
	margin-left: 5px;
	margin-right: 5px;
	border: 1px solid #fff;
	padding:2px;
}
.xiaotu .pro_image.active{
	border-color: #ff6600;
}
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Nuxt 中使用略图Swiper 组件,你需要安装 `swiper` 和 `vue-awesome-swiper` 两个依赖包。然后在你的 Nuxt 项目中,可以按照以下步骤实现略图Swiper。 1. 安装依赖 ```bash npm install swiper vue-awesome-swiper --save ``` 2. 在 Nuxt 中引入 Swiper 在 `nuxt.config.js` 文件中加入以下配置: ```javascript module.exports = { // ... build: { vendor: ['swiper'] }, // ... plugins: [ {src: '~/plugins/vue-awesome-swiper', ssr: false} ], // ... } ``` 这段代码会把 Swiper 加入到 Nuxt 的 vendor 中,并且在客户端运行时加载 `vue-awesome-swiper` 插件。 3. 创建略图Swiper 组件 在你的 Vue 单文件组件中,添加以下代码: ```html <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(image, index) in images" :key="index"> <img :src="image.url" :alt="image.alt"> </div> </div> <div class="swiper-pagination"></div> <div class="swiper-scrollbar"></div> <div class="swiper-thumbs-container"> <div class="swiper-thumbs-wrapper"> <div class="swiper-thumb" v-for="(image, index) in images" :key="index"> <img :src="image.thumbUrl" :alt="image.alt"> </div> </div> </div> </div> </template> <script> export default { data() { return { images: [ { url: 'https://placeimg.com/640/480/any', thumbUrl: 'https://placeimg.com/100/100/any', alt: 'Image 1', }, { url: 'https://placeimg.com/640/480/nature', thumbUrl: 'https://placeimg.com/100/100/nature', alt: 'Image 2', }, { url: 'https://placeimg.com/640/480/tech', thumbUrl: 'https://placeimg.com/100/100/tech', alt: 'Image 3', }, ], } }, mounted() { const swiper = this.$refs.swiper.$swiper const thumbsSwiper = this.$refs.thumbsSwiper.$swiper swiper.controller.control = thumbsSwiper thumbsSwiper.controller.control = swiper }, } </script> <style> .swiper-container { width: 100%; height: 100%; } .swiper-slide img { width: 100%; height: 100%; object-fit: cover; } .swiper-thumbs-container { margin-top: 10px; } .swiper-thumb { width: 80px; height: 80px; margin-right: 10px; } .swiper-thumb img { width: 100%; height: 100%; object-fit: cover; border: 2px solid #ddd; } .swiper-thumb-active img { border-color: #333; } </style> ``` 这段代码会创建一个略图Swiper 组件,并且通过 `controller.control` 属性实现主 Swiper略图 Swiper 的交互。 4. 在页面中使用 Swiper 组件 在你的页面中,使用以下代码引入刚才创建的 Swiper 组件: ```html <template> <div> <swiper :options="swiperOptions" ref="swiper"> <!-- Swiper slides --> </swiper> <div class="swiper-thumbs"> <swiper :options="thumbOptions" ref="thumbsSwiper"> <!-- Swiper thumbs --> </swiper> </div> </div> </template> <script> import { Swiper, SwiperSlide } from 'vue-awesome-swiper' export default { components: { Swiper, SwiperSlide, }, data() { return { swiperOptions: { pagination: { el: '.swiper-pagination', clickable: true, }, scrollbar: { el: '.swiper-scrollbar', }, }, thumbOptions: { slidesPerView: 3, spaceBetween: 10, freeMode: true, watchSlidesVisibility: true, watchSlidesProgress: true, } } }, } </script> <style> .swiper-thumbs { margin-top: 20px; } </style> ``` 这段代码会在你的页面中使用刚才创建的 Swiper 组件,并且通过 `options` 属性传递 Swiper 配置参数。同时,略图 Swiper 组件也被嵌套在主 Swiper 组件中。 这样就可以实现在 Nuxt 中使用略图Swiper 组件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值