本文主要介绍了如何实现小程序tab栏下划线动画效果,分享给大家,具体如下:
最终效果
实现
wxml
<view class='abox'> <view wx:for="{{title}}" class='{{currentindex==index?"tabtrue":""}}' bindtap='changetab' data-aa='{{index}}'> {{item}} </view> <view class='b' style="left:{{left}}px"></view> </view>
wxss
.abox{ display: flex; flex-direction: row; width: 100%; justify-content: space-around; position: relative; padding-bottom: 20rpx; } .tabtrue{ color: red; } .b{ background: red; height: 4rpx; width:64rpx; position: absolute; bottom: 0; transition: all .3s linear; }
js
page({ data: { title: ["首页","掘金","思否","知乎"], currentindex:"0", left:"" }, changetab:function(e){ //console.log(e) this.setdata({ currentindex: e.currenttarget.dataset.aa }) this.changeline(e) }, changeline:function(){ const query = wx.createselectorquery() var _this = this query.select('.tabtrue').boundingclientrect() query.exec(function (res) { _this.setdata({ left: res[0].left }) //console.log(res[0].left) }) }, onload: function () { this.changeline(1) } })
上面代码可以实现效果,这里面最重要的就是通过 changetab方法获取有tabtrue这个class的标签,获取到标签的left值。