微信小程序实现日历小功能


Posted in Javascript onNovember 18, 2020

本文实例为大家分享了微信小程序实现日历功能的具体代码,供大家参考,具体内容如下

微信小程序实现日历小功能

代码

<!-- 日历 -->
 <view class="gradient">
  <view class="box">
  <view class="spaceAroundCenter flex_align_justify_b">
   <view class="flex-item">
   <view class="item-content flex_align_justify_c" bindtap="doDay" data-key='left'>
    <view class="glyphicon glyphicon-triangle-left">
    <van-icon name="arrow-left" class="flex_align" />
    </view>
   </view>
   </view>
   <view class="flex-item item-content-current-day">
   <view class="item-content flex_align_justify_c">{{currentDate}}</view>
   </view>
   <view class="flex-item">
   <view class="item-content flex_align_justify_c" bindtap="doDay" data-key="right">
    <view class="glyphicon glyphicon-triangle-right">
    <van-icon name="arrow" class="flex_align" />
    </view>
   </view>
   </view>
  </view>

  <view class="spaceAroundCenter">
   <view class="spaceAroundCenter_date">一</view>
   <view class="spaceAroundCenter_date">二</view>
   <view class="spaceAroundCenter_date">三</view>
   <view class="spaceAroundCenter_date">四</view>
   <view class="spaceAroundCenter_date">五</view>
   <view class="spaceAroundCenter_date">六</view>
   <view class="spaceAroundCenter_date">日</view>
  </view>

  <view class="spaceAroundCenter">
   <view class="flex-item day_item" wx:for="{{currentDayList}}" wx:for-index='key' wx:for-item="vo" wx:key="{{key}}">
   <view id='{{vo}}' class="item-content flex_align_justify_c bk-color-day" wx:if="{{vo == currentDay}}" bindtap='onClickItem'>{{vo}}</view>
   <view id='{{vo}}' class="item-content flex_align_justify_c bk-color-dayClick" wx:elif="{{vo == currentClickKey && currentClickKey != '' && vo != ''}}" bindtap='onClickItem'>{{vo}}</view>
   <view id='{{vo}}' class="item-content flex_align_justify_c" bindtap='onClickItem' wx:else>{{vo}}</view>
   <view class="day_item_circle" wx:if="{{currentClickKey!=''&¤tClickKey==vo&&remindlist.length>=1}}"></view>
   </view>
  </view>
  </view>
</view>

CSS:

/* 日历 */
.gradient{
 background: #fff;
 margin-bottom: 16rpx;
 overflow: hidden;
 border-bottom: 1rpx solid #f1f1f1;
}
.gradient .box{
 padding: 0 24rpx;
}
.glyphicon .iconfont{
 font-size: 46rpx;
}
.spaceAroundCenter {
 display: flex;
 flex-flow: row wrap;
 align-items: center;
 justify-content: space-around;
}
.spaceAroundCenter_date{
 font-size: 28rpx;
}
.flex-item {
 /* flex-flow: nowrap;
 flex-grow: 1;
 flex-shrink: 1; */
 width: 14.2%;
 /* padding: 10rpx 0; */
 height: 85rpx;
 display: flex;
 align-items: center;
 justify-content: center;
}
 
.item-content {
 font-size: 28rpx;
 width:80%;
 height: 60%;
}
 /* 当前日期 */
.bk-color-day {
 color: #fff;
 border-radius: 50%;
 background-color: #dd5923;
}

/* 当前选中日期 */
.bk-color-dayClick {
 color: #fff;
 border-radius: 50%;
 background-color: #30318b;
}
 
.item-content-current-day {
 flex-grow: 2;
}
.day_item{
 position: relative;
}
.day_item_circle{
 width: 10rpx;
 height: 10rpx;
 background: #dd5923;
 position: absolute;
 bottom: 0;
 border-radius: 50%;
}

js:

