js实现签到日历

本文实例为大家分享了js实现签到日历的具体代码,供大家参考,具体内容如下

wxml代码

<view class="boxMain" style="height:{{dateList.length>35?'805rpx':'730rpx'}}">     <view class="calendarHeader">         <view>本月已签到<text>{{recordList.length}}</text>天</view>     </view>     <view class="calendarChange">         <view class="calendarChangeLeftRight" catchtap="initDateList" data-month="{{chooseMonth-1}}">             <image mode="widthFix" src="{{static}}left_arrow.webp"></image>         </view>                         <text>{{chooseYear}}年{{chooseMonth+1}}月</text>         <view class="calendarChangeLeftRight" catchtap="initDateList" data-month="{{chooseMonth+1}}">             <image mode="widthFix" src="{{static}}right_arrow.webp"></image>         </view>     </view>     <view class="calendarContent">         <view class="calendarWeek">             <text>日</text>             <text>一</text>             <text>二</text>             <text>三</text>             <text>四</text>             <text>五</text>             <text>六</text>         </view>         <view class="calendarDays">             <view wx:for="{{dateList}}" wx:key="index" wx:for-item="dateItem">                 <view style="color:{{dateItem.type=='current'?'#ffffff':(dateItem.type=='before'?'#979797':(dateItem.type=='selected'?'#FE7458':''))}};background-color:{{ dateItem.type=='current'?'#FE7458':(dateItem.type=='before'?'#F8F8FA':(dateItem.type=='selected'?'#ffdcd5':'')) }}">                     {{dateItem.day}}                 </view>                   </view>         </view>     </view> </view>

js代码:

