jQuery实现简单日历效果


Posted in jQuery onJuly 05, 2020

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

日历效果如下图:

jQuery实现简单日历效果

css部分:

<style>
  * {
   margin: 0;
   padding: 0;
   list-style: none;
  }

  .main {
   width: 380px;
   height: 370px;
   margin: 30px auto;
   position: relative;
  }

  button {
   width: 30px;
   height: 30px;
   border-radius: 50%;
   background: pink;
   font-size: 12px;
   text-align: center;
   line-height: 30px;
   border: none;
   position: absolute;
   color: #fff;
   outline: none;
   cursor: pointer;
   user-select: none;
  }

  .prev {
   top: 10px;
   left: 6%;
  }

  .next {
   top: 10px;
   right: 6%;
  }

  .nowTime {
   width: 90px;
   position: absolute;
   top: 15px;
   right: calc(50% - 45px);
   font-size: 14px;
   font-weight: bold;
   background: linear-gradient(to right, rgb(247, 149, 190), rgb(248, 204, 228));
   -webkit-background-clip: text;
   color: transparent;
  }

  .container {
   width: 350px;
   margin: 50px auto;
   border: 1px solid #ccc;
   position: absolute;
   top: calc(50% - 190px);
   left: calc(50% - 175px);

  }

  .container ul:first-of-type {
   background: rgb(245, 195, 195);
   color: rgb(43, 40, 40);
  }

  .container ul {
   width: 350px;
   margin: 0 auto;
   display: flex;
   flex-wrap: wrap;
   text-align: center;
  }

  .container li {
   width: 50px;
   height: 50px;
   text-align: center;
   line-height: 50px;
   cursor: pointer;
  }

  .container li:not(.disabled):hover {
   background: rgb(255, 174, 208);
  }

  li.disabled {
   background: rgb(252, 238, 238);
   cursor: not-allowed;
   color: rgb(206, 206, 206);
  }

  li.active {
   background: rgb(255, 206, 214);
  }
</style>

html部分:

<div class="main">
  <button class="prev">上月</button>
  <p class="nowTime"></p>
  <button class="next">下月</button>
  <div class="container">
   <ul>
    <li>日</li>
    <li>一</li>
    <li>二</li>
    <li>三</li>
    <li>四</li>
    <li>五</li>
    <li>六</li>
   </ul>
   <ul class="content">

   </ul>
  </div>
</div>

js部分:

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 <script>
  $(function () {
   let allDays = 0
   let now = new Date()
   let global_month = now.getMonth() + 1
   let global_year = now.getFullYear()
   let today = now.getDate()

   $('.prev').click(function () {
    now.setMonth(now.getMonth() - 1)
    calendar()
   })
   $('.next').click(function () {
    now.setMonth(now.getMonth() + 1)
    calendar()
   })

   function calendar() {
    $('.content').empty()
    let month = now.getMonth() + 1
    let year = now.getFullYear()

    //得到每个月多少天
    switch (month) {
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12:
      allDays = 31
      break;
     case 4:
     case 6:
     case 9:
     case 11:
      allDays = 30
      break;
     default:
      //判断闰年 得到二月份的天数
      if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
       allDays = 29
      } else {
       allDays = 28
      }

      break;
    }
    //获取当前月份有多少天 生成相应个数的li
    for (let i = 1; i <= allDays; i++) {
     let li = $('<li/>').text(i)
     //给对应今天的li添加高亮效果
     if (i === today && year === global_year && month === global_month) li.addClass('active')
     $('.content').append(li)
    }

    //思路:每个月一号前面空几个 空的个数为周几就空几个
    now.setDate(1) //将时间设置成本月的一号
    let firstDay = now.getDay() // 获取一号是周几
    for (let i = 0; i < firstDay; i++) { // 循环次数为对应的是周几
     now.setDate(now.getDate() - 1) //每循环一次 日期往前倒一天
     let li = $('<li/>').text(now.getDate()).addClass('disabled') //生成相对应的空格
     $('.content').prepend(li)
    }
    now.setDate(now.getDate() + firstDay) //将时间设置回当前时间 否则影响后面时间的获取
    now.setDate(allDays)
    //思路:每个月最后一天后面空几个 空的个数为6减周几就空几个
    now.setDate(allDays) //将时间设置成每月最后一天
    let lastDay = 6 - now.getDay() //最后空的个数
    for (let i = 0; i < lastDay; i++) {
     now.setDate(now.getDate() + 1) //每循环一次 日期往后加一天
     let li = $('<li/>').text(now.getDate()).addClass('disabled') //生成相对应的空格
     $('.content').append(li)
    }
    now.setDate(now.getDate() - lastDay) //将时间设置回当前时间 否则影响后面时间的获取
    now.setDate(1)

    $('.nowTime').html(now.getFullYear() + '年' + (now.getMonth() + 1) + '月')
   }

   calendar()
  })