Page({
 data: {
 currentDayList: '',
 currentObj:'',
 arr: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
 currentDate:'',
 currentDay:'',
 year:'',
 month:'',
 currentClickKey:'',
 remindlist:[1,2,3]
 },
 onLoad: function (options) {
 var currentObj = this.getCurrentDayString()
 this.setData({
  currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
  currentDay: currentObj.getDate(),
  currentObj: currentObj,
  year: currentObj.getFullYear(),
  month: currentObj.getMonth() + 1
 })
 this.setSchedule(currentObj);
 },
 doDay: function (e) {
 this.setData({
  currentClickKey:''
 })
 var arr = this.data.arr;
 for (let i in arr) {
  var newarr = 'arr[' + i + ']';
  this.setData({
  [newarr]: ''
  })
 }
 var that = this
 var currentObj = that.data.currentObj
 var Y = currentObj.getFullYear();
 var m = currentObj.getMonth() + 1;
 var d = currentObj.getDate();
 var str = ''
 if (e.currentTarget.dataset.key == 'left') {
  m -= 1
  if (m <= 0) {
  str = (Y - 1) + '/' + 12 + '/' + d
  } else {
  str = Y + '/' + m + '/' + d
  }
 } else {
  m += 1
  if (m <= 12) {
  str = Y + '/' + m + '/' + d
  } else {
  str = (Y + 1) + '/' + 1 + '/' + d
  }
 }
 currentObj = new Date(str)
 this.setData({
  currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
  currentObj: currentObj,
  year: currentObj.getFullYear(),
  month: (currentObj.getMonth() + 1),
  day: ''
 })
 this.setSchedule(currentObj);
 },
 getCurrentDayString: function () {
 var objDate = this.data.currentObj
 if (objDate != '') {
  return objDate
 } else {
  var c_obj = new Date()
  var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
  return new Date(a)
 }
 },
 setSchedule: function (currentObj) {
 var that = this
 var m = currentObj.getMonth() + 1
 var Y = currentObj.getFullYear()
 var d = currentObj.getDate();
 var dayString = Y + '/' + m + '/' + currentObj.getDate()
 var currentDayNum = new Date(Y, m, 0).getDate()
 var currentDayWeek = currentObj.getUTCDay() + 1
 var result = currentDayWeek - (d % 7 - 1);
 var firstKey = result <= 0 ? 7 + result : result;
 var currentDayList = []
 var f = 0
 for (var i = 0; i < 42; i++) {
  let data = []
  if (i < firstKey - 1) {
  currentDayList[i] = ''
  } else {
  if (f < currentDayNum) {
   currentDayList[i] = f + 1
   f = currentDayList[i]
  } else if (f >= currentDayNum) {
   currentDayList[i] = ''
  }
  }
 }
 that.setData({
  currentDayList: currentDayList
 })
 },
 // 设置点击事件
 onClickItem: function (e) {
 // console.log(JSON.stringify(e));
 // console.log(JSON.stringify(e.currentTarget));
 this.setData({
  currentClickKey: e.currentTarget.id
 });
 },
})

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript 简练的几个函数
Aug 29 Javascript
使用js判断控件是否获得焦点
Jan 03 Javascript
angularjs中的单元测试实例
Dec 06 Javascript
swtich/if...else的替代语句
Aug 16 Javascript
js实现图片放大和拖拽特效代码分享
Sep 05 Javascript
每天一篇javascript学习小结(Date对象)
Nov 13 Javascript
vue 2.0路由之路由嵌套示例详解
May 08 Javascript
vue指令只能输入正数并且只能输入一个小数点的方法
Jun 08 Javascript
JSONP原理及应用实例详解
Sep 13 Javascript
Webpack4 使用Babel处理ES6语法的方法示例
Mar 07 Javascript
ES6中的class是如何实现的(附Babel编译的ES5代码详解)
May 17 Javascript
解决antd Form 表单校验方法无响应的问题
Oct 27 Javascript
微信小程序实现底部弹出模态框
Nov 18 #Javascript
微信小程序实现左滑删除效果
Nov 18 #Javascript
详解vue实现坐标拾取器功能示例
Nov 18 #Vue.js
JQuery+drag.js上传图片并且实现图片拖曳
Nov 18 #jQuery
Vue如何循环提取对象数组中的值
Nov 18 #Vue.js
vue在图片上传的时候压缩图片
Nov 18 #Vue.js
Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互的实例
Nov 18 #Vue.js
You might like
JAVA/JSP学习系列之七
2006/10/09 PHP
浅析PHP中Collection 类的设计
2013/06/21 PHP
PHP获取当前url的具体方法全面解析
2013/11/26 PHP
PHP实现的CURL非阻塞调用类
2018/07/26 PHP
Draggable Elements 元素拖拽功能实现代码
2011/03/30 Javascript
javascript针对DOM的应用实例(一)
2012/04/15 Javascript
关于JS字符串函数String.replace()
2013/04/07 Javascript
javascript根据像素点取位置示例
2014/01/27 Javascript
jquery获取元素索引值index()示例
2014/02/13 Javascript
javascript中使用正则计算中文长度的例子
2014/04/29 Javascript
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
jquery实现倒计时功能
2015/12/28 Javascript
jQuery层次选择器用法示例
2016/09/09 Javascript
BootStrap实现邮件列表的分页和模态框添加邮件的功能
2016/10/13 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
微信小程序checkbox组件使用详解
2018/01/31 Javascript
vue2.0组件之间传值、通信的多种方式(干货)
2018/02/10 Javascript
对vux点击事件的优化详解
2018/08/28 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
2018/11/02 Javascript
跟混乱的页面弹窗说再见
2019/04/11 Javascript
layui使用表格渲染获取行数据的例子
2019/09/13 Javascript
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
python通过scapy获取局域网所有主机mac地址示例
2014/05/04 Python
Django模型序列化返回自然主键值示例代码
2019/06/12 Python
python Elasticsearch索引建立和数据的上传详解
2019/08/04 Python
将matplotlib绘图嵌入pyqt的方法示例
2020/01/08 Python
浅谈Pycharm的项目文件名是红色的原因及解决方式
2020/06/01 Python
如何实现一个python函数装饰器(Decorator)
2020/10/12 Python
数控技术专业推荐信
2013/11/01 职场文书
实习生个人的自我评价
2013/12/08 职场文书
餐饮部总监岗位职责范文
2014/02/13 职场文书
办公室主任岗位承诺书
2014/05/29 职场文书
北京天坛导游词
2015/02/12 职场文书
基石观后感
2015/06/12 职场文书
担保公司2015年终工作总结
2015/10/14 职场文书
python字符串常规操作大全
2021/05/02 Python