運(yùn)行效果
涵蓋全 24 個(gè)時(shí)段,左右滑動(dòng)可見(jiàn)其它。當(dāng)前時(shí)段提示為【搶購(gòu)進(jìn)行中】,之前時(shí)段為【已開(kāi)搶】,之后時(shí)段為【即將開(kāi)始】
JS
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(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 ? '即將開(kāi)始' : '已開(kāi)搶')) })) : (timeList.push({ 'index': t, 'time': '0' + t + ":00", 'hint': (t == nowTime ? '搶購(gòu)進(jìn)行中' : (t > nowTime ? '即將開(kāi)始' : '已開(kāi)搶')) }))
}
return timeList
},
/**
* 時(shí)間選擇器列表點(diǎn)擊監(jiān)聽(tīng)
* @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;
}