看标题也不知道你有没有明白我想表达的意思,先上个动态图吧~

需要分析:

1.获取当前日期的前一个月,后一个月和当月。比如说现在是7月5号,我需要得到6月5号至8月5号的日期,同时还要返回当前的星期。

2.滑动到某个月份的区间的时候,左侧也相应的变到当前月份。比如我现在滑动到6月10号了,那么左侧就要显示成6月了。

3.页面打开默认是显示今天。

实现思路:

1.获取本月的数据(这个例子中就是从7.1至7.31)

2.获取上个月某号至月底的数据(这个例子中就是从6.5至6.30)

3.获取下个月1号至某号的数据(这个例子中就是从8.1至8.5)

4.获取这个月1号,今天,和下一个月1号的scrollLeft的值,这是为了在滑动的时候判断它当前的日期区间是在哪个月份,从而改变左侧的月份值。(这个例子中就是从7.1,7.5,8.1这三个scrollLeft的值)

5.默认显示今天可能通过改变scroll-view的scroll-left的值实现

主要用到的函数:

1.setDate(day):设置一个月的某一天

2.setMonth(month[,day]):设置月份,day是可选参数,可填可不填。填了返回某月的某一天,不填返回某月的第1天。

3.getMonth(),getDate(),getDay():获取月(从0开始),日期,星期(返回0时表示星期日)

具体代码:

JS:

  1. // pages/teacher/interview/index.js
  2. Page({
  3.  
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8.  
  9. },
  10. //获取星期
  11. getWeek(date) {
  12. let weekArray = ['日', '一', '二', '三', '四', '五', '六'];
  13. return weekArray[date.getDay()];
  14. },
  15.  
  16. //滑动至某个区间时显示当前的月份
  17. dayScroll(e) {
  18. const scrollLeftArray = this.data.scrollLeftArray;
  19. console.log(e.detail.scrollLeft)
  20. let dayScrollLeft = e.detail.scrollLeft;
  21.  
  22. if (dayScrollLeft < scrollLeftArray[0] - 100) {
  23. this.setData({
  24. showCurrentMonth: this.data.month
  25. })
  26. } else if (this.data.day < 7) {
  27. if (e.detail.scrollLeft > scrollLeftArray[2] - (7 - this.data.day)*100) {
  28. this.setData({
  29. showCurrentMonth: this.data.month + 2
  30. })
  31. }
  32. } else if (this.data.day >= 7){
  33. if (e.detail.scrollLeft > scrollLeftArray[2]) {
  34. this.setData({
  35. showCurrentMonth: this.data.month + 2
  36. })
  37. }
  38. } else {
  39. this.setData({
  40. showCurrentMonth: this.data.month + 1
  41. })
  42. }
  43. },
  44.  
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad: function(options) {
  49. const _this = this;
  50. let now = new Date(),
  51. month = now.getMonth(),
  52. weekday = _this.getWeek(now),
  53. day = now.getDate(),
  54. prevMonth = month==0 ? 11 : month-1,
  55. nextMonth = month==11 ? 0 : month+1,
  56. lastDay = new Date((new Date().setMonth(month + 1, 1) - 1000 * 60 * 60 * 24)).getDate(), //获取当月最后一天日期;
  57. prevLastDay = new Date((new Date().setMonth(month, 1) - 1000 * 60 * 60 * 24)).getDate(); //获取上一个月最后一天日期;
  58.  
  59. let currentMonthDateArray = [], //当前月份的日期和星期的数据集合
  60. prevMonthDateArray = [], //上月日期和星期的数据集合
  61. nextMonthDateArray = []; //下月日期和星期的数据集合
  62. for (let i = 1; i <= lastDay; i++) {
  63. currentMonthDateArray.push({
  64. day: i,
  65. weekDay: _this.getWeek(new Date(new Date().setDate(i)))
  66. })
  67. }
  68.  
  69. for (let i = day; i <= prevLastDay; i++) {
  70. prevMonthDateArray.push({
  71. day: i,
  72. weekDay: _this.getWeek(new Date(new Date().setMonth(month - 1, i)))
  73. })
  74. }
  75.  
  76. for (let i = 1; i <= day; i++) {
  77. nextMonthDateArray.push({
  78. day: i,
  79. weekDay: _this.getWeek(new Date(new Date().setMonth(month + 1, i)))
  80. })
  81. }
  82. _this.setData({
  83. day: day,
  84. month: month,
  85. weekday: weekday,
  86. showCurrentMonth: month + 1,
  87. prevMonthDateArray: prevMonthDateArray,
  88. currentMonthDateArray: currentMonthDateArray,
  89. nextMonthDateArray: nextMonthDateArray
  90. })
  91.  
  92. //获取左边距是为了滑动时改变月份
  93. const query = wx.createSelectorQuery();
  94. let scrollLeftArray = [];
  95. query.selectAll(`#day${month + 1}1, #day${month + 1}${day}, #day${month + 2}1`).boundingClientRect(function(rects) {
  96. rects.forEach(function(rect) {
  97. scrollLeftArray.push(rect.left)
  98. })
  99. _this.setData({
  100. scrollLeftArray: scrollLeftArray,
  101. scrollLeftInit: scrollLeftArray[1] - 100
  102. })
  103. console.log(scrollLeftArray)
  104. }).exec()
  105. },
  106. })

