首页 > 编程语言 >微信小程序商城分类滚动列表锚点(左菜单右列表)

微信小程序商城分类滚动列表锚点(左菜单右列表)

时间:2023-10-11 18:12:31浏览次数:34  
标签:box goods name 微信 list 列表 item 锚点 来来来

1、TestScroll.wxml

<view class="content-box">
  <!-- 左侧分类 -->
  <scroll-view scroll-y scroll-with-animation class="left" style="width:245rpx;height:100%;background: #f2f2f2;" scroll-into-view='{{leftId}}'>
    <view id='left{{index}}' class="left-item {{activeKey===index?'active':''}}" wx:for="{{navData}}" data-index='{{index}}' wx:key='id' bindtap="onChange">
      <text class='name'>{{item.name}}</text>
    </view>
  </scroll-view>
  <!-- 滚动列表 -->
  <scroll-view class="right" scroll-y scroll-with-animation scroll-into-view="{{selectedId}}" bindscroll="changeScroll" style='height:100%;'>
    <!-- 每个分类 -->
    <view class="item subtitle" wx:for="{{goodslist}}" wx:key="id" id='type{{index}}'>
      <!-- 分类标题 -->
      <view class="type">【{{item.name}}】</view>
      <!-- 分类下的商品 -->
      <view class="item-list">
        <navigator class="list-item" wx:for="{{item.list}}" wx:for-item='key' wx:key="id">
          <!-- <image style="width: 100%; height: 180rpx;" src="{{key.imgurl}}" /> -->
          <view class="item-name">{{key.goods_name}}</view>
        </navigator>
      </view>
      <view wx:if="{{item.list.length===0}}" class="nodata">
        暂无商品
      </view>
    </view>
  </scroll-view>
</view>

2、TestScroll.wxss

.content-box {
  width: 100%;
  height: calc(100% - 108rpx);
  overflow-y: hidden;
  display: flex;
}
.content-box .left {
  height: 100%;
  overflow-y: scroll;
}
.content-box .left .left-item {
  width: 100%;
  padding: 20rpx;
  box-sizing: border-box;
}
.content-box .left .left-item .name {
  word-wrap: break-word;
  font-size: 28rpx;
  color: #323233;
}
.content-box .left .active {
  border-left: 6rpx #ee0a24 solid;
  background-color: #fff;
}
.content-box .right {
  flex: 1;
}
.content-box .right .item {
  position: relative;
  padding: 20rpx;
  background: #fff;
}
.content-box .right .item .type {
  padding: 5rpx;
  position: sticky;
  top: 0;
  background-color: aliceblue;
  z-index: 1;
}
.content-box .right .item .item-list {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 20rpx;
  text-align: center;
}
.content-box .right .item .item-list .item-name {
  color: #3a3a3a;
  font-size: 26rpx;
  margin-top: 10rpx;
}
.content-box .right .item .nodata {
  padding: 20rpx;
  color: #ccc;
}

3、TestScroll.js

 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 分类列表
    navData: [
      { name: "奔驰C" },
      { name: "奔驰E" },
      { name: "雷克萨斯E200" },
      { name: "雷克萨斯E300" },
      { name: "宝马3系" },
      { name: "宝马5系" },
      { name: "奥迪Q5" },
      { name: "奥迪A6" },
      { name: "奥迪A8" },
      { name: "凯迪拉克CT4" },
      { name: "凯迪拉克CT5" },
      { name: "凯迪拉克CT6" },
      { name: "凯美瑞" },
      { name: "亚洲龙" },
      { name: "雅阁" },
      { name: "英诗派" },
    ],
    // 商品列表
    goodslist: [
      { 
        name: "奔驰C",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
        ]
      },
      { 
        name: "奔驰E",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "雷克萨斯E200",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
          { goods_name: "来来来6" },
          { goods_name: "来来来7" },
        ]
      },
      { 
        name: "雷克萨斯E300",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "宝马3系",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
        ]
      },
      { 
        name: "宝马5系",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "奥迪Q5",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
          { goods_name: "来来来6" },
          { goods_name: "来来来7" },
          { goods_name: "来来来8" },
          { goods_name: "来来来9" },
          { goods_name: "来来来10" },
        ]
      },
      { 
        name: "奥迪A6",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "奥迪A8",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "凯迪拉克CT4",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
          { goods_name: "来来来6" },
          { goods_name: "来来来7" },
          { goods_name: "来来来8" },
        ]
      },
      { 
        name: "凯迪拉克CT5",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "凯迪拉克CT6",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
          { goods_name: "来来来6" },
          { goods_name: "来来来7" },
          { goods_name: "来来来8" },
          { goods_name: "来来来9" },
          { goods_name: "来来来10" },
          { goods_name: "来来来11" },
          { goods_name: "来来来12" },
          { goods_name: "来来来13" },
          { goods_name: "来来来14" }
        ]
      },
      { 
        name: "凯美瑞",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "亚洲龙",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
          { goods_name: "来来来4" },
          { goods_name: "来来来5" },
          { goods_name: "来来来6" },
          { goods_name: "来来来7" },
          { goods_name: "来来来8" },
          { goods_name: "来来来9" },
          { goods_name: "来来来10" }
        ]
      },
      { 
        name: "雅阁",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
      { 
        name: "英诗派",
        list: [
          { goods_name: "来来来1" },
          { goods_name: "来来来2" },
          { goods_name: "来来来3" },
        ]
      },
    ],
    // 左侧分类选中项 分类列表数组的下标
    activeKey: 0,
    // 计算出的锚点的位置
    heightList: [],
    // 右侧子元素的锚点
    selectedId: 'type0',
    // 左侧分类的锚点
    leftId: 'left0',
  },
  onShow() {
    this.selectHeight()
  },
  // 计算右侧每个锚点的高距离顶部的高
  selectHeight() {
    var _that = this;
    var list = [];
    const query = wx.createSelectorQuery();
    query.selectAll('.subtitle').boundingClientRect()
    query.exec((res) => {
      console.log("res-----",res[0]);
      res[0].forEach((item) => {
        list.push(item.bottom)
      })
      _that.setData({ heightList: list })
      console.log('heightList-------',list);
    })
  },
  changeScroll(e) {
    // 获取距离顶部的距离
    let scrollTop = e.detail.scrollTop;
    // 当前分类选中项,分类列表下标
    let { activeKey, heightList } = this.data;
    console.log('scrollTop-------',scrollTop);
    // 防止超出分类              判断滚动距离是否超过当前分类距离顶部高度
    if (activeKey + 1 < heightList.length && scrollTop >= heightList[activeKey]) {
      this.setData({
        // 左侧分类选中项改变
        activeKey: activeKey + 1,
        // 左侧锚点对应位置
        leftId: `left${activeKey + 1}`
      })
    }
    if (activeKey - 1 >= 0 && scrollTop < heightList[activeKey - 1]) {
      this.setData({
        activeKey: activeKey - 1,
        leftId: `left${activeKey - 1}`
      })
    }
  },
  onChange(event) {
    let index = event.currentTarget.dataset.index
    this.setData({
      activeKey: index,
      selectedId: "type" + index
    });
  },
})

 

  

 

