小程序?qū)崿F(xiàn)橫向滑動日歷效果
本文實(shí)例為大家分享了小程序?qū)崿F(xiàn)橫向滑動日歷效果的具體代碼,供大家參考,具體內(nèi)容如下
<scroll-view class="scroll-view_H" scroll-x> <view class='list' style='width:{{ width }}rpx'> <view bindtap="select" wx:for="{{ calendar }}" wx:key="" wx:for-item="item" wx:for-index="index" data-index="{{ index }}" class='listItem {{index==currentIndex? "current":""}}'> <text class='name'>{{ item.week }}</text> <text class='date'>{{ item.date }}</text> </view> </view> </scroll-view>
js:
function getThisMonthDays(year, month) { return new Date(year, month, 0).getDate(); } // 計(jì)算每月第一天是星期幾 function getFirstDayOfWeek(year, month) { return new Date(Date.UTC(year, month - 1, 1)).getDay(); } const date = new Date(); const cur_year = date.getFullYear(); const cur_month = date.getMonth() + 1; const cur_date = date.getDate(); const weeks_ch = ['日', '一', '二', '三', '四', '五', '六']; //利用構(gòu)造函數(shù)創(chuàng)建對象 function calendar(date, week) { this.date = cur_year + '-' + cur_month + '-' + date; if (date == cur_date) { this.week = "今天"; } else if (date == cur_date + 1) { this.week = "明天"; } else { this.week = '星期' + week; } } //當(dāng)前月份的天數(shù) var monthLength = getThisMonthDays(cur_year, cur_month) //當(dāng)前月份的第一天是星期幾 var week = getFirstDayOfWeek(cur_year, cur_month) var x = week; for (var i = 1; i <= monthLength; i++) { //當(dāng)循環(huán)完一周后,初始化再次循環(huán) if (x > 6) { x = 0; } //利用構(gòu)造函數(shù)創(chuàng)建對象 that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0]) x++; } //限制要渲染的日歷數(shù)據(jù)天數(shù)為7天以內(nèi)(用戶體驗(yàn)) var flag = that.data.calendar.splice(cur_date, that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7) that.setData({ calendar: flag }) selectd = flag; // console.log(selectd); var ret_id = []; const lengths = selectd.length for (let i = 0; i < lengths; i++) { ret_id[i] = selectd[i].date; } choosedate = ret_id[0]; //設(shè)置scroll-view的子容器的寬度 that.setData({ width: 186 * parseInt(that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7) })
CSS:
/*日歷開始 */ scroll-view{ height: 128rpx; width: 101%; position:fixed; top:355rpx; } scroll-view .list{ display: flex; flex-wrap: nowrap; justify-content: flex-start; } scroll-view .listItem{ text-align: center; width:187rpx; height: 128rpx; background: #f4f4f4; padding-top: 30rpx; box-sizing: border-box; display: inline-block; } scroll-view .listItem text{ display: block; } scroll-view .listItem .name{ font-size: 25rpx; } scroll-view .listItem .date{ font-size: 25rpx; } scroll-view .current{ background-color:pink; width:200rpx; position:relative; } scroll-view .current text{ color: #fff; }
更多精彩的日歷效果請學(xué)習(xí)參考專題:javascript日歷插件
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)的簡單表單驗(yàn)證功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡單表單驗(yàn)證功能,涉及javascript針對表單提交內(nèi)容的獲取、判斷、焦點(diǎn)設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10JavaScript實(shí)現(xiàn)LRU算法的示例詳解
不知道屏幕前的朋友們,有沒有和我一樣,覺得LRU算法原理很容易理解,實(shí)現(xiàn)起來卻很復(fù)雜。所以本文就為大家整理了一下實(shí)現(xiàn)的示例代碼,需要的可以參考一下2023-04-04js獲取input長度并根據(jù)頁面寬度設(shè)置其大小及居中對齊
這篇文章主要介紹了js獲取input長度并根據(jù)頁面寬度設(shè)置其大小及居中對齊的方法,需要的朋友可以參考下2014-08-08javascript中href和replace的比較(詳解)
下面小編就為大家?guī)硪黄猨avascript中href和replace的比較(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11javascript實(shí)現(xiàn)的一個帶下拉框功能的文本框
這篇文章主要介紹了javascript實(shí)現(xiàn)的一個帶下拉框功能的文本框,需要的朋友可以參考下2014-05-05基于JavaScript 實(shí)現(xiàn)拖放功能
本文通過實(shí)例代碼給大家介紹了JavaScript 實(shí)現(xiàn)拖放功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09