欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序之 catalog 切換實現解析

 更新時間:2019年09月12日 10:47:39   作者:禮拜16  
這篇文章主要介紹了微信小程序之 catalog 切換實現解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

組件名稱:catalog

組件屬性:catalogData,type:String

組件描述:這是一個子組件,數據從父組件中傳遞

效果圖:

catalog 目錄為多個,使用 scroll-view 容器。但是在當前組件中,只寫每個 catalog 數據展示并實現 catalog 切換效果,這樣會保證當前組件的最小粒度。

分析當前組件可能用到的屬性為:組件名稱,切換狀態(tài),未選中狀態(tài),切換點擊方法。

<block wx:for="{{catalogData}}" wx:key="id" wx:for-item="item" class="item">
  <view class="scroll-view-item catalog-title {{curIndex == index ? 'catalog-active' : 'catalog-normal'}}" data-index="{{index}}" catchtap="goIndex">{{item.catalog}}</view>
</block>

curIndex 為點擊當前 catalog 時獲取的 index

index 為 catalogData 這個數組默認的 index, 也就是 data-index 的值

goIndex 為點擊當前 catalog 的方法,通過點擊獲取到 curIndex 的值

通過判斷當前 curIndex 是否與數組中的 index 是否相等,如果相等,則給當前 catalog 添加選中的樣式 catalog-active,否則添加 catalog-normal。

更新默認 curIndex 的默認值

Component({
 /**
  * 組件的屬性列表
  */
 properties: {
  catalogData:{
   type: Array,
  }
 },
 /**
  * 組件的初始數據
  */
 data: {
   curIndex:0
 },  
  /**
   * 組件的方法列表
   */
  methods: {
    goIndex(event) {
      let nextIndex = event.currentTarget.dataset.index;
      this.setData({
        curIndex: nextIndex
      })      
      console.log(this.data.curIndex + '=' + nextIndex);
    },
  }
})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論