jquery Calendar Control Implementation method sharing _jquery

Source: Internet
Author: User

Comments out of the default CSS style, you can change into their own style to achieve another style, we refer to the use of it

Copy Code code as follows:



/**


* JQuery Calendar Plugin


*/


(Function ($, window) {

    ' use strict ';
    $.fn.calendar = function (options) {
       //check is Select, if nothing select, return this
        if (!this.length) {
             if (Options && options.debug && Window.console) {
                Console.log ("nothing Select");
           }
            return this;
       }
        var self = $ (this);

Default Parameter setting
var defaults = {
Csspath: ",//user-define loading path of CSS file
EventName: ' Click ',//user-define the event name that triggers
Onselectdate:null,//callback function after select Date
Autoclose:false
};

Inherit user-defined parameter
Defaults = $.extend (defaults, options);

Default as data of the day
var d_date = new Date ();
var _date = {
Year:d_date.getFullYear (),
Month:d_date.getMonth () + 1,
Day:d_date.getDate (),
Week:d_date.getDay ()
};

Default Template of Plugin


var calendardiv = ' <div id= ' calendar ' class= ' Cld_grid ' style= ' display:none;z-index:100; > ';


Calendardiv + = ' <div id= ' calendar_year_month ' class= ' cld_year_month ' style= ' position:relative; > ';


Calendardiv + = ' <div id= ' last-year ' style= ' position:absolute;left:30px; ><</div> ';


Calendardiv + = ' <div id= ' last-month ' style= ' position:absolute;left:60px; ><<</div> ';


Calendardiv + = ' <div id= ' next-year ' style= ' position:absolute;right:30px; >></div> ';


Calendardiv + = ' <div id= ' next-month ' style= ' position:absolute;right:60px; >>></div> ';


Calendardiv + = ' <input style= ' width:40px;text-align:center; "type=" text "id=" calendar_year "value=" ' + _date.year + ' ">-<input style=" Width:25px;text-align:center "type=" text "id=" Calendar_month "value=" ' + _date.month + ' "> </div> '


Calendardiv + + "<div id=" Calendar_week_box "class=" Cld_week_box clearfix ">";


Calendardiv + = ' <div class= ' header-day ' Data-index = ' 0 ' > Day </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 1 ' > </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 2 ' > Two </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 3 ' > three </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 4 ' > Four </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 5 ' > Five </div> ';


Calendardiv + = ' <div class= ' header-day ' data-index = ' 6 ' > Six </div> ';


Calendardiv + = ' </div> ';


Calendardiv = "<div class=" Days Clearfix ">";


for (var k = 0; k < k++) {


Calendardiv + = ' <div class= ' Day ' ><span class= ' day-number ' > ' + ' + ' </span></div> ';


}


Calendardiv + = ' </div></div> ';

        var calendaraction = {
            //initialization
            Initaction:function () {
                 Calendaraction.thisclick ();
                Calendaraction.inputchange ();
                Calendaraction.buttonchange ();
                Calendaraction.choosedate ();
           },

Click to display
Thisclick:function () {
Self.bind (Defaults.eventname, function (e) {
Calendaraction.showcalendar ();
});
},

When year and month in the input box changes


Inputchange:function () {


$ (' #calendar_year, #calendar_month '). Bind (' Change ', function () {


var year = $ (' #calendar_year '). Val (),


Month = $ (' #calendar_month '). Val ();


if (!/^\d{4}$/.test (year)) {


Alert (' Please input four-digit year ');


return false;


}


if (!/^\d{1,2}$/.test (month) | | (Month > | | month <= 0)) {


Alert (' Please input proper month ');


return false;


}


Initialize after the year and month changes


Init_day_numbers (year, month);


});


},

           //click to change year and month
             buttonchange:function () {
                 $ (' #last-year '). Bind (' click ', Function ( ) {
                     var lastyear = parseint ($ (' #calendar_year '). Val ())-1 > 0? parseint ($ (' #calendar_year '). Val ())-1:1;
                     $ (' #calendar_year '). Val (lastyear);
                     init_day_numbers (lastyear, $ (' #calendar_month '). Val ());
               });

$ (' #last-month '). Bind (' click ', function () {


var lastmonth = parseint ($ (' #calendar_month '). Val ())-1 > 0? parseint ($ (' #calendar_month '). Val ())-1:12,


Thisyear = Lastmonth = = 12? parseint ($ (' #calendar_year '). Val ())-1: $ (' #calendar_year '). Val ();


$ (' #calendar_month '). Val (Lastmonth);


$ (' #calendar_year '). Val (thisyear);


Init_day_numbers (Thisyear, lastmonth);


});

$ (' #next-year '). Bind (' click ', function () {
var nextyear = parseint ($ (' #calendar_year '). Val ()) + 1;
$ (' #calendar_year '). Val (nextyear);
Init_day_numbers (Nextyear, $ (' #calendar_month '). Val ());
});

$ (' #next-month '). Bind (' click ', function () {


var nextmonth = parseint ($ (' #calendar_month '). Val ()) + 1 <= 12? parseint ($ (' #calendar_month '). Val ()) + 1:1,


Thisyear = Nextmonth = = 1? parseint ($ (' #calendar_year '). Val ()) + 1: $ (' #calendar_year '). Val ();


$ (' #calendar_month '). Val (Nextmonth);


$ (' #calendar_year '). Val (thisyear);


Init_day_numbers (Thisyear, nextmonth);


});


},

Set the relative offset between control and webpage, then display


Showcalendar:function () {


var o_x = Self.offset (). Left,


O_y = Self.offset (). Top + self[0].offsetheight;


$ (' #calendar '). CSS ({


' Position ': ' absolute ',


' Left ': o_x,


' Top ': o_y,


' Display ': ' Block '


});


},

Close Plugin
Closecalendar:function () {
$ (' #calendar '). Hide ();
},

Select Date


Choosedate:function () {


$ ('. Day '). Live (' click ', function () {


var _date = $ (this). attr (' data-date ');


Trigger callback function


if ($.isfunction (defaults.onselectdate)) {


Defaults.onSelectDate.call (this, _date);


}


if (defaults.autoclose) {


Calendaraction.closecalendar ();


}


});


}


};

Write calendar template in webpage
$ (' body '). Append (Calendardiv);

Initialize the year, month, day of the calendar list
Init_day_numbers (_date.year, _date.month);

Initialize plugin action
Calendaraction.initaction ();

Function init_day_numbers (year, month) {


var maxday = Getmaxday (year, month),


Firstweek,beforenumber,


StartIndex = Beforenumber = Firstweek = Getfirstweek (year, month),


Afternumber = 35-maxday-beforenumber,


Lastmonthmaxday = Getmaxdaybylastmonth (year, month),


Nextmonthmaxday = Getmaxdaybynextmonth (year, month),


Edge = Getlastandnextdate (year, month);


var default_html = ';


for (var k = 0; k < k++) {


default_html + = ' <div class= ' Day ' ><span class= ' day-number ' > ' + ' + ' </span></div> ';


};


$ ('. Days '). HTML (default_html);


for (var s = 1; s <= maxday; s++) {


$ ('. Day-number '). EQ (startIndex). text (s);


$ ('. Day '). EQ (startIndex). attr (' data-date ', year + '-' + month + '-' + s);


if (s = = _date.day && year = _date.year && month = = _date.month) {


$ ('. Day '), eq (startIndex). addclass (' Today ');


}


startindex++;


};


for (var k = 0; k < beforenumber; k++) {


var realday = lastmonthmaxday-beforenumber + k + 1;


$ ('. Day-number '). EQ (k). Text (Realday);


$ ('. Day-number '). EQ (k). addclass (' Last-month ');


$ ('. Day '). EQ (k). attr (' data-date ', Edge.lasty + '-' + edge.lastm + '-' + realday);


};


for (var L = 1; l <= afternumber l++) {


var _index = beforenumber + Maxday + (L-1);


$ ('. Day-number '). EQ (_index). Text (l);


$ ('. Day-number '). EQ (_index). addclass (' Next-month ');


$ ('. Day '). EQ (_index). attr (' data-date ', Edge.nexty + '-' + Edge.nextm + '-' + L ');


}


}

Function getlastandnextdate (year, month) {


var lastm = month-1 > 0? Month-1:12,


Lasty = Lastm = = 12? Year-1: Year,


NEXTM = month + 1 > 12? 1:month + 1,


Nexty = Nextm = = 1? Year + 1:year;


return {


Lastm:lastm,


Lasty:lasty,


NEXTM:NEXTM,


Nexty:nexty


};


}

Obtain what the ' a ' of one month is in a week
Function Getfirstweek (year, month) {
var date = new Date (year, month-1, 1);
return Date.getday ();
}

Obtain, month, day
function Gettoday () {
var date = new Date ();
return today = [Date.getfullyear (), Date.getmonth () + 1, date.getdate (), Date.getday ()];
}

Obtain the number of days for a month
Function Getmaxday (year, month) {
var date = new Date (year, month, 0);
return Date.getdate ();
}

Obtain the number of days for last month
Function Getmaxdaybylastmonth (year, month) {
month = month-1 <= 0? 12:month-1;
Year = month = 12? Year-1: Year;
Return Getmaxday (year, month);
}

Obtain the number of days for next month
Function Getmaxdaybynextmonth (year, month) {
Month = month + 1 > 12? 1:month + 1;
Year = month = 1? Year + 1:year;
Return Getmaxday (year, month);
}

Obtain the days list of a certain and month, return array


Function getdaylist (year, month) {


var _list = [],


Maxday = Getmaxday (year, month),


month = month-1;


for (var i = 1; I &lt;= maxday; i++) {


var d = new Date (year, month, I);


_list[i-1] = D.getday ();


}


return _list;


}


}


}) (jQuery, window);

/*


&lt;style type= "Text/css" &gt;


*{margin:0;padding:0;font-size:12px;font-family:inconsolata, Monaco, ' Andale Mono ', Courier;


. Cld_grid{width:300px;margin:auto;}


. Cld_week_box{background-color: #3883a3; width:100%;}


. header-day{float:left;padding:10px 0px;width:14.2857%;text-align:center;color:white;font-family: ' Microsoft Ya-hei '}


. Clearfix:after{content: "."; display:block;clear:both;visibility:hidden;line-height:0;height:0;}


. Day{float:left;


width:14.2857%;


height:13px;


padding:10px 0;


Text-align:center;


Color: #4f4f4f;


Background-color: #ebebeb;


border-bottom:2px solid white;


Background-image:url (' http://kylestetz.github.io/CLNDR/css/./triangle.svg ');


Background-size:cover;


Background-position:center;


Cursor:pointer;


}


. Last-month, days. Next-month {


opacity:0.3;


}


. Days. today{


Background: #fff;


}


. cld_year_month{


padding:10px;


Background-color: #71bbd2;


Text-align:center;


Color:white;


letter-spacing:1px;


font-size:14px;


}


. Cld_year_month Div{cursor:pointer;}


&lt;/style&gt;


*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.