wxml:

  1. <view class='row day-wrap item-center'>
  2. <view class='month fs-28 fc-66 shrink'>
  3. <text class='fs-48'>{{showCurrentMonth}}</text>
  4. </view>
  5. <view class='day-list grow over-hidden'>
  6. <scroll-view scroll-x="{{true}}" class='day-list-scroll row' bindscroll="dayScroll" scroll-left="{{scrollLeftInit}}px">
  7. <view class="day-item {{prevMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{prevMonthDateArray}}" wx:for-item="prevMonth" id="day{{month}}{{prevMonth.day}}">
  8. <view class='fs-24'>{{prevMonth.weekDay}}</view>
  9. <view class='fs-32 mt-3'>{{prevMonth.day}}</view>
  10. </view>
  11. <view class="day-item {{currentMonth.day == day ? 'today' : ''}} {{currentMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{currentMonthDateArray}}" wx:for-item="currentMonth" id="day{{month+1}}{{currentMonth.day}}">
  12. <view class='fs-24'>{{currentMonth.weekDay}}</view>
  13. <view class='fs-32 mt-3'>{{currentMonth.day}}</view>
  14. </view>
  15. <view class="day-item {{nextMonth.weekDay == weekday ? 'weekday' : ''}}" wx:for="{{nextMonthDateArray}}" wx:for-item="nextMonth" id="day{{month+2}}{{nextMonth.day}}">
  16. <view class='fs-24'>{{nextMonth.weekDay}}</view>
  17. <view class='fs-32 mt-3'>{{nextMonth.day}}</view>
  18. </view>
  19. </scroll-view>
  20. </view>
  21. </view>

wxss:

  1. .day-wrap{
  2. background-color: #fff;
  3. padding-top: 10px;
  4. padding-bottom: 10px;
  5. }
  6. .month{
  7. padding: 0 15px;
  8. }
  9.  
  10. .day-list-scroll{
  11. white-space:nowrap;
  12. }
  13. .day-item{
  14. padding: 2px 8px;
  15. margin-right:15px;
  16. text-align: center;
  17. display: inline-block;
  18. vertical-align: middle
  19. }
  20. .day-item.weekday{
  21. background-color: #f2f2f2;
  22. }
  23. .day-item.today{
  24. background-color: #ffdee8;
  25. color: #ff5e2f
  26. }

Happy Ending~

  1.  

