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

Example code for implementing transparent gradient effects with CSS

The title images on Zhihu Discovery columns are g...

Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14

1. RTMP RTMP streaming protocol is a real-time au...

How to prevent event bubbling in JavaScript

What we need to pay attention to is that the char...

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and...

Key points for writing content of HTML web page META tags

The META tag is an auxiliary tag in the head area...

Book page turning effects made with CSS3

Result:Implementation code: html <!-- Please h...

How to use Element in React project

This is my first time using the element framework...

Example of how to generate random numbers and concatenate strings in MySQL

This article uses an example to describe how MySQ...

Full HTML of the upload form with image preview

The upload form with image preview function, the ...

How InnoDB implements serialization isolation level

Serialization implementation InnoDB implements se...

impress.js presentation layer framework (demonstration tool) - first experience

I haven’t blogged for half a year, which I feel a ...

How to quickly set the file path alias in react

React is a JavaScript library for building user i...

Detailed explanation of the usage and differences of MySQL views and indexes

MySQL Views Simply put, a MySQL view is a shortcu...

Detailed process of modifying hostname after Docker creates a container

There is a medicine for regret in the world, as l...