JS implements simple calendar effect

JS implements simple calendar effect

This article shares the specific code of JS to achieve a simple calendar effect for your reference. The specific content is as follows

CSS

* {
  margin: 0;
  padding: 0;
  list-style: none;
 }

 #box {
  width: 280px;
  height: 360px;
  margin: 50px auto;
  background-color: black;
  color: aliceblue;
  line-height: 40px;
 }

 #header {
  height: 40px;
  color: aliceblue;
  line-height: 40px;
 }

 #header span {
  float: left;
  text-align: center;
  margin-top: 10px;
  line-height: 40px;
 }

 #prev,
 #next {
  width: 20%;
  line-height: 40px;
  cursor: pointer;
 }

 #current {
  width: 60%;
  line-height: 40px;
 }

 #week li {
  width: 40px;
  text-align: center;
  float: left;
  line-height: 40px;
 }

 #content li {
  width: 40px;
  text-align: center;
  float: left;
  line-height: 40px;
}

html

<div id="box">
 <div id="header">
  <span id="prev">Prev</span>
  <span id="current"></span>
  <span id="next">Next</span>
 </div>
 <ul id="week">
  <li>Day</li>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
 </ul>
 <ul id="content">
  <!-- <li>31</li>
    <li>1</li>
    <li>2</li> -->
 </ul>
</div>```

js

var current = document.querySelector('#current');//month name
 var prev = document.querySelector('#prev'); // last month var next = document.querySelector('#next'); // next month var content = document.querySelector('#content'); // date content // the number of days to display in the previous month // find out which day of the week the first day of this month is // find out the maximum number of days in the previous month and set the date to 0
 function getPrevDays(date) {
  var date = new Date(date);
  // Set the date to the first day, in order to get the day of the week date.setDate(1);
  var week = date.getDay();
  // Set the date to 0 to get the last day of the previous month date.setDate(0);
  var maxDay = date.getDate();
  var list = [];
  // Traverse the range of red dates and push them into the array for (var i = maxDay - week + 1; i <= maxDay; i++) {
  list.push(i);
  }
  return list;
 }


 // Find the number of days in this month // Push the month to the next month // Set the date to 0
 function getNowDays(date) {
  var date = new Date(date);
  date.setMonth(date.getMonth() + 1);
  date.setDate(0);
  var maxDay = date.getDate();
  // console.log(maxDay)
  var list = [];
  // 
  for (var i = 1; i <= maxDay; i++) {
  list.push(i)
  }
  return list;
 }



 // The number of days to display in the next month function getNextDays(prevDays, nowDays) {
  var list = [];
  // One page calendar 42 days, 42 - last month's days - this month's days = finally display the remaining days of next month for (var i = 1; i <= 42 - prevDays - nowDays; i++) {
  list.push(i)
  }
  return list
 }

 var x = 1;
 // Encapsulate the output date content // x records the clicked month and automatically obtains the time to be displayed in that month according to the array above the month function output(x) {
  arr1 = getPrevDays('2021-' + x);
  arr2 = getNowDays('2021-' + x);
  arr3 = getNextDays(arr1.length, arr2.length);
  // console.log(arr2);
  var res = '';
  for (var i = 0; i < arr1.length; i++) {
  res += '<li style="color:red;">';
  res += arr1[i];
  res += '</li>';
  }

  for (var i = 0; i < arr2.length; i++) {
  res += '<li>';
  res += arr2[i];
  res += '</li>';
  }

  for (var i = 0; i < arr3.length; i++) {
  res += '<li style="color:red;">';
  res += arr3[i];
  res += '</li>';
  }
  // Concatenate the output results of the three arrays and output return content.innerHTML = res;
 }




 // Output month display var date = new Date();
 current.innerHTML = showMonth(new Date());
 // Month function showMonth(date) {
  var date = new Date(date);
  date.setMonth(date.getMonth());
  var mon = date.getMonth();
  // var year = date.getFullyear();
  return (mon + 1) + 'Month';
 }

 output(x);
 // Next month next.onclick = function () {
  x++;
  // console.log(x);
  if (x > 12) {
  x = 1;
  output(x);
  } else {
  current.innerHTML = showMonth('2021-' + x);
  output(x);
  }
 }

 // Last month prev.onclick = function () {
  x--;
  console.log(x);
  if (x < 1) {
  x = 12;
  current.innerHTML = showMonth('2021-' + x);
  output(x);
  } else {
  current.innerHTML = showMonth('2021-' + x);
  output(x);
  }
 }

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.

<<:  What to do if you forget the root password of Mysql5.7 (simple and effective method)

>>:  How to bind domain name to nginx service

Recommend

Solution to inserting a form with a blank line above and below

I don't know if you have noticed when making a...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...

Use of MySQL query rewrite plugin

Query Rewrite Plugin As of MySQL 5.7.6, MySQL Ser...

Mysql implements three functions for field splicing

When exporting data to operations, it is inevitab...

Vue.js framework implements shopping cart function

This article shares the specific code of Vue.js f...

Python writes output to csv operation

As shown below: def test_write(self): fields=[] f...

Native Js implementation of calendar widget

This article example shares the specific code of ...

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In ...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

Detailed tutorial on building an ETCD cluster for Docker microservices

Table of contents Features of etcd There are thre...

How to delete garbled or special character files in Linux

Due to encoding reasons, garbled characters will ...

Detailed explanation of Linux inotify real-time backup implementation method

Real-time replication is the most important way t...

How to disable foreign key constraint checking in MySQL child tables

Prepare: Define a teacher table and a student tab...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

CSS new feature contain controls page redrawing and rearrangement issues

Before introducing the new CSS property contain, ...