const app = getApp() const http = require('../../config/api.js'); Page({   data: {     static: app.data.static,     recordList: [],     totalReward: {},     dateList: [],     chooseYear: new Date().getFullYear(),     chooseMonth: new Date().getMonth(),     currentDay: new Date().getDate(),     dayNumsByMonth: null   },   onLoad() {     this.initDateList(); //初始化日历   },   initDateList: function (e) {     let that = this;     let chooseMonth = that.data.chooseMonth;     let chooseYear = that.data.chooseYear;     let currentDate = new Date();     let currentYear = currentDate.getFullYear();     let currentMonth = currentDate.getMonth();     if (e) {       chooseMonth = e.currentTarget.dataset.month;       if (chooseMonth >= 12) {         chooseMonth = chooseMonth - 12;         chooseYear = chooseYear + 1;       } else if (chooseMonth < 0) {         chooseMonth = chooseMonth + 12;         chooseYear = chooseYear - 1;       } else {         chooseMonth = chooseMonth;       }       let monthCount = (currentYear - chooseYear) * 12 + currentMonth - chooseMonth;       if (monthCount > 0 && Math.abs(monthCount) > 6) {         wx.showToast({           title: '往前最多查看六个月',           icon: 'none',           duration: 1500         })         return       } else if (monthCount < 0 && Math.abs(monthCount) > 1) {         wx.showToast({           title: '往后最多查看一个月',           icon: 'none',           duration: 1500         })         return       }     }     that.setData({       chooseMonth: chooseMonth,       chooseYear: chooseYear     })     var dateList = [];     let firstDayWeek = new Date(that.data.chooseYear, that.data.chooseMonth, 1).getDay();     let dayNumsByMonth = new Date(that.data.chooseYear, that.data.chooseMonth + 1, 0).getDate();     that.setData({       dayNumsByMonth: dayNumsByMonth     })     for (let i = 0; i < 43; i++) {       let day = i - firstDayWeek + 1;       if (day > dayNumsByMonth && (i == 35 || i == 42)) {         that.setData({           dateList: dateList         });         return       }       dateList.push({         'year': '',         'month': '',         'day': '',         'type': '',       })       if (day > 0 && day <= dayNumsByMonth) {         dateList[i].year = that.data.chooseYear;         dateList[i].month = that.isTen(that.data.chooseMonth + 1);         dateList[i].day = that.isTen(day);         if (that.data.chooseYear == currentYear && that.data.chooseMonth == currentMonth) {           if (day < that.data.currentDay) {             dateList[i].type = 'before';           } else if (day > that.data.currentDay) {             dateList[i].type = 'after';           } else {             dateList[i].type = 'current'           }         } else if (that.data.chooseYear < currentYear || (that.data.chooseYear == currentYear && that.data.chooseMonth < currentMonth)) {           dateList[i].type = 'before';         } else {           dateList[i].type = 'after';         }       }     }   },   isTen: function (v) {     return v >= 10 ? v : `0${v}`   } });

wxss代码

.boxMain {   background-color: #fff;   margin: 36rpx 30rpx 22rpx 30rpx;   border-radius: 10rpx;   padding: 0 20rpx;   display: flex;   flex-direction: column; } .boxMain .calendarHeader {   display: flex;   justify-content: space-between;   color: #333333;   font-size: 28rpx;   margin-top: 40rpx;   padding: 0 4rpx; } .boxMain .calendarHeader view text {   color: #FE7458;   padding: 0 6rpx; } .boxMain .calendarHeader view:last-child {   font-size: 24rpx; } .boxMain .calendarChange {   display: flex;   padding: 0 50rpx;   align-items: center;   justify-content: space-between;   background-color: #F8F8FA;   border-radius: 25rpx;   height: 50rpx;   line-height: 50rpx;   font-size: 24rpx;   text-align: center;   margin: 25rpx 0; } .boxMain .calendarChange .calendarChangeLeftRight{   width: 50rpx;   height: 50rpx;   display: flex;   flex-direction: column;   justify-content: center; } .boxMain .calendarChange image{   width: 12rpx;   margin: 0 auto; } .boxMain .calendarContent .calendarWeek{   font-size: 26rpx;   display: flex;       justify-content: space-between; } .boxMain .calendarContent .calendarWeek text{   width: 14%;   height: 40rpx;   text-align: center; } .boxMain .calendarContent .calendarDays{   font-size: 26rpx;   display: flex;   justify-content: space-between;   flex-wrap:wrap;   align-items: center;   margin-top: 47rpx; } .boxMain .calendarContent .calendarDays>view{   width: 14%;   text-align: center;   display: block;       margin-bottom: 32rpx;   height: 46rpx;   line-height: 46rpx; } .boxMain .calendarContent .calendarDays>view>view{   width: 46rpx;   height: 46rpx;   margin: 0 auto;   border-radius: 50%; } .boxMain .calendarContent .calendarDays .box{   margin-top: -10rpx; } .boxMain .calendarContent .calendarDays .box image{   width: 42rpx; } .boxMain .calendarContent .calendarDays .box text{   float: left;   margin-top: -20rpx;   padding-left: 4rpx;   color: #FE7458; }

推荐阅读

    js设置div的边框|怎样给div设置边框

    js设置div的边框|怎样给div设置边框,,1. 怎样给div设置边框1、首先新建一个html文件,输入基本的内容,这里设置一个div,并把它的class设置为de

    js设置样式|js设置样式类

    js设置样式|js设置样式类,,js设置样式    javascript改变CSS样式分为局部和全局,分别如下:  一、局部改变样式    有三种方法:直接

    js用代码实现简单购物车

    js用代码实现简单购物车,,图: 选择所有按钮: 复制代码代码如下所示: 选择 笔记本电脑:3000元 笔记本电脑:3000元 笔记本电脑:3000元 笔记本电脑:3

    js设置背景色|js设置颜色

    js设置背景色|js设置颜色,,js设置背景色首先通过js定位到div的子元素,再通过setatteibute方法给属性添加背景色。js设置颜色js改变字体的颜

    Safari调试iOS中的js

    Safari调试iOS中的js,页面,设备,概述对于HTML5的开发,大家都知道Chrome的DevTools工具有强大的功能和友好的用户体验,不仅能快速方便调试Jav

    Bootstrap的js插件之模态框|modal

    Bootstrap的js插件之模态框|modal,模态,饭盒,.modal——指明div元素包裹模态框;.fade——给模态框添加淡入淡出效果;.modal-dialog——包裹

    js默认事件汇总

    js默认事件汇总,事件,表单,默认事件   就是浏览器通过HTML标签或DOM元素提供的一些功能性的默认行为。比如在a标签href属性上的跳转,右键

    js获取元素宽度

    js获取元素宽度,内联,样式,js获取元素宽度1、使用内联样式,即直接把CSS写在HTML元素的style属性中<div > </div>复制代码通过以下js代码

    SQLite使用JSON扩展

    SQLite使用JSON扩展,插件,加载,一、介绍 SQLite3.9.0之后的版本,添加了JSON扩展。在表中可以保存JSON类型。实际上SQLite将JSON类型的

    jscript.dll是什么

    jscript.dll是什么,系统,文件,论坛,显示,扫描,修复,  jscript.dll是Microsoft JavaScript脚本支持相关文件。属于: Microsoft JScript系统 DLL

    postgresql中对jsonb的查询及转换

    postgresql中对jsonb的查询及转换,数据,字段,表数据:需要将strata排除,并且过滤info字段中为{}的数据,将jsonb转换成text,替换“,{,}见SQL:sele