Home >Web Front-end >JS Tutorial >Make a calendar based on jQuery calendar plug-in_jquery
Let’s take a look at the final rendering:
I’m a bit ugly, don’t complain about me-. -
First, let’s talk about the main production logic of this calendar:
·A month has a maximum of 31 days, and a 7X6 table is needed to load it
·If you know what day of the week the 1st of a certain month is and how many days there are in this month, you can display the calendar of a certain month in a loop (your eyes are shining*.*)
·Add some controls to make it easier for users to operate (for example, you can enter the year and month, and you can click to select the year and month)
Create a new html file, html structure:
<div class="container"> <input type="text" value="" id="cal-input"/> <div class="cal-box"> <table> <thead> <tr> <td class="sun">日</td> <td>一</td> <td>二</td> <td>三</td> <td>四</td> <td>五</td> <td class="sta">六</td> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </div>
Add some styles and open the browser to see the effect:
thead td,tbody td{ width: 20px; height: 20px;<br><span class="styles-clipboard-only"> <span class="webkit-css-property">text-align: <span class="expand-element"><span class="value">center;</span></span></span></span> } thead td.sun,thead td.sta{ color: #eec877; } tbody td{ border: 1px solid #eee; }
It looks good, but this is a plug-in. It is unreasonable to write so much html code. It should be dynamically inserted inside the plug-in. It is also written like this for intuitive demonstration.
We are about to start writing JS code. Now we need to know what day of the week the 1st of a certain month is, so that we can traverse and display the calendar of a certain month. The Zeiler formula is used here
PS: A brief explanation, Zeiller's formula: var week = y + parseInt(y/4) + parseInt(c/4) - 2*c + parseInt(26*(m+1 )/10) + d - 1;
c is the first two digits of the year, y is the last two digits of the year (in 2016, c is 20, y is 16), m is the month, d is the date, and after adding week%7, the result is the week How many
However, January and February must be calculated as March and April of the previous year. For example, 2016.2.3 must be converted to 2015.14.3 using the Zeiler formula
The modulus is different when week is a positive number and a negative number. When it is a negative number, (week%7+7)%7 is required. When it is a positive number, it is directly modulo week%7,
You also need to know how many days there are in this month. January, March, May, July, August, October and December have 31 days, April, June, September and November have 30 days. February is a leap year and a peace year. There are 28 days in a normal year, and 29 days in a leap year. A leap year can be evenly divisible by 4 but not by 100. Now that you have some prerequisites, you can still write JS quickly
$(function(){ var $td = $('tbody').find('td'); var date = new Date(), year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(),days; function initCal(yy,mm,dd){ if(mm ==2 && yy%4 == 0 && yy%100 !==0 ){ days = 28; }else if(mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12){ days = 31; }else if(mm==4 || mm==6 || mm==9 || mm==11 ){ days = 30; }else{ days = 29; } var m = mm < 3 ? (mm == 1 ? 13 : 14): mm; yy = m > 12 ? yy - 1 : yy; var c = Number(yy.toString().substring(0,2)), y = Number(yy.toString().substring(2,4)), d = 1; //蔡勒公式 var week = y + parseInt(y/4) + parseInt(c/4) - 2*c + parseInt(26*(m+1)/10) + d - 1; week = week < 0 ? (week%7+7)%7 : week%7; for(var i=0 ;i<42;i++){ $td.eq(i).text(''); //清空原来的text文本 } for(var i = 0;i < days; i++){ $td.eq( week % 7 +i).text(i+1); } } initCal(year,month,day); })
Open the browser again and take a look. The calendar now looks like this
Open the calendar on your phone and take a look. It is March 2016. Well, it looks exactly the same (smug face)
Now we need to add some controls, two input boxes and four buttons. The buttons use iconfont. The html code is as follows:
<div class="container"> <input type="text" value="" id="cal-input"/> <div class="cal-box"> <div class="cal-control-box"> <div class="wif iw-bofangqixiayiqu left"></div> <div class="wif iw-iconfont-bofang left"></div> <input type="" value=""/> <span>年</span> <input type="" value=""/> <div class="wif iw-iconfont-bofang right"></div> <div class="wif iw-bofangqixiayiqu right"></div> </div> <table> <thead> <tr> <td class="sun">日</td> <td>一</td> <td>二</td> <td>三</td> <td>四</td> <td>五</td> <td class="sta">六</td> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </div> </div>
This is what the calendar looks like now
Now let’s bind click events to the buttons and bind the change event to the input box
//更改月份按钮 $(document).on("click",".iw-iconfont-bofang",function(){ if($(this).hasClass("left")){ //判断加还是减 if(month == 1 ){ month = 12; year--; }else{ month--; } }else{ if(month == 12){ month = 1; year ++; }else{ month ++; } } initCal(year,month,day); }) //更改年份 $(document).on("click",".iw-bofangqixiayiqu",function(){ if($(this).hasClass("left")){ year--; }else{ year++; } initCal(year,month,day); }) //年份输入 $(document).on("change","input.cal-year",function(){ year = $(this).val(); initCal(year,month,day); }) //月份输入 $(document).on("change","input.cal-month",function(){ month = $(this).val(); initCal(year,month,day); })
By the way, in the initCal() function, you need to use JQ’s val() method to put the year and month values into the input box.
Conclusion: This is not written in the form of a plug-in, but the main ideas for the implementation of this calendar have been written. I have been busy writing my graduation thesis recently, and there are many things I want to write down and share. I always feel that there is no time. It’s not enough. Next time I will write about how to write this calendar as a chrome plug-in, which is the one below
I hope this article will be helpful to jquery programming.