微信小程序 Utils丨Scrollview 實(shí)現(xiàn)橫向滑動(dòng)時(shí)間選擇器

運(yùn)行效果

涵蓋全 24 個(gè)時(shí)段,左右滑動(dòng)可見其它。當(dāng)前時(shí)段提示為【搶購(gòu)進(jìn)行中】,之前時(shí)段為【已開搶】,之后時(shí)段為【即將開始】


JS

  /**
   * 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
   */
  onLoad: function (options) {
    that = this;
    that.setData({
      timeList: that.initTimeList(24)
    })
  },

  /**
   * 時(shí)段數(shù)組生成
   * @param itemNum 需要的時(shí)段數(shù)量
   * 
   * return 生成的完整數(shù)組
  */
  initTimeList:function(itemNum){
    // 基礎(chǔ)判斷
    if (itemNum <= 0){
      console.log(' Error From initTimeList():所需時(shí)段數(shù)不可小于等于零')
      return []
    }

    // 當(dāng)前時(shí)段
    var nowTime = new Date().getHours()

    // 組裝數(shù)組
    var timeList = []
    for (var t = 0; t < itemNum ; t++){
      t > 9 ? (timeList.push({ 'index': t, 'time': t + ':00', 'hint': (t == nowTime ? '搶購(gòu)進(jìn)行中' : (t > nowTime ? '即將開始' : '已開搶')) })) : (timeList.push({ 'index': t, 'time': '0' + t + ":00", 'hint': (t == nowTime ? '搶購(gòu)進(jìn)行中' : (t > nowTime ? '即將開始' : '已開搶')) }))
    }
    return timeList
  },

  /**
   * 時(shí)間選擇器列表點(diǎn)擊監(jiān)聽
   * @param item 被點(diǎn)擊的item對(duì)象,包含所有信息
  */
  clickItem:function(item){
    // 列表點(diǎn)擊事件
    console.log(item.currentTarget.dataset.item.index)
  }


WXML

<scroll-view scroll-x class="scroll-x">
  <view wx:for="{{timeList}}" wx:key="{{index}}" class="view_item" >
    <view class="view_item_time" bindtap="clickItem" data-item="{{item}}">{{item.time}}</view>
    <view class="view_item_hint" bindtap="clickItem" data-item="{{item}}">{{item.hint}}</view>
  </view>
</scroll-view>

WXSS

.scroll-x{
    white-space:nowrap;
    display:flex;
    background: #333;
}

.view_item{
    display:inline-block;
    padding: 20rpx;
}

.view_item_time{
    width:100rpx;
    height:50rpx;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:0.8rem;
    color:#FFFFFF;
    background:#000;
}

.view_item_hint{
    width:100rpx;
    height:50rpx;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:0.5rem;
    color:#FFFFFF;
    background:#000;
}

/* 隱藏scrollbar */
::-webkit-scrollbar{
    width: 0;
    height: 0;
    color: transparent;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容