根据id来实现小程序tab切换,

本例根据绑定id来实现tab切换,但本例仍有缺陷,用for循环数据,无法实现切换。如有大神能够有更好方法,欢迎留言更正

WXML:

<view class="tab"> 
  <view bindtap="tabs" class="{{tabArr.curHdIndex=='0'? 'active' : ''}}" id="0" data-id="0">tab-hd01</view> 
  <view bindtap="tabs" class="{{tabArr.curHdIndex=='1'? 'active' : ''}}" id="1" data-id="1">tab-hd02</view> 
  <view bindtap="tabs" class="{{tabArr.curHdIndex=='2'? 'active' : ''}}" id="2" data-id="2">tab-hd03</view> 
  <view bindtap="tabs" class="{{tabArr.curHdIndex=='3'? 'active' : ''}}" id="3" data-id="3">tab-hd04</view> 
</view> 
 
<view class="tabcon"> 
  <view class="{{tabArr.curBdIndex=='0'? 'active' : ''}}">tab-bd01111</view> 
  <view class="{{tabArr.curBdIndex=='1'? 'active' : ''}}">tab-bd02222</view> 
  <view class="{{tabArr.curBdIndex=='2'? 'active' : ''}}">tab-bd03333</view> 
  <view class="{{tabArr.curBdIndex=='3'? 'active' : ''}}">tab-bd04444</view> 
</view>

WXSS:

.tab{
  display: flex;
  flex-direction:row; 
  height: 50px;
  line-height: 50px;

}
.tab view{
  width: 25%;
  text-align: center;
}
.tab .active{
  display: inline-block;
  color: #188eee;
  border-bottom: 1px #188eee solid;
}
.tabcon view{
  display: none;
}
.tabcon .active{
  display: inline-block;
}

JS

Page({
  data: {
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0,
    }
  },
  tabs: function (e) {
    var dataId = e.currentTarget.id;
    var obj = {};
    obj.curHdIndex = dataId;
    obj.curBdIndex = dataId;
    
    this.setData({
      tabArr: obj
    }); 
  }
});

  

微信小程序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时,下划线会通过动画效果实现平滑的移动和改变宽度的效果,从而提升用户的交互体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值