小程序中简单Tab切换

微信   2024-12-25 13:35   70   0  

20166_wvie_1067.png

index.wxml

<view>
  <block wx:for="{{data}}" wx-key="index" class="list_top">
    <view data-index="{{index}}" class="list_one {{index==currentIndex?'active':''}}" bindtap="handtab">{{item.name}}
    </view>
  </block>
  <block wx:for="{{shopList}}" wx:key="index">
    <view class="shopList_box" bindtap="handDetail" data-id="{{item.goods_id}}" wx:if="{{status==0}}">
    <!-- 在wxml中使用wx:if status == 0来改变tab切换下面的部分显示与隐藏 -->
      <view class="shopList_left">
        <image class="productpic" src="{{item.goods_big_logo}}"></image>
      </view>
      <view class="shopList_right"> <text class="right">{{item.goods_name}}</text>
        <text class="price">¥{{item.goods_price}}</text>
      </view>
    </view>
  </block>
  <block>
  status==1的情况
    <view wx:if="{{status==1}}">2</view>
  </block>
  <block>
  status==2的情况
    <view wx:if="{{status==2}}">3</view>
  </block>
</view>


index.js

Page({
  data: {
    status: 0,
    data:[
      {name:'电脑科技产品',},
      {name:'手机通讯产品'},
      {name:'移动存储设备'}
    ],
    shopList:[
      {goods_price:10,goods_big_logo:"http://php.szhot.com/main/design/style/rz-2.gif"},
      {goods_price:10,goods_big_logo:"http://php.szhot.com/main/design/style/rz-2.gif"},
      {goods_price:10,goods_big_logo:"http://php.szhot.com/main/design/style/rz-2.gif"}
    ]
  },
  handtab(e) {
    console.log(e);
    let index = e.currentTarget.dataset.index
    console.log(index);
    this.setData({
      currentIndex: index,
      status: e.currentTarget.dataset.index
      //在点击切换时利用status来切换根据index下标来实现
    })
  } 
})

index.wxss

.list_one{
  float: left;
  margin-left: 15px;
  width:100px;
  height: 25px;
  background-color: rgb(210, 223, 238);
}
.productpic{
  width:100px;
  height: 100px;
}


来源:https://blog.csdn.net/weixin_57998947/article/details/116670915


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。