温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用js实现签到日历

发布时间:2022-08-29 11:18:05 阅读:131 作者:iii 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本文小编为大家详细介绍“怎么用js实现签到日历”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用js实现签到日历”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

怎么用js实现签到日历

wxml代码

<view class="boxMain" >
    <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.png"></image>
        </view>                
        <text>{{chooseYear}}年{{chooseMonth+1}}月</text>
        <view class="calendarChangeLeftRight" catchtap="initDateList" data-month="{{chooseMonth+1}}">
            <image mode="widthFix" src="{{static}}right_arrow.png"></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 >
                    {{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: [],
    chooseYearnew Date().getFullYear(),
    chooseMonthnew Date().getMonth(),
    currentDaynew Date().getDate(),
    dayNumsByMonthnull
  },
  onLoad() {
    this.initDateList(); //初始化日历
  },
  initDateListfunction (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',
          duration1500
        })
 
        return
      } else if (monthCount < 0 && Math.abs(monthCount) > 1) {
        wx.showToast({
          title'往后最多查看一个月',
          icon'none',
          duration1500
        })
        return
      }
    }
 
    that.setData({
      chooseMonth: chooseMonth,
      chooseYear: chooseYear
    })
    var dateList = [];
 
    let firstDayWeek = new Date(that.data.chooseYear, that.data.chooseMonth1).getDay();
    let dayNumsByMonth = new Date(that.data.chooseYear, that.data.chooseMonth + 10).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';
        }
      }
    }
  },
  isTenfunction (v) {
    return v >= 10 ? v : `0${v}`
  }
});

wxss代码

.boxMain {
  background-color#fff;
  margin36rpx 30rpx 22rpx 30rpx;
  border-radius10rpx;
  padding0 20rpx;
  display: flex;
  flex-direction: column;
}
 
.boxMain .calendarHeader {
  display: flex;
  justify-content: space-between;
  color#333333;
  font-size28rpx;
  margin-top40rpx;
  padding0 4rpx;
}
 
.boxMain .calendarHeader view text {
  color#FE7458;
  padding0 6rpx;
}
 
.boxMain .calendarHeader view:last-child {
  font-size24rpx;
}
 
.boxMain .calendarChange {
  display: flex;
  padding0 50rpx;
  align-items: center;
  justify-content: space-between;
  background-color#F8F8FA;
  border-radius25rpx;
  height50rpx;
  line-height50rpx;
  font-size24rpx;
  text-align: center;
  margin25rpx 0;
}
.boxMain .calendarChange .calendarChangeLeftRight{
  width50rpx;
  height50rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.boxMain .calendarChange image{
  width12rpx;
  margin0 auto;
}
.boxMain .calendarContent .calendarWeek{
  font-size26rpx;
  display: flex;    
  justify-content: space-between;
}
.boxMain .calendarContent .calendarWeek text{
  width14%;
  height40rpx;
  text-align: center;
}
.boxMain .calendarContent .calendarDays{
  font-size26rpx;
  display: flex;
  justify-content: space-between;
  flex-wrap:wrap;
  align-items: center;
  margin-top47rpx;
}
.boxMain .calendarContent .calendarDays>view{
  width14%;
  text-align: center;
  display: block;    
  margin-bottom32rpx;
  height46rpx;
  line-height46rpx;
}
.boxMain .calendarContent .calendarDays>view>view{
  width46rpx;
  height46rpx;
  margin0 auto;
  border-radius50%;
}
.boxMain .calendarContent .calendarDays .box{
  margin-top: -10rpx;
}
.boxMain .calendarContent .calendarDays .box image{
  width42rpx;
}
.boxMain .calendarContent .calendarDays .box text{
  float: left;
  margin-top: -20rpx;
  padding-left4rpx;
  color#FE7458;
}

读到这里,这篇“怎么用js实现签到日历”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI

开发者交流群×