微信小程序 tabs选项卡效果的实现

微信小程序 tabs选项卡效果

前言:

最近微信应用号是炒的如火如荼,热门满满,但是也可以发现搜索关键词出来,各类网站出现的还都是微信的官方文档解释。正好赶上这个热潮,这几天先把小程序技术文档看了个遍,就直接着手写案例了。很多组件微信内部已经封装完了,正好发现没有tab选项卡效果,这两天正好研究了下。思路如下:

1.首先点击导航的时候需要两个变量,一个存储当前点击样式类,一个是其它导航默认的样式类

2.选项卡内容列表同样也需要两个变量,一个存储当前显示块,一个存储的是其它隐藏的默认块

3.使用三目运算通过点击获取导航索引,根据索引判断是否添加当前类【备注,这里我将点击事件绑定在父级导航栏,通过target对象得到点击触发的事件对象属性】

请结合如下效果图:

微信小程序 tabs选项卡效果的实现

接下来直接查看源码:

demo.wxml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<view class= "tab" >
<view class= "tab-left"  bindtap= "tabFun" >
  <view class= "{{tabArr.curHdIndex=='0'? 'active' : ''}}"  id= "tab-hd01"  data-id= "0" >tab-hd01</view>
  <view class= "{{tabArr.curHdIndex=='1'? 'active' : ''}}"  id= "tab-hd02"  data-id= "1" >tab-hd01</view>
  <view class= "{{tabArr.curHdIndex=='2'? 'active' : ''}}"  id= "tab-hd03"  data-id= "2" >tab-hd01</view>
  <view class= "{{tabArr.curHdIndex=='3'? 'active' : ''}}"  id= "tab-hd04"  data-id= "3" >tab-hd01</view>
</view>
  
<view class= "tab-right" >
  <view class= "right-item {{tabArr.curBdIndex=='0'? 'active' : ''}}" >tab-bd01</view>
  <view class= "right-item {{tabArr.curBdIndex=='1'? 'active' : ''}}" >tab-bd02</view>
  <view class= "right-item {{tabArr.curBdIndex=='2'? 'active' : ''}}" >tab-bd03</view>
  <view class= "right-item {{tabArr.curBdIndex=='3'? 'active' : ''}}" >tab-bd04</view>
</view>
</view>


demo.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Page( {
data: {
  tabArr: {
   curHdIndex: 0,
   curBdIndex: 0
  },
},
tabFun:  function (e){
  //获取触发事件组件的dataset属性
  var  _datasetId=e.target.dataset.id;
  console.log( "----" +_datasetId+ "----" );
  var  _obj={};
  _obj.curHdIndex=_datasetId;
  _obj.curBdIndex=_datasetId;
  this .setData({
   tabArr: _obj
  });
},
onLoad:  function ( options ) {
  alert(  "------"  );
}
});

demo.wxss:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
.tab{
  display: flex;
  flex-direction: row;
}
.tab-left{
  width: 200rpx;
  line-height: 160%;
  border-right: solid 1px gray;
}
.tab-left view{
  border-bottom: solid 1px red;
}
.tab-left .active{
  color:  #f00;
}
.tab-right{
  line-height: 160%;
}
.tab-right .right-item{
  padding-left: 15rpx;
  display: none;
}
.tab-right .right-item.active{
  display: block;
}

最终演示效果如下:

微信小程序 tabs选项卡效果的实现

微信小程序 tabs选项卡效果的实现

感谢阅读

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值