calendar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * 完整代码
  3. */
  4. // 关于月份: 在设置时要-1,使用时要+1
  5. $(function() {
  6. $('#calendar').on('click', function() {
  7. // return false;
  8. })
  9. $('#calendar').calendar({
  10. ifSwitch: true, // 是否切换月份
  11. hoverDate: false, // hover是否显示当天信息
  12. backToday: true // 是否返回当天
  13. });
  14. });
  15. ;
  16. (function($, window, document, undefined) {
  17. var Calendar = function(elem, options) {
  18. this.$calendar = elem;
  19. this.defaults = {
  20. ifSwitch: true,
  21. hoverDate: false,
  22. backToday: false
  23. };
  24. this.opts = $.extend({}, this.defaults, options);
  25. // console.log(this.opts);
  26. };
  27. Calendar.prototype = {
  28. showHoverInfo: function(obj) { // hover 时显示当天信息
  29. var _dateStr = $(obj).attr('data');
  30. var offset_t = $(obj).offset().top + (this.$calendar_today.height() - $(obj).height()) / 2;
  31. var offset_l = $(obj).offset().left + $(obj).width();
  32. var changeStr = addMark(_dateStr);
  33. var _week = changingStr(changeStr).getDay();
  34. var _weekStr = '';
  35. // this.$calendar_today.show();
  36. this.$calendar_today
  37. .css({ left: offset_l, top: offset_t })
  38. .stop()
  39. .animate({ left: offset_l, top: offset_t });
  40. switch (_week) {
  41. case 0:
  42. _weekStr = 'Sunday';
  43. break;
  44. case 1:
  45. _weekStr = 'Monday';
  46. break;
  47. case 2:
  48. _weekStr = 'Tuesday';
  49. break;
  50. case 3:
  51. _weekStr = 'Wednesday';
  52. break;
  53. case 4:
  54. _weekStr = 'Thursday';
  55. break;
  56. case 5:
  57. _weekStr = 'Friday';
  58. break;
  59. case 6:
  60. _weekStr = 'Saturday';
  61. break;
  62. }
  63. this.$calendarToday_date.text(changeStr);
  64. this.$calendarToday_week.text(_weekStr);
  65. },
  66. showCalendar: function() { // 输入数据并显示
  67. var self = this;
  68. var year = dateObj.getDate().getFullYear();
  69. var month = dateObj.getDate().getMonth() + 1;
  70. var dateStr = returnDateStr(dateObj.getDate());
  71. var firstDay = new Date(year, month - 1, 1); // 当前月的第一天
  72. this.$calendarTitle_text.text(year + '/' + dateStr.substr(4, 2));
  73. this.$calendarDate_item.each(function(i) {
  74. // allDay: 得到当前列表显示的所有天数
  75. var allDay = new Date(year, month - 1, i + 1 - firstDay.getDay());
  76. var allDay_str = returnDateStr(allDay);
  77. $(this).text(allDay.getDate()).attr('data', allDay_str);
  78. if (returnDateStr(new Date()) === allDay_str) {
  79. $(this).attr('class', 'item item-curDay');
  80. } else if (returnDateStr(firstDay).substr(0, 6) === allDay_str.substr(0, 6)) {
  81. $(this).attr('class', 'item item-curMonth');
  82. } else {
  83. $(this).attr('class', 'item');
  84. }
  85. });
  86. // 已选择的情况下,切换日期也不会改变
  87. if (self.selected_data) {
  88. var selected_elem = self.$calendar_date.find('[data=' + self.selected_data + ']');
  89. selected_elem.addClass('item-selected');
  90. }
  91. },
  92. renderDOM: function() { // 渲染DOM
  93. this.$calendar_title = $('<div class="calendar-title"></div>');
  94. this.$calendar_week = $('<ul class="calendar-week"></ul>');
  95. this.$calendar_date = $('<ul class="calendar-date"></ul>');
  96. this.$calendar_today = $('<div class="calendar-today"></div>');
  97. var _titleStr = '<a href="#" class="title"></a>' +
  98. '<a href="javascript:;" id="backToday">T</a>' +
  99. '<div class="arrow">' +
  100. '<span class="arrow-prev"><</span>' +
  101. '<span class="arrow-next">></span>' +
  102. '</div>';
  103. var _weekStr = '<li class="item">日</li>' +
  104. '<li class="item">一</li>' +
  105. '<li class="item">二</li>' +
  106. '<li class="item">三</li>' +
  107. '<li class="item">四</li>' +
  108. '<li class="item">五</li>' +
  109. '<li class="item">六</li>';
  110. var _dateStr = '';
  111. var _dayStr = '<i class="triangle"></i>' +
  112. '<p class="date"></p>' +
  113. '<p class="week"></p>';
  114. for (var i = 0; i < 6; i++) {
  115. _dateStr += '<li class="item">26</li>' +
  116. '<li class="item">26</li>' +
  117. '<li class="item">26</li>' +
  118. '<li class="item">26</li>' +
  119. '<li class="item">26</li>' +
  120. '<li class="item">26</li>' +
  121. '<li class="item">26</li>';
  122. }
  123. this.$calendar_title.html(_titleStr);
  124. this.$calendar_week.html(_weekStr);
  125. this.$calendar_date.html(_dateStr);
  126. this.$calendar_today.html(_dayStr);
  127. this.$calendar.append(this.$calendar_title, this.$calendar_week, this.$calendar_date, this.$calendar_today);
  128. this.$calendar.show();
  129. },
  130. inital: function() { // 初始化
  131. var self = this;
  132. this.renderDOM();
  133. this.$calendarTitle_text = this.$calendar_title.find('.title');
  134. this.$backToday = $('#backToday');
  135. this.$arrow_prev = this.$calendar_title.find('.arrow-prev');
  136. this.$arrow_next = this.$calendar_title.find('.arrow-next');
  137. this.$calendarDate_item = this.$calendar_date.find('.item');
  138. this.$calendarToday_date = this.$calendar_today.find('.date');
  139. this.$calendarToday_week = this.$calendar_today.find('.week');
  140. this.selected_data = 0;
  141. this.showCalendar();
  142. if (this.opts.ifSwitch) {
  143. this.$arrow_prev.bind('click', function(e) {
  144. var _date = dateObj.getDate();
  145. dateObj.setDate(new Date(_date.getFullYear(), _date.getMonth() - 1, 1));
  146. self.showCalendar();
  147. return false;
  148. });
  149. this.$arrow_next.bind('click', function() {
  150. var _date = dateObj.getDate();
  151. dateObj.setDate(new Date(_date.getFullYear(), _date.getMonth() + 1, 1));
  152. self.showCalendar();
  153. return false;
  154. });
  155. }
  156. if (this.opts.backToday) {
  157. var cur_month = dateObj.getDate().getMonth() + 1;
  158. this.$backToday.bind('click', function() {
  159. var item_month = $('.item-curMonth').eq(0).attr('data').substr(4, 2);
  160. var if_lastDay = (item_month != cur_month) ? true : false;
  161. if (!self.$calendarDate_item.hasClass('item-curDay') || if_lastDay) {
  162. dateObj.setDate(new Date());
  163. self.showCalendar();
  164. }
  165. });
  166. }
  167. this.$calendarDate_item.hover(function() {
  168. self.showHoverInfo($(this));
  169. }, function() {
  170. self.$calendar_today.css({ left: 0, top: 0 }).hide();
  171. });
  172. this.$calendarDate_item.click(function() {
  173. var _dateStr = $(this).attr('data');
  174. var _date = changingStr(addMark(_dateStr));
  175. var $curClick = null;
  176. self.selected_data = $(this).attr('data');
  177. dateObj.setDate(new Date(_date.getFullYear(), _date.getMonth(), 1));
  178. if (!$(this).hasClass('item-curMonth')) {
  179. self.showCalendar();
  180. }
  181. $curClick = self.$calendar_date.find('[data=' + _dateStr + ']');
  182. $curDay = self.$calendar_date.find('.item-curDay');
  183. if (!$curClick.hasClass('item-selected')) {
  184. self.$calendarDate_item.removeClass('item-selected');
  185. $curClick.addClass('item-selected');
  186. }
  187. // console.log(_date)
  188. var timeName = document.getElementById('timeName')
  189. var year = _date.getFullYear();
  190. var month = (_date.getMonth() + 1) < 10 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1);
  191. var date = _date.getDate() < 10 ? '0' + _date.getDate() : _date.getDate();
  192. var hour = _date.getHours() < 10 ? '0' + _date.getHours() : _date.getHours();
  193. var min = _date.getMinutes() < 10 ? '0' + _date.getMinutes() : _date.getMinutes();
  194. var second = _date.getSeconds() < 10 ? '0' + _date.getSeconds() : _date.getSeconds();
  195. var mydate = year + '-' + month + '-' + date + ' ' + hour + ':' + min + ':' + second;
  196. timeName.setAttribute('timedata', mydate)
  197. timeName.innerHTML = (mydate.split(' '))[0]
  198. $('#timeshow').text(Tool.toCH($('#timeName').text()))
  199. ajax.loadProgram();
  200. ajax.loadAllProgram();
  201. // ajax.loaddata()
  202. $('#calendarboxes').hide();
  203. });
  204. },
  205. constructor: Calendar
  206. };
  207. $.fn.calendar = function(options) {
  208. var calendar = new Calendar(this, options);
  209. return calendar.inital();
  210. };
  211. // ========== 使用到的方法 ==========
  212. var dateObj = (function() {
  213. var _date = new Date();
  214. return {
  215. getDate: function() {
  216. return _date;
  217. },
  218. setDate: function(date) {
  219. _date = date;
  220. }
  221. }
  222. })();
  223. function returnDateStr(date) { // 日期转字符串
  224. var year = date.getFullYear();
  225. var month = date.getMonth() + 1;
  226. var day = date.getDate();
  227. month = month <= 9 ? ('0' + month) : ('' + month);
  228. day = day <= 9 ? ('0' + day) : ('' + day);
  229. return year + month + day;
  230. };
  231. function changingStr(fDate) { // 字符串转日期
  232. var fullDate = fDate.split("-");
  233. return new Date(fullDate[0], fullDate[1] - 1, fullDate[2]);
  234. };
  235. function addMark(dateStr) { // 给传进来的日期字符串加-
  236. return dateStr.substr(0, 4) + '-' + dateStr.substr(4, 2) + '-' + dateStr.substring(6);
  237. };
  238. // 条件1:年份必须要能被4整除
  239. // 条件2:年份不能是整百数
  240. // 条件3:年份是400的倍数
  241. function isLeapYear(year) { // 判断闰年
  242. return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
  243. }
  244. })(jQuery, window, document);