王朝网络
分享
 
 
 

原生javascript-日历插件编写

王朝学院·作者佚名  2016-05-05  
宽屏版  字体: |||超大  

在线实例:http://lgy.1zwq.com/calendar/

按照我们常用的日历格式,是7*6的格子,所以生成格子的总数就确定为42

例子:(如:2013年8月,这个时间为例子)

/*----------生成日历格子:

-----------------------------------------------------*/

(1)获取本月的第一天是星期几--(为第三步做铺垫)

/*--注意:传递的月份是7,而不是8

------------------------------------*/

var first_day = new Date(2013,7,1).getDay();

(2)获取本月的最后一天是几号

var final_date = new Date(2013,8,0).getDate(); //下个月的0号,就是返回本月份最后一天的date

(3)上个月,在本月份显示的天数

这里我是用循环判断输出的,判断的依据就是 first_day(值为4),上个月的最后一天是几号,就是 new Date(2013,7,0).getDate()(值为31),然后就可以循环输出

for(){.........}

(4)下个月,在本月份后面显示的天数

var surplus = 42 - first_day - final_date; // 42-4-31 = 7;

//剩下的和第三步一样,也是循环输出

/*-------填充日历总代码:

-----------------------------------------------------------*/

复制代码

/*这里,写成传参数,为切换事件铺垫

-------------------*/

fillDate:function(year,month){

//本月份第一天是星期几 -为上个月的显示天数做铺垫

var first_day = new Date(year,month,1).getDay(),

//本月份最后一天是几号

final_date = new Date(year,month+1,0).getDate(),

//上个月的最后一天是几号

last_date = new Date(year,month,0).getDate(),

//剩余的格子数--即排在后面的格子数

surplus = 42 - first_day - final_date;

/*填充日历执行

---------------------------*/

var html = '';

//上个月的显示天数

for(var i=0;i<first_day;i++){

html+='<span class="g-calendar-grey">'+(last_date-(first_day-1)+i)+'</span>';

}

//本月的显示天数

for(var j=0;j<final_date;j++){

html+='<span>'+(j+1)+'</span>';

}

//下个月的显示天数

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

html+='<span class="g-calendar-grey">'+(k+1)+'</span>';

}

//fill

this.oBody.innerHTML = html;

}

复制代码

/*------------全部源代码--------------

----------------------------------------------------*/

复制代码

function LGY_calendar(option){

this.oWrap = this.getId(option.wrapId);

this.oHead = this.getByClassName('g-calendar-hd',this.oWrap)[0];

this.oBody = this.getByClassName('g-calendar-bd',this.oWrap)[0];

this.oTit = this.getByClassName('g-calendar-tit',this.oWrap)[0];

this.oPRev = this.getByClassName('g-calendar-prev',this.oWrap)[0];

this.oNext = this.getByClassName('g-calendar-next',this.oWrap)[0];

this.init();

}

LGY_calendar.prototype = {

///////////获取ID元素

getId:function(id){

return document.getElementById(id);

},

////////获取CSS类名元素

getByClassName:function(className,parent){

var elem = [],

node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),

p = new RegExp("(^|\\s)"+className+"(\\s|$)");

for(var n=0,i=node.length;n<i;n++){

if(p.test(node[n].className)){

elem.push(node[n]);

}

}

return elem;

},

//填充日历

fillDate:function(year,month){

//本月份第一天是星期几-为显示上个月的天数做铺垫

var first_day = new Date(year,month,1).getDay(),

//如果刚好是星期天,则空出一行(显示上个月的天数)

first_day = first_day == 0?first_day=7:first_day;

//本月份最后一天是几号

final_date = new Date(year,month+1,0).getDate(),

//上个月的最后一天是几号

last_date = new Date(year,month,0).getDate(),

//剩余的格子数--即排在末尾的格子数

surplus = 42 - first_day - final_date;

/*设置表头的日历

---------------------------*/

this.oHead.innerHTML = year+'年'+(month+1)+'月';

/*填充日历执行

---------------------------*/

var html = '';

//上个月的显示天数

for(var i=0;i<first_day;i++){

html+='<span class="g-calendar-grey">'+(last_date-(first_day-1)+i)+'</span>';

}

//本月的显示天数

for(var j=0;j<final_date;j++){

html+='<span>'+(j+1)+'</span>';

}

//下个月的显示天数

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

html+='<span class="g-calendar-grey">'+(k+1)+'</span>';

}

//fill

this.oBody.innerHTML = html;

// 当前状态

if(year==this.c_year&&this.c_month==month){

this.oBody.getElementsByTagName('span')[first_day+this.date-1].className='g-calendar-on';

}

},

// next切换

next:function(){

var _that = this;

this.oNext.onclick = function(){

_that.month++;

if(_that.month>11){

_that.month = 0;

_that.year++;

}

// 填充日历

_that.fillDate(_that.year,_that.month);

}

},

// prev切换

prev:function(){

var _that = this;

this.oPrev.onclick = function(){

_that.month--;

if(_that.month<0){

_that.month = 11;

_that.year--;

}

// 填充日历

_that.fillDate(_that.year,_that.month);

}

},

init:function(){

this.oTit.innerHTML = '<span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span>';

// 获取今天的日历时间

var now = new Date();

this.c_year = this.year = now.getFullYear();

this.c_month = this.month = now.getMonth();

this.date = now.getDate();

// 初始化--填充日历

this.fillDate(this.year,this.month);

//next切换

this.next();

//prev切换

this.prev();

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有