Native Js implementation of calendar widget

Native Js implementation of calendar widget

This article example shares the specific code of js to implement the calendar widget for your reference. The specific content is as follows

Css code

/*****************************
 * Calendar style corresponding table* #date calendar block* table table* th header* td body* a.now this month* a.non-arrival other months* a.day today* a.href link* #date_diglogs remember dialog****************************/
 
#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: Calendar pendant* Qingyuan Miaoshan*/
 
function BlogDate(nowDate) {
 /* Variable data */
 this.year = nowDate.getFullYear(); // Get the yearthis.month = nowDate.getMonth(); // Get the monththis.day = nowDate.getDate(); // Get what day it is todaythis.week = new Date(this.year, this.month, 1).getDay(); // Get the number of free days at the beginning of each monththis.days = new Date(this.year, this.month + 1, -1).getDate() + 1; // Get the total number of daysthis.last_month = new Date(this.year, this.month, -1).getDate() + 1; // Save the number of days in last month/* unchanged data*/
 this.now_year = nowDate.getFullYear(); // Save this year's year this.now_month = nowDate.getMonth(); // Save this year's month }
 
BlogDate.prototype.createDate = function(name) {
 // Get blocks and create tables let date_div = document.getElementById('date');
 let date_table = document.createElement('table');
 date_div.appendChild(date_table);
 
 // Create all tr ​​tags 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]);
 }
 
 // Create the head th tag 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]);
 }
 
 // Set special element attributes 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');
 
 // Create the week th label 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]);
 }
 
 // Create body td, a tag array let date_body_td = new Array();
 let date_body_td_a = new Array();
 
 // Create body td, a tag entity 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) {
 /* Update data */
 this.year = newDate.getFullYear(); // Get the yearthis.month = newDate.getMonth(); // Get the monththis.day = newDate.getDate(); // Get today's datethis.week = new Date(this.year, this.month, 1).getDay(); // Get the number of free days at the beginning of each monththis.days = new Date(this.year, this.month + 1, -1).getDate() + 1; // Get the total number of daysthis.last_month = new Date(this.year, this.month, -1).getDate() + 1; // Get the number of days in last month}
 
BlogDate.prototype.updateTime = function(blogs_date) {
 // Get the calendar object let date_div = document.getElementById('date');
 let date_table = date_div.getElementsByTagName('table')[0];
 
 // Create calendar header tr, th
 let date_head_tr = date_table.getElementsByTagName('tr')[0];
 let date_head_th = date_head_tr.getElementsByTagName('th');
 
 // Create header data let date_head_arr = [
 '<', `${this.year} year ${this.month + 1} month`, '>'
 ];
 
 // Update header data for (let n = 0; n < date_head_th.length; n++) {
 date_head_th[n].textContent = date_head_arr[n];
 }
 
 // Create the week part tr, th
 let date_week_tr = date_table.getElementsByTagName('tr')[1];
 let date_week_th = date_week_tr.getElementsByTagName('th');
 
 // Create week data let date_week_arr = [
 'Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'
 ];
 
 // Update week data for (let n = 0; n < date_week_th.length; n++) {
 date_week_th[n].textContent = date_week_arr[n];
 }
 
 // Get the a tag of body td let date_body_td_a = date_table.getElementsByTagName('a');
 
 // Set the number of days in other months (front)
 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');
 }
 
 // Set the number of days in the current month (current)
 for (let n = this.week, i = 1; i <= this.days; n++, i++) {
 date_body_td_a[n].textContent = i;
 // If it is this day of this month, set the day style, otherwise set the now style 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');
 }
 }
 
 // Set the number of days in other months (later)
 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');
 }
 
 // If the date data in the link part is the same, set the corresponding style 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 not, determine whether there are redundant attributes 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(
 // Default calendar parameter table blogs_date = {
 blogs_name: 'My Calendar',
 href_year: [2021],
 href_month: [2],
 href_day: [26],
 href_url: ['http://www.4399.com/'],
 href_prompt: ['This is the calendar widget I wrote'],
 href_dialog: false,
 href_num: undefined
 }
) {
 // Are the parameter lengths equal 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('Calendar parameter lengths are not equal');
 return false;
 }
 // The parameter lengths are the same, set the corresponding length else {
 blogs_date.href_num = blogs_date.href_day.length;
 }
 
 // Create calendar data let timeDate = new Date();
 let blogDate = new BlogDate(timeDate);
 
 // Create calendar entity blogDate.createDate(blogs_date.blogs_name);
 blogDate.updateTime(blogs_date);
 
 // Add prev event document.getElementById('prev').onclick = function() {
 timeDate.setMonth(timeDate.getMonth() - 1);
 blogDate.setBlogDate(timeDate);
 blogDate.updateTime(blogs_date);
 }
 
 // Add next event 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) {
 // Whether to open the dialog 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 : 'My Calendar',
 href_year : [2021, 2021], 
 href_month : [2, 2], 
 href_day : [27, 3], 
 href_url : ['http://www.4399.com/', 'http://www.baidu.com/'],
 href_prompt: ['I have to go out to see my family today', 'I have to go to bed early today'],
 href_dialog: true
 });
 </script>
 </body>
</html>

Effect

Reference link: jQuery calendar effect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A guide to using calendar.js, a lightweight native js calendar plugin
  • JS learning a simple calendar control
  • js calendar control (accurate to the minute)
  • PHP+javascript calendar control
  • Simple JS calendar control example code
  • Modify js Calendar control to be compatible with IE9/Google/Firefox
  • How to use the calendar control and JS version of the calendar control in ASP.NET (Section 5)
  • New algorithm for JavaScript blog-style calendar control
  • Support IE, Firefox javascript calendar control
  • Sample code for implementing calendar component with native JS

<<:  How to create, save, and load Docker images

>>:  Detailed steps to install mysql5.7.18 on Mac

Recommend

Solution to 1067 when Mysql starts in Windows

I just started working a few days ago and install...

Reduce memory and CPU usage by optimizing web pages

Some web pages may not look large but may be very ...

Let's talk about MySQL joint query in detail

Table of contents Union query 1. Query the ID and...

Mysql join query principle knowledge points

Mysql join query 1. Basic concepts Connect each r...

Two ways to specify the character set of the html page

1. Two ways to specify the character set of the h...

How to use axios to filter multiple repeated requests in a project

Table of contents 1. Introduction: In this case, ...

One question to understand multiple parameters of sort command in Linux

The sort command is very commonly used, but it al...

Detailed explanation of MySQL master-slave replication and read-write separation

Table of contents Preface 1. Overview 2. Read-wri...

Let the web page redirect to other pages after opening for a few seconds

Just add the following code to achieve it. Method ...

JavaScript Basics Series: Functions and Methods

Table of contents 1. The difference between funct...

JS array loop method and efficiency analysis comparison

Array Methods JavaScript has provided many array ...

MySQL 8.0 can now handle JSON

Table of contents 1. Brief Overview 2. JSON basic...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...