找回密码
 立即注册

扫一扫,访问微社区

查看: 65978|回复: 243

微信小程序实用组件:日历小程序(真机可用)

 火... [复制链接]

76

主题

28

回帖

10万

金钱

管理员

积分
0

优秀版主

发表于 2017-1-16 17:24:33 | 显示全部楼层 |阅读模式
小程序很火,但是现有的很多web插件都是操作DOM的,导致不能直接使用,最近需要弄个小程序日历,故而分享出来,大家相互交流。
可以左右切换,GIF工具掉咯,就传个静态图吧,大家自己下载体验



js文件
[mw_shl_code=applescript,true]const conf = {
  data: {
    // hasEmptyGrid 变量控制是否渲染空格子,若当月第一天是星期天,就不应该渲染空格子
    hasEmptyGrid: false
  },
  // 控制scroll-view高度
  getSystemInfo() {
    try {
      const res = wx.getSystemInfoSync();
      this.setData({
        scrollViewHeight: res.windowHeight * res.pixelRatio || 667
      });
    } catch (e) {
      console.log(e);
    }
  },
  // 获取当月共多少天
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  // 获取当月第一天星期几
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  // 计算当月1号前空了几个格子
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  // 绘制当月天数占的格子
  calculateDays(year, month) {
    let days = [];
    const thisMonthDays = this.getThisMonthDays(year, month);
    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
    }
    this.setData({
      days
    });
  },
  // 初始化数据
  onLoad(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.getSystemInfo();
    this.setData({
      cur_year,
      cur_month,
      weeks_ch
    })
  },
  // 切换控制年月
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  },
  onShareAppMessage() {
    return {
      title: '小程序日历',
      desc: '还是新鲜的日历哟',
      path: 'pages/index/index'
    }
  }
};

Page(conf);[/mw_shl_code]
wxml文件
[mw_shl_code=applescript,true]<scroll-view scroll-y="true" class="flex box box-tb box-pack-center box-align-center" style="height: {{scrollViewHeight*2}}rpx">
  <view class="calendar pink-color box box-tb">
        <view class="top-handle fs28 box box-lr box-align-center box-pack-center">
            <view class="prev box box-rl" bindtap="handleCalendar" data-handle="prev">
                <view class="prev-handle box box-lr box-align-center box-pack-center">《</view>
            </view>
            <view class="date-area box box-lr box-align-center box-pack-center">{{cur_year || "--"}} 年 {{cur_month || "--"}} 月</view>
            <view class="next box box-lr" bindtap="handleCalendar" data-handle="next">
                <view class="next-handle box box-lr box-align-center box-pack-center">》</view>
            </view>
        </view>
        <view class="weeks box box-lr box-pack-center box-align-center">
            <view class="flex week fs28" wx:for="{{weeks_ch}}" wx:key="{{index}}" data-idx="{{index}}">{{item}}</view>
        </view>
        <view class="days box box-lr box-wrap">
            <view wx:if="{{hasEmptyGrid}}" class="grid white-color box box-align-center box-pack-center" wx:for="{{empytGrids}}" wx:key="{{index}}" data-idx="{{index}}">
            </view>
            <view class="grid white-color box box-align-center box-pack-center" wx:for="{{days}}" wx:key="{{index}}" data-idx="{{index}}">
                <view class="day {{index >= 5 && index <= 13 ? 'border-radius pink-bg' : ''}} box box-align-center box-pack-center">{{item}}</view>
            </view>
        </view>
    </view>
</scroll-view>[/mw_shl_code]
附件下载
游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

0

主题

8

回帖

16

金钱

新人求带

积分
0
发表于 2017-2-20 21:08:09 | 显示全部楼层
啥也不说了,感谢楼主分享哇!

0

主题

1

回帖

13

金钱

新人求带

积分
0
发表于 2017-4-27 13:48:45 | 显示全部楼层
啥也不说了,感谢楼主分享哇!

0

主题

4

回帖

62

金钱

新人求带

积分
0
发表于 2017-5-4 11:54:03 | 显示全部楼层
啥也不说了,感谢楼主分享哇!

0

主题

5

回帖

57

金钱

新人求带

积分
0
发表于 2017-5-4 13:47:39 | 显示全部楼层
水水水水水水水水水水水水水水水水水水水

0

主题

2

回帖

22

金钱

新人求带

积分
0
发表于 2017-6-26 16:05:53 | 显示全部楼层
谢谢楼主 ~
回复

使用道具 举报

0

主题

8

回帖

75

金钱

新人求带

积分
0
发表于 2017-7-1 01:30:15 | 显示全部楼层
这个没玩过,怎么样

0

主题

3

回帖

27

金钱

新人求带

积分
0
发表于 2017-8-5 13:41:41 | 显示全部楼层
11111111111111111111111

0

主题

245

回帖

63

金钱

新人求带

积分
0
发表于 2017-8-5 17:42:27 | 显示全部楼层
谢谢楼主的分享!!!!

0

主题

2

回帖

22

金钱

新人求带

积分
0
发表于 2017-9-16 16:55:36 | 显示全部楼层
感谢分享,多谢楼主
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|微信小程序开发|教程|文档|资源汇总_即速论坛 ( 粤ICP备14097199号-1  

GMT+8, 2024-4-27 09:40 , Processed in 0.124846 second(s), 32 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表