微信小步调实现左侧商品范例选择,右边表现商品列表的效果?

[复制链接]
发表于 2025-10-30 19:17:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在微信小步调中实现一个左侧为商品范例选择,右侧表现商品列表的效果,通常涉及到利用 scroll-view、view、swiper 等组件来布局和展示内容。下面是一个简单的实现思绪和代码示例:
  计划布局

我们可以利用一个 scroll-view 包裹左侧的商品范例列表,右侧用一个 scroll-view 或 view 来展示商品列表。为了实现联动效果,可以在点击商品范例时更新右侧的商品列表。
根本布局

  1. <view class="container">
  2.   <!-- 左侧商品类型选择 -->
  3.   <scroll-view class="left-side" scroll-y="true" scroll-with-animation>
  4.     <view class="item" wx:for="{
  5.    
  6.     {categories}}" wx:key="index" bindtap="onCategoryChange" data-category="{
  7.    
  8.     {item}}">
  9.       {
  10.   
  11.   {item.name}}
  12.     </view>
  13.   </scroll-view>
  14.   <!-- 右侧商品列表 -->
  15.   <scroll-view class="right-side" scroll-y="true" scroll-with-animation>
  16.     <view class="product" wx:for="{
  17.    
  18.     {products}}" wx:key="index">
  19.       <image src="{
  20.    
  21.     {item.image}}" class="product-image" />
  22.       <view class="product-info">
  23.         <text>{
  24.   
  25.   {item.name}}</text>
  26.         <text>{
  27.   
  28.   {item.price}}</text>
  29.       </view>
  30.     </view>
  31.   </scroll-view>
  32. </view>
复制代码
页面样式

  1. .container {
  2.    
  3.    
  4.   display: flex;
  5. }
  6. .left-side {
  7.    
  8.    
  9.   width: 200rpx;
  10.   height: 100%;
  11.   background-color: #f5f5f5;
  12. }
  13. .right-side {
  14.    
  15.    
  16.   flex: 1;
  17.   padding: 20rpx;
  18. }
  19. .item {
  20.    
  21.    
  22.   padding: 10rpx;
  23.   text-align: center;
  24.   background-color: #ffffff;
  25.   margin-bottom: 10rpx;
  26.   border-radius: 5rpx;
  27. }
  28. .product {
  29.    
  30.    
  31.   display: flex;
  32.   flex-direction: column;
  33.   align-items: center;
  34.   margin-bottom: 20rpx;
  35. }
  36. .product-image {
  37.    
  38.    
  39.   width: 150rpx;
  40.   height: 150rpx;
  41. }
  42. .product-info {
  43.    
  44.    
  45.   text-align: center;
  46. }
复制代码
页面逻辑

在 js 文件中,处置惩罚商品范例的点击事故,并根据选中的范例更新商品列表:
  1. Page({
  2.    
  3.    
  4.   data: {
  5.    
  6.    
  7.     categories: [
  8.       {
  9.    
  10.     id: 1, name: '电子产品' },
  11.       {
  12.    
  13.     id: 2, name: '服装' },
  14.       {
  15.    
  16.     id: 3, name: '家居' },
  17.       // 其他类别...
  18.     ],
  19.     products: [],  // 商品列表
  20.   },
  21.   onLoad: function () {
  22.    
  23.    
  24.     // 初始加载电子产品的商品列表
  25.     this.loadProductsByCategory(1);
  26.   },
  27.   // 处理点击类别,更新商品列表
  28.   onCategoryChange: function (e) {
  29.    
  30.    
  31.     const category = e.currentTarget.dataset.category;
  32.     this.loadProductsByCategory(category.id);
  33.   },
  34.   // 根据类别加载商品
  35.   loadProductsByCategory: function (categoryId) {
  36.    
  37.    
  38.     // 假设这里是从服务器获取数据,示例使用静态数据
  39.     let productList =
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表