原生Js实现日历挂件

2022-07-24,,

本文实例为大家分享了js实现日历挂件的具体代码,供大家参考,具体内容如下

css code

/*************************
 * 日历样式对应表
 * #date 日历块 
 * table 表格
 * th 头部
 * td 身体
 * a.now 本月
 * a.non-arrival 其他月
 * a.day 今天
 * a.href 链接
 * #date_diglogs 记住对话框
 *************************/
 
#date {
 width: 220px;
 padding-bottom: 5px;
 box-shadow: 0 1px 3px #ccc;
 border: 1px solid #ededed;
}
 
#date table {
 width: inherit;
 user-select: none;
 font-size: 12px;
 border-collapse: collapse;
 border-spacing: 0px;
}
 
#date table tr th {
 background-color: #f8f8f8;
 color: #5e5f63;
}
 
#date table tr:nth-of-type(2) th {
 font-weight: 300;
}
 
#date table tr td {
 text-align: center;
 font-family: "comic sans ms";
 padding: 2px 0;
}
 
#date table tr td a {
 text-decoration: none;
}
 
#date table tr td a.now {
 color: #757575;
}
 
#date table tr td a.day {
 background: mediumblue;
 text-decoration: underline;
 color: #fff;
}
 
#date table tr td a.href {
 border: 1px solid #ccc;
 transition: all 1s linear;
}
 
#date table tr td a.href:hover {
 border: 1px dotted #5e5f63;
 background: gold;
}
 
#date table tr td a.non-arrival {
 color: #ccc;
} 
 
.date_diglogs {
 font-size: 10px;
 background: #fff;
 padding: 2px 5px;
 border-radius: 3px;
 box-shadow: 0 1px 3px #ccc;
 border: 1px solid #ededed;
 color: #757575;
}

js code

/* 2021/2/26
 * 功能: 日历挂件
 * 清源妙善
 */
 
function blogdate(nowdate) {
 /* 可变数据 */
 this.year = nowdate.getfullyear(); // 获取年份
 this.month = nowdate.getmonth(); // 获取月份
 this.day = nowdate.getdate(); // 获取今天是几号
 this.week = new date(this.year, this.month, 1).getday(); // 获取每月前面的空余天数
 this.days = new date(this.year, this.month + 1, -1).getdate() + 1; // 获取总共有几天
 this.last_month = new date(this.year, this.month, -1).getdate() + 1; // 保存上个月的天数 
 
 /* 不变数据 */
 this.now_year = nowdate.getfullyear(); // 保存今年的年份
 this.now_month = nowdate.getmonth(); // 保存今年的月份
}
 
blogdate.prototype.createdate = function(name) {
 // 获取块与创建表格
 let date_div = document.getelementbyid('date');
 let date_table = document.createelement('table');
 date_div.appendchild(date_table);
 
 // 创建所有的 tr 标签
 let date_all_tr = new array();
 for (let n = 0; n < 8; n++) {
 date_all_tr[n] = document.createelement('tr');
 date_table.appendchild(date_all_tr[n]);
 }
 
 // 创建头部th标签
 let date_head_th = new array();
 for (let n = 0; n < 3; n++) {
 date_head_th[n] = document.createelement('th');
 date_all_tr[0].appendchild(date_head_th[n]);
 }
 
 // 设置特殊元素属性
 date_head_th[0].setattribute('id', 'prev');
 date_head_th[1].setattribute('colspan', '5');
 date_head_th[1].setattribute('title', `${name}`);
 date_head_th[2].setattribute('id', 'next');
 
 // 创建星期 th 标签
 let date_week_th = new array();
 for (let n = 0; n < 7; n++) {
 date_week_th[n] = document.createelement('th');
 date_all_tr[1].appendchild(date_week_th[n]);
 }
 
 // 创建身体 td, a 标签数组
 let date_body_td = new array();
 let date_body_td_a = new array();
 
 // 创建身体 td, a 标签实体
 for (let n = 2, i = 0; n < 8; n++, i++) {
 date_body_td[i] = [];
 date_body_td_a[i] = [];
 
 for (let m = 0; m < 7; m++) {
 date_body_td[i][m] = document.createelement('td');
 date_body_td_a[i][m] = document.createelement('a');
 date_body_td[i][m].appendchild(date_body_td_a[i][m]);
 date_all_tr[n].append(date_body_td[i][m]);
 }
 }
}
 