标签:box,goods,name,微信,list,列表,item,锚点,来来来
From: https://www.cnblogs.com/btsn/p/17757865.html

相关文章

  • 第三章:列表
    点击查看代码#列表bicycles=['trek','cannodale','redline','specialized']print(bicycles)#访问元素print(bicycles[0].title())#第一个print(bicycles[-1].title())#倒数第一个print(f"Mybicycleisa{bicycles[-2].title(......
  • 微信浏览器环境H5使用扫一扫功能
    一、需求:1、微信浏览器环境H5使用扫一扫功能=>方案:引入wxjssdk,然后获取公众号的签名信息;再在config中配置;配置好了就能使用扫一扫api了2、短信链接内部实现扫一扫功能=>方案:使用inputaccpet=“image/*”,读取图片区识别,识别二维码数据后发送给后台|-->由于手机浏览器兼容性问......
  • 关于微信小程序VM22:2 (in promise) MiniProgramError {“errMsg“:“hideLoading:fai
    参考地址:https://blog.csdn.net/qq_41227106/article/details/108465104 出现错误的原因如下1、是微信小程序2、把请求接口统一封装,开始请求接口时showLoading,请求接口后hideLoading3、一个页面同时请求多个接口,由于请求是异步的,很有可能上一个开启了showLoading还没请求完......
  • DTD列表
    1.DTD为英文DocumentTypeDefinition,中文意思为“文档类定义”。DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示代码。2.一个DTD应该放在每一个文档的第一行(包括空白).这样正确地放置,你的DTD才能告诉浏览器的用的是什么标......
  • struts2+hibernate+spring+jquery返回json List列表
    1.引入包:struts2-json-plugin-2.1.8.1.jarjson-lib-2.1.jarcommons-collections-3.2.1.jarcommons-beanutils-1.8.2.jarcommons-lang-2.4.jarezmorph-1.0.6.jar,其他的包略,这几个包是返回json形式的数据必须的2.<packagename="default"extends="js......
  • PHP 微信v3 敏感信息解密
    protectedstaticfunctiongetDecrypt($encrypted){$private_key=config('wx.merchantPrivateKeyFilePath');//商户私钥如果是路劲,可以使用file_get_content来获取$pi_key=openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是......
  • PHP 微信v3 敏感信息加密
    protectedstaticfunctiongetEncrypt($str){$public_key=config('wx.platformCertificateFilePath');//平台证书,如果是地址,可以使用file_get_content来获取内容$pu_key=openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可......
  • 微信小程序开发环境搭建
    浏览器打开https://mp.weixin.qq.com/ 立即注册,点击小程序    填写账号和密码 采用邮箱激活   用户主体           下载开发工具:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 安装之后,扫码,      ......
  • 低代码助力微信小程序对接,提升开发效率
    摘要:本文由葡萄城技术团队原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。前言微信小程序相信大家都用过,相较于APP,微信小程序的优势在于其便捷性,只需要下载一个微信就可以访问所有的小程序,因此许多开发者也逐渐将自己开发......
  • element ui 列表页 新增修改查看表单
    列表页直接操作删除,新增修改查看用一个子组件表单实现列表<template><divclass="mod-configml10"><!--搜索--><divclass="searchsearchHead"v-show="showSearch"><el-formref="searchForm":model="se......