小程序模板網(wǎng)

微信小程序?qū)嵗坛蹋簩?shí)現(xiàn)tabs選項(xiàng)卡效果

發(fā)布時(shí)間:2018-03-24 15:22 所屬欄目:小程序開(kāi)發(fā)教程

最近微信應(yīng)用號(hào)是炒的如火如荼,熱門滿滿,但是也可以發(fā)現(xiàn)搜索關(guān)鍵詞出來(lái),各類網(wǎng)站出現(xiàn)的還都是微信的官方文檔解釋。正好趕上這個(gè)熱潮,這幾天先把小程序技術(shù)文檔看了個(gè)遍,就直接著手寫案例了。很多組件微信內(nèi)部已 ...

 
 
 

最近微信應(yīng)用號(hào)是炒的如火如荼,熱門滿滿,但是也可以發(fā)現(xiàn)搜索關(guān)鍵詞出來(lái),各類網(wǎng)站出現(xiàn)的還都是微信的官方文檔解釋。正好趕上這個(gè)熱潮,這幾天先把小程序技術(shù)文檔看了個(gè)遍,就直接著手寫案例了。很多組件微信內(nèi)部已經(jīng)封裝完了,正好發(fā)現(xiàn)沒(méi)有tab選項(xiàng)卡效果,這兩天正好研究了下。思路如下:

1、首先點(diǎn)擊導(dǎo)航的時(shí)候需要兩個(gè)變量,一個(gè)存儲(chǔ)當(dāng)前點(diǎn)擊樣式類,一個(gè)是其它導(dǎo)航默認(rèn)的樣式類

2、選項(xiàng)卡內(nèi)容列表同樣也需要兩個(gè)變量,一個(gè)存儲(chǔ)當(dāng)前顯示塊,一個(gè)存儲(chǔ)的是其它隱藏的默認(rèn)塊

3、使用三目運(yùn)算通過(guò)點(diǎn)擊獲取導(dǎo)航索引,根據(jù)索引判斷是否添加當(dāng)前類【備注,這里我將點(diǎn)擊事件綁定在父級(jí)導(dǎo)航欄,通過(guò)target對(duì)象得到點(diǎn)擊觸發(fā)的事件對(duì)象屬性】

 

請(qǐng)結(jié)合如下效果圖:

 

      接下來(lái)直接查看源碼:

demo.wxml:

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


demo.js:

 

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


demo.wxss:

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


    最終演示效果如下:

 

以上僅是個(gè)人方案,如果有更好的方案,歡迎提出~



本文地址:http://www.u-renovate.com/wxmini/doc/course/22851.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×