微信小程序 tab切换展示不同的内容

index.wxml

<view class='shopDetail-box'>
  <view class="tab-left" >
    <view class="{{tabArr.curHdIndex=='1'? 'active' : ''}}" id="1" data-id="1" bindtap="tab">人身保障</view>
    <view class="{{tabArr.curHdIndex=='2'? 'active' : ''}}" id="2" data-id="2" bindtap="tab">财产保障</view>
    <view class="{{tabArr.curHdIndex=='3'? 'active' : ''}}" id="3" data-id="3" bindtap="tab">健康保障</view>
    <view class="{{tabArr.curHdIndex=='4'? 'active' : ''}}" id="4" data-id="4" bindtap="tab">养老保障</view>
  </view>

  <view class="tab-right">
    <view class="right-item {{tabArr.curBdIndex=='1'? 'active' : ''}}">
   1
    </view>
    <view class="right-item {{tabArr.curBdIndex=='2'? 'active' : ''}}">
     2
    </view>
    <view class="right-item {{tabArr.curBdIndex=='3'? 'active' : ''}}">
     3
    </view>
    <view class="right-item {{tabArr.curBdIndex=='4'? 'active' : ''}}">
     4
    </view>
  </view>
</view>

index.wxss

.fs18{
  font-size: 18px;
}
.fs16{
  font-size: 16px;
}
.fs14{
  font-size: 14px;
}
.fs12{
  font-size: 12px;
}
.mb10{
  margin-bottom: 10px;
}
.shopDetail-box{
  padding-top: 55px;
}
.tab-left .active {
  color: #fc5b13;
  border-bottom: 2px solid #fc5b13;
}

.tab-left {
  background: #fff;
  position: fixed;
  z-index: 22;
  top: 0;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  color: #333;
  border-bottom: 1px solid #d5d5d5;
}

.tab-left view {
  width: 30%;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tab-right .right-item {
  display: none;
  height: 300rpx;
  width: 100%;
  /* border: solid 1px; */
}

.tab-right .right-item.active {
  display: block;
}

.shopDetail-bottom{
  position: absolute;
  left: 15px;
  bottom: 15px;
  color: #fff;
}

index.js

Page({
  data: {
    // tab 切换
    tabArr: {
      curHdIndex: 1,
      curBdIndex: 1
    }
  },
  //tab切换
  tab: function (e) {
    //var dataId = e.currentTarget.dataset.id;
    var dataId = e.currentTarget.id;
    var obj = {};
    obj.curHdIndex = dataId;
    obj.curBdIndex = dataId;
    this.setData({
      tabArr: obj
    })
    console.log(obj);
  },


})

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
微信小程序tab切换时下划线动画效果可以通过使用动画组件和样式设置来实现。首先,在wxml文件中,我们可以使用 `<view>` 元素来创建一个下划线,并给它一个唯一的id,例如: ```html <view id="underline"></view> ``` 然后,在wxss文件中,我们可以为这个下划线设置初始样式,例如设置宽度、高度、颜色和定位等属性。 ```css #underline { position: absolute; bottom: 0; height: 2rpx; background-color: #FF0000; /* 设置下划线的颜色 */ width: 100rpx; /* 设置初始宽度 */ transition: all 0.3s ease-out; /* 设置过渡动画效果 */ } ``` 接下来,在js文件中,我们可以通过监听tab切换的事件来改变下划线的位置和宽度,以实现动画效果。 ```javascript Page({ data: { activeTab: 1 // 当前活动的tab索引 }, onTabChange(event) { const { index } = event.detail; const query = wx.createSelectorQuery().in(this); query .select(`#tab-${index}`) .boundingClientRect(rect => { const underline = wx.createSelectorQuery().select('#underline'); underline .boundingClientRect(underlineRect => { underline .scrollOffset(res => { const left = rect.left + res.scrollLeft; const width = rect.width; // 更新下划线的位置和宽度 this.setData({ underlineLeft: `${left}px`, underlineWidth: `${width}px`, activeTab: index }); }) .exec(); }) .exec(); }) .exec(); } }); ``` 最后,在wxml文件中,我们可以动态绑定下划线的位置和宽度属性,实现动态显示效果。 ```html <view class="tab" id="tab-0" bindtap="onTabChange" data-index="0">Tab 1</view> <view class="tab" id="tab-1" bindtap="onTabChange" data-index="1">Tab 2</view> <view class="tab" id="tab-2" bindtap="onTabChange" data-index="2">Tab 3</view> <view id="underline" style="left: {{underlineLeft}}; width: {{underlineWidth}};"></view> ``` 通过以上代码,我们可以实现微信小程序tab切换时下划线的动画效果。当切换不同tab时,下划线会通过动画效果实现平滑的移动和改变宽度的效果,从而提升用户的交互体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值