您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在小程序中實現一個橫向滑動日歷效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
具體內容如下
<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(); } // 計算每月第一天是星期幾 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 = ['日', '一', '二', '三', '四', '五', '六']; //利用構造函數創建對象 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; } } //當前月份的天數 var monthLength = getThisMonthDays(cur_year, cur_month) //當前月份的第一天是星期幾 var week = getFirstDayOfWeek(cur_year, cur_month) var x = week; for (var i = 1; i <= monthLength; i++) { //當循環完一周后,初始化再次循環 if (x > 6) { x = 0; } //利用構造函數創建對象 that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0]) x++; } //限制要渲染的日歷數據天數為7天以內(用戶體驗) 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]; //設置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; }
看完上述內容,你們對怎么在小程序中實現一個橫向滑動日歷效果有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。