blogdate.prototype.setblogdate = function(newdate) {
 /* 更新数据 */
 this.year = newdate.getfullyear(); // 获取年份
 this.month = newdate.getmonth(); // 获取月份
 this.day = newdate.getdate(); // 获取今天是几号
 this.week = new date(this.year, this.month, 1).getday(); // 获取每月前面的空余天数
 this.days = new date(this.year, this.month + 1, -1).getdate() + 1; // 获取总共有几天
 this.last_month = new date(this.year, this.month, -1).getdate() + 1; // 获取上个月的天数
}
 
blogdate.prototype.updatetime = function(blogs_date) {
 // 获取日历对象
 let date_div = document.getelementbyid('date');
 let date_table = date_div.getelementsbytagname('table')[0];
 
 // 创建日历头部 tr, th
 let date_head_tr = date_table.getelementsbytagname('tr')[0];
 let date_head_th = date_head_tr.getelementsbytagname('th');
 
 // 创建头部数据
 let date_head_arr = [
 '<', `${this.year} 年 ${this.month + 1} 月`, '>'
 ];
 
 // 更新头部数据
 for (let n = 0; n < date_head_th.length; n++) {
 date_head_th[n].textcontent = date_head_arr[n];
 }
 
 // 创建星期部分 tr, th
 let date_week_tr = date_table.getelementsbytagname('tr')[1];
 let date_week_th = date_week_tr.getelementsbytagname('th');
 
 // 创建星期数据
 let date_week_arr = [
 '日', '一', '二', '三', '四', '五', '六'
 ];
 
 // 更新星期数据
 for (let n = 0; n < date_week_th.length; n++) {
 date_week_th[n].textcontent = date_week_arr[n];
 }
 
 // 获取身体 td 的 a 标签
 let date_body_td_a = date_table.getelementsbytagname('a');
 
 // 设置其他月份的天数 ( 前 )
 for (let n = this.week - 1, last_month = this.last_month; n >= 0; n--, last_month--) {
 date_body_td_a[n].textcontent = last_month;
 date_body_td_a[n].setattribute('class', 'non-arrival');
 }
 
 // 设置现在月份的天数 ( 现 )
 for (let n = this.week, i = 1; i <= this.days; n++, i++) {
 date_body_td_a[n].textcontent = i;
 // 如果今年今月今日, 设置 day 样式, 其余 now 样式
 if ((i == this.day) &&
 (new date(this.year, this.month, 1).getmonth() == this.now_month) &&
 (new date(this.year, this.month, 1).getfullyear() == this.now_year)) {
 date_body_td_a[n].setattribute('class', 'day');
 } else {
 date_body_td_a[n].setattribute('class', 'now');
 }
 }
 
 // 设置其他月份的天数 ( 后 )
 for (let n = this.week + this.days, i = 1; n < date_body_td_a.length; n++, i++) {
 date_body_td_a[n].textcontent = i;
 date_body_td_a[n].setattribute('class', 'non-arrival');
 }
 
 // 如果链接部分日期数据相同, 设置对应样式
 for (let n = 0; n < date_body_td_a.length; n++) {
 for (let m = 0; m < blogs_date.href_num; m++) {
 if ((this.year == blogs_date.href_year[m]) &&
 (this.month + 1 == blogs_date.href_month[m]) &&
 (n == blogs_date.href_day[m])) {
 date_body_td_a[n].setattribute('href', blogs_date.href_url[m]);
 date_body_td_a[n].classlist.add('href');
 date_body_td_a[n].setattribute('target', '_blank');
 } else {
 // 如果不是则判断是否存在多余属性
 if (boolean(date_body_td_a[n].getattribute('target')) &&
 boolean(date_body_td_a[n].getattribute('href')) &&
 (date_body_td_a[n].getattribute('class') == 'now' ||
 date_body_td_a[n].getattribute('class') == 'non-arrival')) {
 date_body_td_a[n].removeattribute('href');
 date_body_td_a[n].removeattribute('target');
 }
 }
 }
 }
}
 
