jQuery 强大日历插件 Calendar.js

  • 源码大小:38.43KB
  • 所需积分:1积分
  • 源码编号:19JP-3753
  • 浏览次数:1183次
  • 最后更新:2023年07月18日
  • 所属栏目:时间和时钟
本站默认解压密码:19jp.com 或 19jp_com

简介

一个轻量级、快速、可定制且易于样式化的日历组件,构建在jQuery之上。

主要功能:

  • 使用“下一个/上一个”按钮在月份之间切换。
  • 或者通过单击年份,在选择器UI中直接在月份之间切换。
  • 还允许您从下拉列表中选择年份。
  • 允许您设置最小/最大日期。
  • 也支持禁用日期。
  • 有用的回调,可用于日期选择器、月份选择器或年份选择器。

如何使用它:

1.放置主脚本日历.min.js加载最新的jQuery库之后。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  2. <script src="/path/to/calendar.min.js"></script>

2.创建一个容器来容纳日历。

  1. <div class="calendar-container"></div>

3.调用函数在您刚刚创建的容器中生成一个默认日历。

  1. $(function(){
  2. $('.calendar-container').calendar();
  3. });

4.将CSS类应用于日历或在页面上包含以下CSS文件。

  1. <link rel="stylesheet" href="style.css" />
  2. <link rel="stylesheet" href="theme.css" />

5.确定要在页面加载中突出显示的日期。

  1. $('.calendar-container').calendar({
  2. date: new Date() // today
  3. });

6.设置一周中日期名称的长度。默认值:1。

  1. $('.calendar-container').calendar({
  2. weekDayLength: 2
  3. });

7.使用禁用日期使残废作用

  1. $('.calendar-container').calendar({
  2. date: new Date(),
  3. disable: function (date) {
  4. return date < new Date();
  5. }
  6. });

8.在选择日期时触发一个功能。

  1. $('.calendar-container').calendar({
  2. onClickDate: function (date) {
  3. // do something
  4. }
  5. });

9.更多可自定义日历的配置。

  1. $('.calendar-container').calendar({
  2.  
  3. // text for prev/next buttons
  4. prevButton: "Prev",
  5. nextButton: "Next",
  6.  
  7. // custom separator between the month and the year in the month view.
  8. monthYearSeparator: " ",
  9.  
  10. // false = 4 months in a row
  11. showThreeMonthsInARow: true,
  12.  
  13. // whether to change either month or year
  14. enableMonthChange: true,
  15.  
  16. // whether to disable year view
  17. enableYearView: true,
  18.  
  19. // shows a Today button on the bottom of the calendar
  20. showTodayButton: true,
  21. todayButtonContent: "Today",
  22.  
  23. // highlights all other dates with the same week-day
  24. highlightSelectedWeekday: true,
  25.  
  26. // highlights the selected week that contains the selected date
  27. highlightSelectedWeek: true,
  28.  
  29. // whether to enable/disable the year selector
  30. showYearDropdown: false,
  31.  
  32. // min/max dates
  33. // Date Object or Date String
  34. min: null,
  35. max: null,
  36.  
  37. // start on Sunday instead
  38. startOnMonday: false,
  39.  
  40. // format week day
  41. formatWeekDay: function(weekDay) {
  42. // function to format the week day
  43. },
  44.  
  45. // format date
  46. formatDate: function(day) {
  47. // function to format date
  48. },
  49.  
  50. // map the month number to a string
  51. monthMap: {
  52. 1: "january",
  53. 2: "february",
  54. 3: "march",
  55. 4: "april",
  56. 5: "may",
  57. 6: "june",
  58. 7: "july",
  59. 8: "august",
  60. 9: "september",
  61. 10: "october",
  62. 11: "november",
  63. 12: "december",
  64. }
  65.  
  66. // map the week number to a string
  67. dayMap: {
  68. 0: "sunday",
  69. 1: "monday",
  70. 2: "tuesday",
  71. 3: "wednesday",
  72. 4: "thursday",
  73. 5: "friday",
  74. 6: "saturday",
  75. }
  76.  
  77. // map the week number to a string when monday is the start of the week
  78. alternateDayMap: {
  79. 1: "monday",
  80. 2: "tuesday",
  81. 3: "wednesday",
  82. 4: "thursday",
  83. 5: "friday",
  84. 6: "saturday",
  85. 7: "sunday",
  86. },
  87. });

10.更多回调函数。

  1. $('.calendar-container').calendar({
  2.  
  3. onChangeMonth: function (date) {},
  4. onClickToday: function (date) {},
  5. onClickMonthNext: function (date) {},
  6. onClickMonthPrev: function (date) {},
  7. onClickYearNext: function (date) {},
  8. onClickYearPrev: function (date) {},
  9. onShowYearView: function (date) {},
  10. onSelectYear: function (date) {},
  11. });

11.获取所选日期。

  1. var calendar = $('.calendar-container').calendar();
  2. console.log(calendar.getSelectedDate());

更新日志:

2021-01-10

  • JS更新

v1.3.0 (2021-12-18)

  • 添加了formatWeekDay函数
  • 添加了formatDate函数
  • 已添加monthMap对象
  • 已添加dayMap对象
  • 添加了alternateDayMap对象

2021-08-31

  • v1.2.1:Safari的错误修复。

2021-07-28

  • v1.2:添加了getSelectedDate方法。修复startOnMonday为true时错误日期的错误

2021-03-31

  • v1.1:添加了“startOnMonday”标志。这使您可以在星期一而不是星期天开始本月的工作。

2021-03-30

  • v1.0:首次稳定发布

2021-03-26

  • Safari的错误修复。

预览截图