微信小程序——获取当天的前一个月至后一个月的更多相关文章

  1. 微信小程序-获取经纬度

    微信小程序-获取经纬度 最近公司新功能 要求在外的市场人员 发送位置信息回来. 用的还是微信小程序开发.... 微信小程序 提供一个接口 getLocation 这个接口反回来的位置 相对实际位置 相 ...

  2. 微信小程序-获取当前城市位置及再次授权地理位置

    微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...

  3. 微信小程序获取Access_token和页面URL生成小程序码或二维码

    1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...

  4. [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息

    因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...

  5. C# 微信小程序获取openid sessionkey

    项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...

  6. JavaScript和微信小程序获取IP地址的方法

    最近公司新加了一个需求,根据用户登录的IP地址判断是否重复登录,重复登录就进行逼退,那么怎么获取到浏览器的IP地址呢?最后发现搜狐提供了一个JS接口,可以通过它获取到客户端的IP. 接口地址如下: h ...

  7. 微信小程序获取输入框(input)内容

    微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...

  8. .Net之微信小程序获取用户UnionID

    前言: 在实际项目开发中我们经常会遇到账号统一的问题,如何在不同端或者是不同的登录方式下保证同一个会员或者用户账号唯一(便于用户信息的管理).这段时间就有一个这样的需求,之前有个客户做了一个微信小程序 ...

  9. 微信小程序获取手机号码看这篇文章就够了

    前言 微信小程序获取手机号码,从官方文档到其他博主的文档 零零散散的 (我就是这样看过来 没有一篇满意的 也许是我搜索姿势不对) 依旧是前人栽树 后人乘凉 系列.保证看完 就可以实现获取手机号码功能 ...

随机推荐

  1. graph处理工具

    仅作为记录笔记,完善中...................... 1       PyGSP https://pygsp.readthedocs.io/en/stable/index.html ht ...

  2. [转帖]关于4A(统一安全管理平台)系统的理解

    雪山上的蒲公英 https://www.cnblogs.com/zjfjava/p/10674577.html 关于4A(统一安全管理平台)系统的理解   1. 4A系统的需求分析 近年来企业用户的业 ...

  3. poj1458公共子序列 C语言

    /*Common SubsequenceTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 56416 Accepted: 23516D ...

  4. 国内P2P网贷行业再次大清理,仅剩646家

    最近有网贷行业头部网站流出消息,国内网贷行业再次迎来大洗牌 清扫之后网贷的平台数量仅剩646家,数量陡降 根据小编了解.自2007年国外网络借贷平台模式引入中国以来,由于国家一时没有做出相应规定个条例 ...

  5. python_dict json读写文件

    命令汇总: json.dumps(obj) 将python数据转化为json Indent实现缩进,ensure_ascii 是否用ascii解析 json.loads(s) 将json数据转换为py ...

  6. DevExpress中GridColumnCollection实现父子表数据绑定

    绑定数据: 父表: DataTable _parent = _dvFlt.ToTable().Copy(); 子表: DataTable _child = _dvLog.ToTable().Copy( ...

  7. hystrix简介

    hystrix,框架,提供了高可用相关的各种各样的功能,然后确保说在hystrix的保护下,整个系统可以长期处于高可用的状态,100%. 高可用系统架构: 资源隔离.限流.熔断.降级.运维监控 资源隔 ...

  8. Windows 7 下安装 docker

    Windows 7 下需要安装docker toolbox即可(里面打包了docker.oracle virtualbox.Git) 1. 下载 1. 下载路径https://github.com/d ...

  9. 【转载】C#中List集合使用IndexOf判断元素第一次出现的索引位置

    在C#的List集合操作中,有时候需要判断元素对象在List集合中第一次出现的索引位置信息,此时需要使用到List集合的IndexOf方法来判断,如果元素存在List集合中,则IndexOf方法返回所 ...

  10. Python运算符大全

    一. Python的算术运算 Python的算术运算符与C语言类似,略有不同.包括加(+).减(-).乘(*).除(/).取余(%).按位或(|).按位与(&).按位求补(~).左移位(< ...