// 鍏充簬鏈堜唤锛 鍦ㄨ缃椂瑕?1锛屼娇鐢ㄦ椂瑕?1 $(function () { $('#calendar').calendar({ ifSwitch: true, // 鏄惁鍒囨崲鏈堜唤 hoverDate: false, // hover鏄惁鏄剧ず褰撳ぉ淇℃伅 backToday: false // 鏄惁杩斿洖褰撳ぉ }); }); ;(function ($, window, document, undefined) { var Calendar = function (elem, options) { this.$calendar = elem; this.defaults = { ifSwitch: true, hoverDate: false, backToday: false }; this.opts = $.extend({}, this.defaults, options); // console.log(this.opts); }; Calendar.prototype = { showHoverInfo: function (obj) { // hover 鏃舵樉绀哄綋澶╀俊鎭 }, showCalendar: function () { // 杈撳叆鏁版嵁骞舵樉绀 var self = this; var year = dateObj.getDate().getFullYear(); var month = dateObj.getDate().getMonth() + 1; var dateStr = returnDateStr(dateObj.getDate()); var firstDay = new Date(year, month - 1, 1); // 褰撳墠鏈堢殑绗竴澶 this.$calendarTitle_text.text(year + '骞? + parseInt(dateStr.substr(4, 2))+'鏈?); var first_day_count = firstDay.getDay(); if(first_day_count == 0) { //琛ㄧず鏄懆澶?鍥犱负瑕佷粠鍛ㄤ竴寮€濮嬫樉绀?鎵€浠ヨ繖閲岀壒娈婂鐞嗕竴涓 first_day_count = 7; } first_day_count = first_day_count -1; var index = 0; this.$calendarDate_item.each(function (i) { //绗竴鍒楃幇鍦ㄦ斁鎴愬懆宸ヤ綔浜?鎵€浠ヨ繖閲岀壒娈婂鐞嗕笅 if(!$(this).hasClass('week_detail_li')) { // allDay: 寰楀埌褰撳墠鍒楄〃鏄剧ず鐨勬墍鏈夊ぉ鏁 var allDay = new Date(year, month - 1, index + 1 - first_day_count); //console.log(firstDay.getDay()) var allDay_str = returnDateStr(allDay); //console.log(allDay_str) $(this).text(allDay.getDate()).attr('data', allDay_str); if (returnDateStr(new Date()) === allDay_str) { $(this).attr('class', 'item item-curDay'); } else if (returnDateStr(firstDay).substr(0, 6) === allDay_str.substr(0, 6)) { $(this).attr('class', 'item item-curMonth'); $(this).attr('id', 'qb-curMonth'); } else { $(this).attr('class', 'item'); $(this).attr('id', 'qb-item'); } index++; } }); }, renderDOM: function () { // 娓叉煋DOM this.$calendar_title = $('
'); this.$calendar_week = $(''); this.$calendar_date = $(''); this.$calendar_today = $('
'); //''+ //'T'+ var _titleStr = '
'+ ' <'+ ''+ ''+ '
'; var _weekStr = '
  • 涓€鍛ㄥ畨鎺扅/li>'+ '
  • 鏄熸湡涓€
  • '+ '
  • 鏄熸湡浜孅/li>'+ '
  • 鏄熸湡涓堻/li>'+ '
  • 鏄熸湡鍥汓/li>'+ '
  • 鏄熸湡浜擖/li>'+ '
  • 鏄熸湡鍏?/li>'+ '
  • 鏄熸湡鏃?/li>'; var _dateStr = ''; var _dayStr = ''+ '

    '+ '

    '; for (var i = 0; i < 6; i++) { _dateStr += '
  • '+ '
  • 26
  • '+ '
  • 26
  • '+ '
  • 26
  • '+ '
  • 26
  • '+ '
  • 26
  • '+ '
  • 26
  • '+ '
  • 26
  • '; } this.$calendar_title.html(_titleStr); this.$calendar_week.html(_weekStr); this.$calendar_date.html(_dateStr); this.$calendar_today.html(_dayStr); this.$calendar.append(this.$calendar_title, this.$calendar_week, this.$calendar_date, this.$calendar_today); this.$calendar.show(); }, inital: function () { // 鍒濆鍖 var self = this; this.renderDOM(); this.$calendarTitle_text = this.$calendar_title.find('.title'); this.$backToday = $('#backToday'); this.$arrow_prev = this.$calendar_title.find('.arrow-prev'); this.$arrow_next = this.$calendar_title.find('.arrow-next'); this.$calendarDate_item = this.$calendar_date.find('.item'); this.$calendarToday_date = this.$calendar_today.find('.date'); this.$calendarToday_week = this.$calendar_today.find('.week'); this.showCalendar(); if (this.opts.ifSwitch) { this.$calendarDate_item.bind('click', function () { $(".item").removeClass("cur"); $(this).addClass("cur"); if(!$(this).hasClass('week_detail_li')) { var _date = $(this).attr('data'); var c_date = _date.substring(0,4)+"-"+_date.substring(4,6)+"-"+_date.substring(6,8); $(".opendiv").hide(); console.log("item-left=="+$(this).offset().left); console.log("item-top=="+$(this).offset().top); $("#arrange_info").css("left",$(this).offset().left-540); $("#arrange_info").css("top",$(this).offset().top); $("#arrange_info").show(); showArrangeFDay(c_date,changingStr(c_date).getDay()); }else{ //鐐瑰嚮宸ヤ綔鍛 var week_id = $(this).attr('week_id'); if(week_id != null && week_id != undefined && week_id != '') { $(".opendiv").hide(); console.log("item-left=="+$(this).offset().left); console.log("item-top=="+$(this).offset().top); $("#arrange_week").css("left",$(this).offset().left-600); $("#arrange_week").css("top",$(this).offset().top); $("#arrange_week").show(); showArrange(week_id,$(this).attr('week_code')); } } }); this.$arrow_prev.bind('click', function () { var _date = dateObj.getDate(); var new_date = new Date(_date.getFullYear(), _date.getMonth() - 1, 1); dateObj.setDate(new_date); self.showCalendar(); //alert(new_date.getFullYear()+"-"+(new_date.getMonth() + 1)) var t_month = new_date.getMonth() + 1; if(t_month < 10) t_month = "0"+t_month; getWeekDetailList(new_date.getFullYear()+"-"+t_month); }); this.$arrow_next.bind('click', function () { var _date = dateObj.getDate(); var new_date = new Date(_date.getFullYear(), _date.getMonth() + 1, 1); dateObj.setDate(new_date); self.showCalendar(); //alert(new_date.getFullYear()+"-"+(new_date.getMonth() + 1)) var t_month = new_date.getMonth() + 1; if(t_month < 10) t_month = "0"+t_month; getWeekDetailList(new_date.getFullYear()+"-"+t_month); }); } if (this.opts.backToday) { this.$backToday.bind('click', function () { if (!self.$calendarDate_item.hasClass('item-curDay')) { dateObj.setDate(new Date()); self.showCalendar(); } }); } this.$calendarDate_item.hover(function () { self.showHoverInfo($(this)); }, function () { self.$calendar_today.css({left: 0, top: 0}).hide(); }); }, constructor: Calendar }; $.fn.calendar = function (options) { var calendar = new Calendar(this, options); return calendar.inital(); }; // ========== 浣跨敤鍒扮殑鏂规硶 ========== var dateObj = (function () { var _date = new Date(); return { getDate: function () { return _date; }, setDate: function (date) { _date = date; } } })(); function returnDateStr(date) { // 鏃ユ湡杞瓧绗︿覆 var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); month = month < 10 ? ('0' + month) : ('' + month); day = day < 10 ? ('0' + day) : ('' + day); return year + month + day; }; function changingStr(fDate) { // 瀛楃涓茶浆鏃ユ湡 var fullDate = fDate.split("-"); return new Date(fullDate[0], fullDate[1] - 1, fullDate[2]); }; })(jQuery, window, document);