本文實(shí)例為大家分享了微信小程序實(shí)現(xiàn)批量倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
實(shí)現(xiàn)思路
微信小程序?qū)崿F(xiàn)倒計(jì)時(shí),可以將倒計(jì)時(shí)的時(shí)間進(jìn)行每一秒的計(jì)算和渲染!
JS
倒計(jì)時(shí)函數(shù)詳解
let goodsList = [ {actEndTime: '2018-05-01 10:00:43'}, {actEndTime: '2018-04-01 11:00:00'}, {actEndTime: '2018-06-01 12:45:56'}, {actEndTime: '2018-07-01 15:00:23'}, {actEndTime: '2018-05-23 17:00:22'}, {actEndTime: '2018-05-14 19:00:44'}, {actEndTime: '2018-05-21 21:00:34'}, {actEndTime: '2018-06-17 09:00:37'}, {actEndTime: '2018-03-21 05:00:59'}, {actEndTime: '2018-04-19 07:00:48'}, {actEndTime: '2018-04-28 03:00:11'}]Page({ data: { countDownList: [], actEndTimeList: [] }, onLoad(){ let endTimeList = []; // 將活動(dòng)的結(jié)束時(shí)間參數(shù)提成一個(gè)單獨(dú)的數(shù)組,方便操作 goodsList.forEach(o => {endTimeList.push(o.actEndTime)}) this.setData({ actEndTimeList: endTimeList}); // 執(zhí)行倒計(jì)時(shí)函數(shù) this.countDown(); }, timeFormat(param){//小于10的格式化函數(shù) return param < 10 ? '0' + param : param; }, countDown(){//倒計(jì)時(shí)函數(shù) // 獲取當(dāng)前時(shí)間,同時(shí)得到活動(dòng)結(jié)束時(shí)間數(shù)組 let newTime = new Date().getTime(); let endTimeList = this.data.actEndTimeList; let countDownArr = []; // 對(duì)結(jié)束時(shí)間進(jìn)行處理渲染到頁(yè)面 endTimeList.forEach(o => { let endTime = new Date(o).getTime(); let obj = null; // 如果活動(dòng)未結(jié)束,對(duì)時(shí)間進(jìn)行處理 if (endTime - newTime > 0){ let time = (endTime - newTime) / 1000; // 獲取天、時(shí)、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hou = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: this.timeFormat(hou), min: this.timeFormat(min), sec: this.timeFormat(sec) } }else{//活動(dòng)已結(jié)束,全部設(shè)置為'00' obj = { day: '00', hou: '00', min: '00', sec: '00' } } countDownArr.push(obj); }) // 渲染,然后每隔一秒執(zhí)行一次倒計(jì)時(shí)函數(shù) this.setData({ countDownList: countDownArr}) setTimeout(this.countDown,1000); }})
WXML
簡(jiǎn)單的布局和居中顯示。
<view class='tui-countdown-content' wx:for="{{countDownList}}" wx:key="countDownList"> 剩余 <text class='tui-conutdown-box'>{{item.day}}</text>天 <text class='tui-conutdown-box'>{{item.hou}}</text>時(shí) <text class='tui-conutdown-box'>{{item.min}}</text>分 <text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>秒</view>
WXSS
page{background-color: #eee;}.tui-countdown-content{ height: 50px; line-height: 50px; text-align: center; background-color: #fff; margin-top: 15px; padding: 0 15px; font-size: 18px;}.tui-conutdown-box{ display: inline-block; height: 26px; width: 26px; line-height: 26px; text-align: center; background-color: #000; color: #fff; margin: 0 5px;}.tui-countdown-bg{ background-color: #DF0101;}
實(shí)際應(yīng)用效果圖
由于活動(dòng)已結(jié)束,所以時(shí)間全部為'00'
總結(jié)
由于微信小程序的數(shù)據(jù)處理比HTML的更加方便快捷,所以在實(shí)現(xiàn)相同效果的時(shí)候需要對(duì)應(yīng)的轉(zhuǎn)化一下思路,可能更好的解決問(wèn)題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注