function initdate(
 // 默认日历参数表
 blogs_date = {
 blogs_name: '我的日历',
 href_year: [2021],
 href_month: [2],
 href_day: [26],
 href_url: ['http://www.4399.com/'],
 href_prompt: ['这是我编写的日历挂件'],
 href_dialog: false,
 href_num: undefined
 }
) {
 // 参数长度是否相等
 if ((blogs_date.href_day.length != blogs_date.href_month.length) ||
 (blogs_date.href_month.length != blogs_date.href_year.length) ||
 (blogs_date.href_year.length != blogs_date.href_url.length)) {
 console.info('日历参数长度不等');
 return false;
 }
 // 参数长度相同, 设置对应长度
 else {
 blogs_date.href_num = blogs_date.href_day.length;
 }
 
 // 创建日历数据
 let timedate = new date();
 let blogdate = new blogdate(timedate);
 
 // 创建日历实体
 blogdate.createdate(blogs_date.blogs_name);
 blogdate.updatetime(blogs_date);
 
 // 添加 prev 事件
 document.getelementbyid('prev').onclick = function() {
 timedate.setmonth(timedate.getmonth() - 1);
 blogdate.setblogdate(timedate);
 blogdate.updatetime(blogs_date);
 }
 
 // 添加 next 事件
 document.getelementbyid('next').onclick = function() {
 timedate.setmonth(timedate.getmonth() + 1);
 blogdate.setblogdate(timedate);
 blogdate.updatetime(blogs_date);
 }
 
 opendialogs(blogs_date);
 
 showblogsdata(blogs_date, timedate);
}
 
function showblogsdata(blogs_date, now) {
 for (let k in blogs_date) {
 console.info(`[${k}] : ${blogs_date[k]}`);
 }
 console.info(`blogsdate ok ${now}`);
}
 
function opendialogs(blogs_date) {
 // 是否开启对话框
 switch (blogs_date.href_dialog) {
 case true:
 let hrefid = document.getelementsbyclassname('href');
 for (let n = 0; n < hrefid.length; n++) {
 hrefid[n].onmouseover = function(e) {
 if (this.getattribute('class') != 'now' &&
 this.getattribute('class') != 'non-arrival') {
 
 var e = e || window.event;
 
 let x = e.clientx;
 let y = e.clienty;
 
 let prompt = blogs_date.href_prompt[n];
 let dialogs = document.createelement('div');
 
 dialogs.classlist.add('date_diglogs');
 dialogs.textcontent = prompt;
 dialogs.style.csstext = `position: absolute;
 left: ${x-20}px;
 top: ${y+20}px`;
 
 document.body.appendchild(dialogs);
 }
 }
 hrefid[n].onmouseout = function() {
 if (this.getattribute('class') != 'now' &&
 this.getattribute('class') != 'non-arrival') {
 
 let diglogs = document.getelementsbyclassname('date_diglogs')[0];
 document.body.removechild(diglogs);
 }
 }
 }
 break;
 case false:
 break;
 }
} 

html code

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title>date html</title>
 <link rel="stylesheet" href="date.css" media="screen">
 </head>
 <body>
 <h3>hello</h3>
 <div id="date"></div>
 <script src="date.js"></script>
 <script>
 initdate(blogs_date = {
 blogs_name : '我的日历',
 href_year : [2021, 2021], 
 href_month : [2, 2], 
 href_day : [27, 3], 
 href_url : ['http://www.4399.com/', 'http://www.baidu.com/'],
 href_prompt: ['今天要出门看亲人', '今天要早睡'],
 href_dialog: true
 });
 </script>
 </body>
</html>

效果

参考链接:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《原生Js实现日历挂件.doc》

下载本文的Word格式文档,以方便收藏与打印。