当前位置:   article > 正文

微信小程序商品分类页最佳实践_分类列表商品滚动交互描述

分类列表商品滚动交互描述

首先我们来分析下UI小妹发来的产品原型图

微信小程序商品分类页需要实现

1.单击左边的商品类目,右侧实现联动跳转到对应商品类目标题;

2.触屏拖动右侧商品列表,右侧跳转到对应商品类目;

2.分析需求我们可以把屏幕分为以下部分,主要使用到view scroll-view,代码结构和分解图如下:

  1. <view>
  2. <!--搜索框-->
  3. <view></view>
  4. <!--商品类别.商品列表-->
  5. <view>
  6. <!--left-->
  7. <scroll-view></scroll-view>
  8. <!--right-->
  9. <scroll-view></scroll-view>
  10. </view>
  11. </view>

 3.搜索view比较简单,在这里就不在阐述,主要实现商品类别和商品列表的交互。

scroll-view使用到的属性

scroll-y:允许纵向滚动(需要设置高度)。

scroll-with-animation:在设置滚动条位置时使用动画过渡。

scroll-top:设置竖向滚动条位置(商品列表上下滑动时动态变更位置)。

scroll-into-view:值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素。

代码实现最后结果如下:

1.test.wxml

  1. //*******************************************************
  2. //* 微信版蚂蚁森林上线了!微信搜蚂蚁森林立即体验! *
  3. //*******************************************************
  4. <view class="container">
  5. <view style="height: 100rpx;"></view>
  6. <view class="VerticalBox">
  7. <!--left-->
  8. <scroll-view class="VerticalNav nav" scroll-y scroll-with-animation scroll-top="{{navTop}}" style="height:calc(100vh - 100rpx)">
  9. <view class="cu-item {{index==TabCur?'text-green cur':''}}" wx:for="{{dRes}}" wx:key="this" bindtap='tabSelect' data-id="{{index}}">{{item._name}}</view>
  10. </scroll-view>
  11. <!--right-->
  12. <scroll-view class="VerticalMain" scroll-y scroll-with-animation style="height:calc(100vh - 100rpx)" scroll-into-view="main-{{titleCur}}" bindscroll="VerticalMain">
  13. <view class="padding-lr" wx:for="{{dRes}}" wx:key="this" id="main-{{index}}">
  14. <!--标题-->
  15. <view class='cu-bar'>
  16. <view class='action iconfont icon-icon_collect'> {{item._name}</view>
  17. </view>
  18. <!--列-->
  19. <block wx:if="{{item.res.length>0}}">
  20. <template is="hotSelling" wx:for="{{item.res}}" wx:key="this" data="{{item}}"></template>
  21. </block>
  22. <block wx:else>
  23. <view style="height: 300rpx;line-height: 300rpx;text-align: center;color:gray;">本类目无</view>
  24. </block>
  25. </view>
  26. </scroll-view>
  27. </view>
  28. </view>

2.test.wxss

  1. .VerticalBox {display: flex;width: 100vw;}
  2. .VerticalNav.nav {width: 200rpx;white-space: initial;}
  3. .VerticalNav.nav .cu-item {width: 100%;text-align: center;background-color: #fff;margin: 0;border: none;height: 50px;line-height: 50px;position: relative;}
  4. .VerticalNav.nav .cu-item.cur {background-color: #f1f1f1;}
  5. .text-green {color: #39b54a;font-weight: bold;}
  6. .VerticalNav.nav .cu-item.cur::after {content: ''; width: 18rpx;height: 40rpx;border-radius: 10rpx 0 0 10rpx;position: absolute;background-color: currentColor;top: 0;right: 0rpx;bottom: 0;margin: auto;}
  7. .VerticalMain {background-color: #f1f1f1;}
  8. .padding-top {padding-top: 30rpx;}
  9. .padding-lr {padding-left: 20rpx;padding-right: 20rpx;}
  10. .cu-bar {display: flex;position: relative;align-items: center;min-height: 100rpx;justify-content: space-between;position: relative;background-color: white;color: #666666;margin-bottom: 2rpx;background-image: linear-gradient(rgb(0, 180, 230), rgb(250, 250, 250));}
  11. .cu-bar .action {display: flex;align-items: center;height: 100%;justify-content: center;max-width: 100%;}

3.test.js

  1. //*******************************************************
  2. //* 微信版蚂蚁森林上线了!微信搜蚂蚁森林立即体验! *
  3. //*******************************************************
  4. // 微信版蚂蚁森林:https://developers.weixin.qq.com/community/personal/oCJUswzZJO5lZcMDd3mKoDAClVdo
  5. const app = getApp()
  6. Page({
  7. data: {
  8. TabCur: 0, //当前点击Tab
  9. titleCur: 0,//标题指引
  10. navTop: 0,
  11. load: true,
  12. dRes: [],
  13. },
  14. tabSelect(e) {
  15. let i = Number(e.currentTarget.dataset.id)
  16. this.setData({
  17. TabCur: i,
  18. titleCur: i,
  19. navTop: (i - 1) * 50
  20. })
  21. },
  22. VerticalMain(e) {
  23. let self= this;
  24. let dRes = this.data.dRes;
  25. let tabHeight = 0;
  26. if (this.data.load) {
  27. for (let i = 0; i < dRes.length; i++) {
  28. let view = wx.createSelectorQuery().select("#main-" + i);
  29. view.fields({
  30. size: true
  31. }, data => {
  32. dRes[i].top = tabHeight;
  33. tabHeight = tabHeight + data.height;
  34. dRes[i].bottom = tabHeight;
  35. }).exec()
  36. }
  37. self.setData({
  38. load: false,
  39. dRes: dRes
  40. })
  41. }
  42. let scrollTop = e.detail.scrollTop + 20;
  43. for (let i = 0; i < dRes.length; i++) {
  44. if (scrollTop > dRes[i].top && scrollTop < dRes[i].bottom) {
  45. that.setData({
  46. navTop: (i - 1) * 50,
  47. TabCur: i
  48. })
  49. return false
  50. }
  51. }
  52. }
  53. })
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/247371
推荐阅读
相关标签
  

闽ICP备14008679号