</script>

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

jQuery 相关文章推荐
jQueryeasyui 中如何使用datetimebox 取两个日期间相隔的天数
Jun 13 jQuery
jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]
Jul 05 jQuery
JQuery 获取多个select标签option的text内容(实例)
Sep 07 jQuery
jQuery UI 实例讲解 - 日期选择器(Datepicker)
Sep 18 jQuery
一个有意思的鼠标点击文字特效jquery代码
Sep 23 jQuery
jquery 给动态生成的标签绑定事件的几种方法总结
Feb 24 jQuery
js与jQuery实现获取table中的数据并拼成json字符串操作示例
Jul 12 jQuery
详解jquery和vue对比
Apr 16 jQuery
jQuery实现html可联动的百分比进度条
Mar 26 jQuery
jquery实现手风琴案例
May 04 jQuery
jQuery+Ajax+js实现请求json格式数据并渲染到html页面操作示例
Jun 02 jQuery
jquery实现简易验证插件封装
Sep 13 jQuery
jQuery实现飞机大战小游戏
Jul 05 #jQuery
jquery实现上传图片功能
Jun 29 #jQuery
jQuery实时统计输入框字数及限制
Jun 24 #jQuery
jQuery实现移动端下拉展现新的内容回弹动画
Jun 24 #jQuery
如何解决jQuery 和其他JS库的冲突
Jun 22 #jQuery
jQuery 移除事件的方法
Jun 20 #jQuery
Jquery ajax书写方法代码实例解析
Jun 12 #jQuery
You might like
php的控制语句
2006/10/09 PHP
PHP XML操作的各种方法解析(比较详细)
2010/06/17 PHP
php 获取页面中指定内容的实现类
2014/01/23 PHP
PHP访问Google Search API的方法
2015/03/05 PHP
完美解决在ThinkPHP控制器中命名空间的问题
2017/05/05 PHP
javascript简单性能问题及学习笔记
2014/02/04 Javascript
通过Jquery的Ajax方法读取将table转换为Json
2014/05/31 Javascript
js实现右下角提示框的方法
2015/02/03 Javascript
javascript实现简单的全选和反选功能
2016/01/05 Javascript
Angular.js与Bootstrap相结合实现表格分页代码
2016/04/12 Javascript
原生js仿jquery实现对Ajax的封装
2016/10/04 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
2016/10/09 Javascript
JS实现的DIV块来回滚动效果示例
2017/02/07 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
JavaScript生成指定范围随机数和随机序列的方法
2018/05/05 Javascript
深入浅析Node.js 事件循环、定时器和process.nextTick()
2018/10/22 Javascript
[04:40]2016个国际邀请赛中国区预选赛场地——华西村观战指南
2016/06/25 DOTA
numpy找出array中的最大值,最小值实例
2018/04/03 Python
Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度
2018/04/09 Python
Python3.6实现连接mysql或mariadb的方法分析
2018/05/18 Python
Python嵌套列表转一维的方法(压平嵌套列表)
2018/07/03 Python
python 通过SSHTunnelForwarder隧道连接redis的方法
2019/02/19 Python
Python尾递归优化实现代码及原理详解
2020/10/09 Python
HTML5网页音乐播放器的示例代码
2017/11/09 HTML / CSS
Html5移动端适配IphoneX等机型的方法
2019/06/25 HTML / CSS
HTML5 3D书本翻页动画的实现示例
2019/08/28 HTML / CSS
为女性购买传统的印度服装和婚纱:Kalkifashion
2019/07/22 全球购物
JD Sports荷兰:英国领先的运动时尚零售商
2020/03/13 全球购物
英语系本科生个人求职信
2013/09/21 职场文书
英文自荐信
2013/12/15 职场文书
家教广告词
2014/03/19 职场文书
新闻专业毕业生英文求职信
2014/03/19 职场文书
清洁工岗位职责
2015/02/13 职场文书
金陵十三钗观后感
2015/06/04 职场文书
如何利用Python实现一个论文降重工具
2021/07/09 Python
尝试使用Python爬取城市租房信息
2022/04/12 Python