微信小程序如何实现商品数据联动效果

发布时间:2022-08-03 15:55:55 作者:iii
来源:亿速云 阅读:290

本篇内容介绍了“微信小程序如何实现商品数据联动效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

微信小程序实现商品数据联动

微信小程序如何实现商品数据联动效果

js部分:

// pages/demo1/demo1.js
//存放右侧分类的高度累加数组
var heightList = [];
//记录scroll-view滚动过程中距离顶部的高度
var distanceToTop = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    curActive: 0, //当前选中项
    productList: [{
        c_name: '新鲜水果',
        list: [{
            id: 0,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '樱桃'
          },
          {
            id: 1,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 2,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 3,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 4,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 5,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 6,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
          {
            id: 7,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '荔枝'
          },
        ]
      },
      {
        c_name: '海鲜水产',
        list: [{
            id: 0,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 1,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 2,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 3,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 4,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 5,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 6,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 7,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 8,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
        ]
      },
      {
        c_name: '测试数据1',
        list: [{
            id: 0,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 1,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 2,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 3,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 4,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 5,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 6,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 7,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 8,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
        ]
      },
      {
        c_name: '测试数据2',
        list: [{
            id: 0,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 1,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
          {
            id: 2,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 3,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 4,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 5,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 6,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 7,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          }, {
            id: 8,
            img: 'https://img.alicdn.com/imgextra/i4/615340156/O1CN0117eRFg1D1UYcEhmXj_!!0-saturn_solar.jpg_468x468q75.jpg_.webp',
            goodsName: '鲍鱼'
          },
        ]
      }
    ]
  },
  /**
   * 左侧: 菜单事件
   * @param {*} e 
   */
  selectMenu(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      curActive: index,
      toView: "nav" + index
    })
  },
  /**
   * 获取商品列表高度
   * 计算右侧商品某分类的高度
   */
  getProListHeight() {
    let tmpHeightList = [];
    let tmpH = 0; //临时存放每个分类的高度
    // 获取元素信息 返回一个 SelectorQuery 对象实例
    const query = wx.createSelectorQuery()
    query.selectAll('.product').boundingClientRect()
    query.exec(function (res) {
      res[0].forEach((item) => {
        tmpH += item.height;
        // Math.floor()向下取整 【根据个人需要修改】
        tmpHeightList.push(Math.floor(tmpH));
      })
      heightList = tmpHeightList;
      console.log(heightList);
    })
  },
  /**
   * 监听滚动时触发事件
   * @param {*} e 
   */
  scroll(e) {
    if (heightList.length == 0) return;
    let scrollTop = e.detail.scrollTop; //滚动位置
    let current = this.data.curActive;
    console.log('scrollTop======>', scrollTop);
    if (scrollTop > distanceToTop) {
      //如果右侧 可视区域的竖直滚动位置 超过 当前列表选中项距顶部的高度(且没有下标越界),则更新左侧选中项
      if ((current + 1 < heightList.length) && (scrollTop >= heightList[current])) {
        this.setData({
          curActive: current + 1
        })
      }
    } else {
      //如果右侧 可视区域的竖直滚动位置 小于 当前列表选中的项距顶部的高度,则更新左侧选中项
      if ((current - 1 >= 0) && (scrollTop < heightList[current - 1])) {
        this.setData({
          curActive: current - 1
        })
      }
    }
    //更新顶部的距离
    distanceToTop = scrollTop;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取商品列表高度
    this.getProListHeight();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('触发下拉')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触发上拉触底')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})

然后是wxml部分:

<view class="title">商品数据联动</view>

<view class="box">
  <view class="left">
    <scroll-view scroll-y="true" scroll-with-animation="true" class="container">
      <view class="menu-item {{index == curActive ? 'active' : ''}}" wx:for="{{productList}}" data-index="{{index}}" wx:key="index" bindtap="selectMenu">
        {{item.c_name}}
      </view>
    </scroll-view>
  </view>
  <view class="right">
    <scroll-view scroll-y="true" scroll-with-animation="true" bindscroll="scroll" scroll-into-view="{{toView}}" >
      <block wx:for="{{productList}}" wx:key="*this">
        <!-- id:实现点击左侧某分类时,右侧滚动至相应位置(注意:id不能以数字开头)-->
        <view id="nav{{index}}" class="product">
          <view class="c_name">{{item.c_name}}</view>
          <view class="goods-box">
            <view class="goods-item" wx:for="{{item.list}}" wx:for-item='items' wx:key="index">
              <image class="goods-img" src="{{items.img}}" mode="aspectFit"></image>
              <view class="goods-name">{{items.goodsName}}</view>
            </view>
          </view>
        </view>
      </block>
      <view class="bottom">到底了~没有更多了</view>

    </scroll-view>
  </view>
</view>

然后是css部分:

.title {
  text-align: center;
  margin-top: 30px;
}

.box{
  width: 100%;
  height: 400px;
  display: flex;

}
.left {
  width: 80px;
  display: inline-block;
}
.right{
  flex: 1;
  background: #ccc;
}

.container {
  width: 100%;
  height: 400px;
  background-color: gray;
  padding: 0;
}

.menu-item {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #000;
  background-color: pink;
  line-height: 50px;
  text-align: center;
}

.active {
  background-color: red;
}
.c_name{
  width: 100%;
  text-align: center;
  padding-top: 20px;
}
.goods-box {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  
}
.goods-item{
  width: 80px;
  height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.goods-img{
  width: 50px;
}

.bottom{
  width: 100%;
  height: 60px;
  text-align: center;
  line-height: 80px;
  font-size: 10px;
  color: rgb(139, 135, 135);
}

“微信小程序如何实现商品数据联动效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 微信小程序如何实现菜单左右联动
  2. 微信小程序如何实现分享商品海报功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

微信小程序

上一篇:java开发ShardingSphere的路由引擎类型有哪些

下一篇:Vue中如何使用Teleport

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》