#yyds干货盘点#【愚公系列】2023年02月 微信小程序-电商项目-商品详情页面的轮播图功能实现

时间:2023-02-04 22:54:16

前言

轮播图是指在一个模块或者窗口,通过鼠标点击或手指滑动后,可以看到多张图片。这些图片统称为轮播图,这个模块叫做轮播模块。轮播图常见于电商类、资讯类应用、功能首页、功能模块主页面,还有网易云音乐App的发现模块主页。

小程序swiper的api

属性 类型 默认值 必填 说明 最低版本
indicator-dots boolean false 是否显示面板指示点 1.0.0
indicator-color color rgba(0, 0, 0, .3) 指示点颜色 1.1.0
indicator-active-color color #000000 当前选中的指示点颜色 1.1.0
autoplay boolean false 是否自动切换 1.0.0
current number 当前所在滑块的index 1.0.0
interval number 5000 自动切换时间间隔 1.0.0
duration number 500 滑动动画时长 1.0.0
circular boolean false 是否采用衔接滑动 1.0.0
vertical boolean false 滑动方向是否为纵向 1.0.0
previous-margin string "0px" 前边距,可用于露出前一项的一小部分,接受 px 和 rpx 值 1.9.0
next-margin string "0px" 后边距,可用于露出后一项的一小部分,接受 px 和 rpx 值 1.9.0
snap-to-edge boolean false 当 swiper-item 的个数大于等于 2,关闭 circular 并且开启 previous-margin 或 next-margin 的时候,可以指定这个边距是否应用到第一个、最后一个元素 2.12.1
display-multiple-items number 1 同时显示的滑块数量 1.9.0
easing-function string "default" 指定 swiper 切换缓动动画类型 2.6.5
bindchange eventhandle current 改变时会触发 change 事件,event.detail = {current, source} 1.0.0
bindtransition eventhandle swiper-item 的位置发生改变时会触发 transition 事件,event.detail = {dx: dx, dy: dy} 2.4.3
bindanimationfinish eventhandle 动画结束时会触发 animationfinish 事件,event.detail 同上 1.9.0

小程序swiper-item的api

属性 类型 默认值 必填 说明 最低版本
item-id string 该 swiper-item 的标识符 1.9.0
skip-hidden-item-layout boolean false 是否跳过未显示的滑块布局,设为 true 可优化复杂情况下的滑动性能,但会丢失隐藏状态滑块的布局信息 1.9.0

vant-weapp的api :https://vant-contrib.gitee.io/vant-weapp/#/image

#yyds干货盘点#【愚公系列】2023年02月 微信小程序-电商项目-商品详情页面的轮播图功能实现

一、商品详情页面的轮播图功能实现

<!--轮播图-->
<swiper indicator-dots style="height:300px;">
	<block wx:for="{{goodsImages}}" wx:key="*this">
		<swiper-item>
			<van-image lazy-load loading="loading" fit="cover" width="100%" height="300" src="{{item.content}}" />
		</swiper-item>
	</block>
</swiper>
/* pages/goods/index.wxss *//* miniprogram/pages/goods/index.wxss */
.price .van-cell__title{
  color: rgba(181,154,81,1);
  font-size: 20pt;
}
.title .van-cell__title{
  font-size: 17pt;
}
.buydesc .van-cell__title{
  font-size: 13px;
}
.share{
  font-size:11px;background-color:#F5F9FA;color:#767A7B;border-radius:30px;padding: 5px 10px;margin-right: -27px;width: 50px;text-align: center;float: right;
}
.van-tag{
  margin-left: 10px;
}
.van-popup{
  background-color: #efefef;
}
// miniprogram/pages/goods/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoginPanel:false,
    showSkuPanel: false,
    goodsId:0,
    goodsData:{},
    goodsImages: [],
    goodsContentInfo:{},
    goodsSkuData:{},
    selectedGoodsSku:{},
    selectedAttrValue:{},
    selectedGoodsSkuObject:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let goodsId = options.goodsId
    this.data.goodsId = goodsId
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('goodsData', (res)=> {
      console.log(res)
      let goodsImages = res.data.goods_infos.filter(item=>(item.kind == 0))
      let goodsContentInfo = res.data.goods_infos.filter(item=>(item.kind == 1))[0]

      this.setData({
        goodsData:res.data,
        goodsImages,
        goodsContentInfo
      })
    })
    // 拉取sku规格数据
    let goodsSkuDataRes = await wx.wxp.request({
      url: `http://localhost:3000/goods/goods/${goodsId}/sku`,
    })
    if (goodsSkuDataRes){
      let goodsSkuData = goodsSkuDataRes.data.data 
      this.setData({
        goodsSkuData
      })
    }
  },

})
{
  "usingComponents": {
    "van-image": "@vant/weapp/image/index",
    "van-row": "@vant/weapp/row/index",
    "van-col": "@vant/weapp/col/index",
    "van-cell": "@vant/weapp/cell/index",
    "van-cell-group": "@vant/weapp/cell-group/index",
    "van-popup": "@vant/weapp/popup/index",
    "van-button": "@vant/weapp/button/index",
    "van-tag": "@vant/weapp/tag/index",
    "van-goods-action": "@vant/weapp/goods-action/index",
    "van-goods-action-icon": "@vant/weapp/goods-action-icon/index",
    "van-goods-action-button": "@vant/weapp/goods-action-button/index",
    "LoginPanel":"../../components/login"
  }
}

二、效果

#yyds干货盘点#【愚公系列】2023年02月 微信小程序-电商项目-商品详情页面的轮播图功能实现