您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在小程序中實(shí)現(xiàn)一個(gè)日歷左右滑動(dòng)效果,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
wxml
<scroll-view class="scroll-view" scroll-left="{{scrollLeftIndex*itemWidth}}" scroll-x scroll-with-animation> <view class='item' style='width:{{itemWidth}}px' wx:for="{{dateList}}" data-index='{{index}}' bindtap='clickDate'> <view class='text-view'> <view class='day {{index==clickIndex?"day-active":""}}'>{{item.day}}</view> <text class='month'>{{item.month}}月</text> <view wx:if="{{item.month == sysmonth && item.day == nowDate}}"> <text class='week'>今日</text> </view> <view wx:else> <text class='week'>{{item.week}}</text> </view> </view> </view> </scroll-view>
js
// byjs_sun/pages/product/all/all.js Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { dateList:[], //存放日期的數(shù)組 nowDate:'', //系統(tǒng)當(dāng)前日期 }, // 格式化日期,時(shí)間 formatTime(date) { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':') }, // 格式化數(shù)字 formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n }, // 獲取日期詳情 getDateInfo(ts) { const date = new Date(ts); const weekArr = new Array("日", "一", "二", "三", "四", "五", "六"); const week = date.getDay(); let dateString = this.formatTime(date); let shortDateString = dateString.replace(/\//g, '-').substring(5, 10).replace(/-/g, '月') + "日"; if (date.getDate() < 10) { shortDateString = shortDateString.replace(/0/g, ''); } return { shortDateString, dateString, month: date.getMonth() + 1, day: date.getDate(), week: weekArr[week] } }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 */ onLoad: function (options) { var that = this; var myDate = new Date(); //獲取系統(tǒng)當(dāng)前時(shí)間 var sysmonth = myDate.getMonth() + 1 var nowDate = myDate.getDate(); //當(dāng)前是本月幾日 var today = myDate.toLocaleDateString(); //今日年月日 that.setData({ nowDate: nowDate, sysmonth: sysmonth }), console.log('系統(tǒng)日期:',myDate); console.log('系統(tǒng)日期(年/月/日):',today); console.log('系統(tǒng)日期(月):', sysmonth); console.log('系統(tǒng)日期(日):', nowDate); // 獲取屏幕寬度,設(shè)置每個(gè)日期寬度 wx.getSystemInfo({ success: (res) => { console.log(res); this.setData({ windowWidth: res.windowWidth, itemWidth: parseInt(res.windowWidth / 7) }); }, }) this.initData(); }, // 初始化日期 initData() { const nowDateTime = +new Date(); let dateList = []; for (let i = -30; i < 30; i++) { let obj = this.getDateInfo(nowDateTime + i * 24 * 60 * 60 * 1000); obj.isChoose = i == 0; dateList.push(obj); } this.setData({ dateList, clickIndex: 30, scrollLeftIndex: 30 }); }, // 點(diǎn)擊日期方法 clickDate(e) { var that = this; console.log('點(diǎn)擊日期攜帶的下標(biāo):', e.currentTarget.dataset.index); //當(dāng)前的點(diǎn)擊的日期 var index = e.currentTarget.dataset.index; that.setData({ clickIndex: index }); // console.log(that.data.scrollLeftIndex); console.log('當(dāng)前點(diǎn)擊日期:',that.data.dateList[index].shortDateString); //當(dāng)前點(diǎn)擊的日期 // const { // index // } = e.currentTarget.dataset; // this.setData({ // clickIndex: index // }); // console.log(this.data.dateList[index]); }, })
樣式
.scroll-view { height: 200rpx; background-color: #313751; width: 100%; white-space: nowrap; } .item { height: 200rpx; display: inline-block; } .text-view { display: flex; align-items: center; justify-content: center; flex-direction: column; width: 100%; height: 100%; color: #fff; } .month { font-size: 30rpx; margin-top: 10rpx; } .week { font-size: 32rpx; margin-top: 10rpx; } .day { font-size: 32rpx; width: 60rpx; height: 60rpx; border-radius: 10rpx; text-align: center; line-height: 60rpx; margin-top: 20rpx; } /* 日期選中的樣式 */ .day-active { background-color: #FFB400; color: #fff; }
上述就是小編為大家分享的怎么在小程序中實(shí)現(xiàn)一個(gè)日歷左右滑動(dòng)效果了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。