原生js实现日历效果

所属分类: 网络编程 / JavaScript 阅读数: 568
收藏 0 赞 0 分享

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

html代码

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>日历插件</title>
 <link rel="stylesheet" href="./css/reset.css" >
 <link rel="stylesheet" href="./css/index.css" >
</head>
<body>
 <div class="show">
 <button id="pre">上月</button>
 <span id="showtime">2019-01</span>
 <button id="next">下月</button>
 </div>
 <div class="box" id="box">
 </div>
</body>
<script>
 // 获取本月第一天
 function getMonthDay(date){
 date=new Date(date.valueOf())
 date.setDate(1);
 return date
 }
 // 获取本月最后一天
 function getMonthLastDay(date){
 date=new Date(date.valueOf())
 date.setMonth(date.getMonth()+1);
 date.setDate(0);
 return date
 }
 //获取本月的时间对象集合
 function getMonth(date){
 var arr=[]
 // 获取本月第一天
 var _date=getMonthDay(date)
 // // 获取本月最后一天
 var dataLast=getMonthLastDay(date).getDate()
 arr.push(new Date(_date.valueOf()))
 // 处理本月第一天 到本月最后一天
 for(var i =1;i<dataLast;i++){
  _date.setDate(_date.getDate()+1)
  arr.push(new Date(_date.valueOf()))
 }
 // 向前补全,重置为本月一号
 _date=getMonthDay(date)
 var forln=_date.getDay()
 for(var i=0;i<forln;i++){
  _date.setDate(_date.getDate()-1)
  arr.unshift(new Date(_date.valueOf()))
 }
 // 向后补全,重置为本月最后一天
 _date=getMonthLastDay(date)
 forln=_date.getDay()
 for(var i=forln;i<6;i++){
  _date.setDate(_date.getDate()+1)
  arr.push(new Date(_date.valueOf()))
 }
 return arr
 }
 // 将集合渲染到页面
 function renderTable(monthDateArr,date){
 document.getElementById("showtime").innerHTML=`${date.getFullYear()}-${date.getMonth()+1}`
 var table=document.createElement('table')
 var trTh=document.createElement('tr')
 trTh.innerHTML=`
  <th>周日</th>
  <th>周一</th>
  <th>周二</th>
  <th>周三</th>
  <th>周四</th>
  <th>周五</th>
  <th>周六</th>
 `
 table.appendChild(trTh)
 var tr=document.createElement('tr')
 for(let i in monthDateArr){
  var td=document.createElement('td')
  td.innerHTML=monthDateArr[i].getDate()
  tr.appendChild(td)
  if((i*1+1)%7===0){
  table.appendChild(tr)
  tr=document.createElement('tr')
  }else if(i == monthDateArr.length-1){
  table.appendChild(tr)
  }
 }
 document.getElementById("box").innerHTML=table.outerHTML
 }
 var date=new Date()
 document.getElementById('pre').onclick=function(){
 date.setDate(1)
 date.setMonth(date.getMonth()-1)
 renderTable(getMonth(date),date)
 }
 document.getElementById('next').onclick=function(){
 date.setDate(1)
 date.setMonth(date.getMonth()+1)
 rrenderTable(getMonth(date),date)
 }
 renderTable(getMonth(date),date)
 
</script>
</html>

css代码

.box{
 width: 700px;
 margin: 0 auto;
 box-sizing: border-box;
 padding: 0 1px;
}
table{
 width: 100%;
}
th,td{
 width:100px;
 text-align: center;
}
th{
 height: 30px;
 line-height: 30px;
 background: #e0e5e5;
}
td{
 height: 70px;
 line-height: 70px;
 background: #f3f5f5;
}
.show{
 display: flex;
 align-items: center;
 justify-content: center;
}

更多精彩内容请点击专题《javascript日历插件使用方法汇总》进行学习

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

更多精彩内容其他人还在看

深入解析Vue 组件命名那些事

本篇文章主要介绍了深入解析Vue 组件命名那些事,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Vue学习笔记进阶篇之vue-cli安装及介绍

这篇文章主要介绍了Vue学习笔记进阶篇之vue-cli安装及介绍,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jquery版轮播图效果和extend扩展

这篇文章主要为大家详细介绍了jquery版轮播图效果,以及extend扩展的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jQuery Validate格式验证功能实例代码(包括重名验证)

本文通过实例代码给大家介绍了jQuery Validate格式验证功能,代码中包括重名验证的方法,需要的的朋友参考下吧
收藏 0 赞 0 分享

Angular.js中angular-ui-router的简单实践

本篇文章主要介绍了Angular.js中angular-ui-router的简单实践,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JavaScript实现二维坐标点排序效果

这篇文章主要为大家详细介绍了JavaScript实现二维坐标点排序效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

深入理解vue2.0路由如何配置问题

本篇文章主要介绍了vue2.0路由配置问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于bootstrap实现多个下拉框同时搜索功能

这篇文章主要为大家详细介绍了基于bootstrap实现多个下拉框同时搜索功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JavaScript 值类型和引用类型的初次研究(推荐)

这篇文章主要介绍了JavaScript 值类型和引用类型的初次研究,需要的朋友可以参考下
收藏 0 赞 0 分享

利用jQuery异步上传文件的插件用法详解

这篇文章主要介绍了利用jQuery异步上传文件的插件用法详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多