js to achieve simple calendar effect

js to achieve 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 module <style type="text/css">
  *{
   margin: 0;
   padding: 0;
  }
  .date{
   width: 300px;
   height: 220px;
   border: 1px solid #000;
   margin: 100px auto;
  }
  .title{
   width: 200px;
   display: flex;
   font-size: 12px;
   margin: auto;
   text-align: center;
   justify-content: space-around;
   align-items: center;
  }
  .year{
   margin: 0 40px;
   display: flex;
   flex-direction: column;
  }
  #week{
   border-top: 1px solid #000;
   border-bottom: 1px solid #000;
   margin: auto;
   list-style-type: none;
   display: flex;
  }
  #week li{
   display: inline-block;
   text-align: center;
   flex:1;
  }
  #ul{
   list-style-type: none;
   margin-top: 5px;
  }
  #ul li {
   display: inline-block;
   width: 40px;
   height: 21px;
   text-align: center;
   border: 1px solid #fff;
  }
  .current{
   color:red;
  }
  #ul li:hover{
   border: 1px solid red;
  }
  #prev,#next{
   cursor: pointer;
  }
 </style>

##html
<div class="date">
  <div class="title">
   <span id="prev">&lt;Previous month</span>
   <div class="year">
    <span id="year">2021</span>
    <span id="month">May</span>
   </div>
   <span id="next">Next month&gt;</span>
  </div>
  <!-- Use ul to make a calendar -->
  <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="ul">
   
  </ul>
</div>
## js code <script type="text/javascript">
  // Date object, convenient for switching months, so set it as global let date = new Date();
  // Click to switch the month event document.getElementById('prev').addEventListener('click',function(){
   date.setMonth(date.getMonth()-1);
   add();
  })
  document.getElementById('next').addEventListener('click',function(){
   date.setMonth(date.getMonth()+1);
   add();
  })
  add();
  
  //Function to create calendar function add(){
   // Current year let cYear = date.getFullYear();
   // Current month let cMonth = date.getMonth()+1;
   // Get the current date let cDay = date.getDate();
   
   // Write the year and month document.getElementById('year').innerHTML = cYear;
   document.getElementById('month').innerHTML = cMonth+'月';
   
   
   let days = new Date(cYear,cMonth,-1);
   // The number of days in the current month let n = days.getDate()+1;
   // What day of the week is the first day of each month let week = new Date(cYear,cMonth-1,1).getDay();
   let html = '';
   //Write to dom
   for(let i=0;i<week;i++){
    html+=`<li></li>`
   }
   for(let i=1;i<=n;i++){
    if(i==cDay){
     html+=`<li class="current">${i}</li>`
    }else{
     html+=`<li>${i}</li>`
    }
   }
   // Insert once document.getElementById('ul').innerHTML = html
  }
</script>

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:
  • js css+html to realize a simple calendar
  • A guide to using calendar.js, a lightweight native js calendar plugin
  • JS learning a simple calendar control
  • Vue.js creates Calendar effect
  • JS Calendar Recommendation
  • Pure js simple calendar implementation code
  • js writes a simple calendar effect for the day [implementation code]
  • js calendar control (accurate to the minute)
  • PHP+javascript calendar control
  • Simple JS calendar control example code

<<:  Complete steps for Nginx to configure anti-hotlinking

>>:  Solution to forgetting mysql password under linux

Recommend

Use of filter() array filter in JS

Table of contents 1. Introduction 2. Introduction...

A brief talk about cloning JavaScript

Table of contents 1. Shallow cloning 2. Deep clon...

js implements the algorithm for specifying the order and amount of red envelopes

This article shares the specific code of js to im...

Two ways to install the Linux subsystem in Windows 10 (with pictures and text)

Windows 10 now supports Linux subsystem, saying g...

How to implement element floating and clear floating with CSS

Basic Introduction to Floating In the standard do...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Implementing Binary Search Tree in JavaScript

The search binary tree implementation in JavaScri...

CSS3 uses the transition property to achieve transition effects

Detailed description of properties The purpose of...

CSS to achieve glowing text and a little bit of JS special effects

Implementation ideas: Use text-shadow in CSS to a...

Using cursor loop to read temporary table in Mysql stored procedure

cursor A cursor is a method used to view or proce...

VSCode+CMake+Clang+GCC environment construction tutorial under win10

I plan to use C/C++ to implement basic data struc...

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...