JavaScript quickly implements calendar effects

JavaScript quickly implements calendar effects

This article example shares the specific code of JavaScript to quickly achieve the calendar effect for your reference. The specific content is as follows

Rendering

Code

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;

    }

    #calendar {
      background-color: #9900ff;
      color: #fff;
      border-radius: 5px;
      margin: 100px auto;
    }

    #title {
      font-size: 1.4em;
      padding: 4px 0.55em;
    }

    #days th {
      font-weight: bold;
      text-align: center;
      padding: 4px 0.55em;
    }

    #calendar td {
      text-align: center;
      padding: 4px 20px;
    }

    #today {
      color: #f00;
      font-weight: bold;
    }

    .poin {
      cursor: pointer;
      cursor: hand;
    }
  </style>
  <script>
    window.onload = function(){
      var form = document.getElementById('calendar');
      //Call the calendar object's own init method calendar.init(form);
    }
    var calendar = {
      year: null,
      month: null,
      //Day array dayTable: null,
      // Initialization function init(form) {
        // 1 Get the day array this.dayTable=form.getElementsByTagName('td');
        //2 Create a calendar and pass in the current time this.createCalendar(form,new Date());
        var nextMon=form.getElementsByTagName('th')[2];
        var preMon=form.getElementsByTagName('th')[0];
        preMon.onclick=function(){
        calendar.clearCalendar(form)
          var preDate = new Date (calendar.year,calendar.month-1,1);
          calendar.createCalendar(form,preDate)
        }
        nextMon.onclick=function(){
          calendar.clearCalendar(form)
          var nextDate=new Date(calendar.year,calendar.month+1,1);
          calendar.createCalendar(form,nextDate)
        }
      },
      // Clear calendar data clearCalendar(form) {
        var tds = form.getElementsByTagName('td');
        for (var i = 0; i < tds.length; i++) {
          tds[i].innerHTML='&nbsp';
          // Clear today's style tds[i].id='';
        }
      },
      // 3 Generate calendar // from table date the date to be created createCalendar(form,date){
        //Get the current year this.year=date.getFullYear(); 
         //Get the current month this.month=date.getMonth();

         //Write the year and month into the calendar form.getElementsByTagName('th')[1].innerHTML = this.year+"年"+(this.month+1)+"月";
         //Get the number of days in this month var dataNum=this.getDateLen(this.year,this.month);
         var firstDay = this.getFristDay(calendar.year,calendar.month);
        // Loop and write the number of each day into the calendar // Let i represent the date.
        for (var i = 1; i <= dataNum; i++) {
          this.dayTable[fristDay+i-1].innerHTML=i;
          var nowDate =new Date();
          if(i == nowDate.getDate() && calendar.month == nowDate.getMonth() && calendar.year == nowDate.getFullYear()){
            // Set the id of the current element to today
            calendar.dayTable[i+fristDay-1].id = "today";
          }
        }
      },
       // Get the number of days in this month getDateLen(year,month){
        //Get the first day of the next month var nextMonth=new Date(year,month+1,1);
        // Set the hour of the first day of next month - 1, which is the hour of the last day of last month. Subtract a value that does not exceed 24 hours nextMonth.setHours(nextMonth.getHours()-1);
        //Get the date of the next month, which is the last day of the previous month.
        return nextMonth.getDate();
       },
       // Get the first day of the month.
      getFristDay:function(year,month){
        var firstDay=new Date(year,month,1);
        return firstDay.getDay();
      }
    } 
  </script>
</head>

<body>
  <table id="calendar">
    <tr>
      <!-- Left Arrow -->
      <th class="poin">&lt;&lt;</th>
      <!-- Year Month -->
      <th id="title" colspan="5"></th>
      <!-- Right Arrow -->
      <th class="poin">&gt;&gt;</th>
    </tr>
    <tr id="days">
      <th>Day</th>
      <th>一</th>

      <th>Two</th>
      <th>three</th>
      <th>Four</th>
      <th>Five</th>
      <th>six</th>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>

</body>

</html>

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
  • JS Calendar Recommendation
  • Vue.js creates Calendar effect
  • 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

<<:  Comparison of several examples of insertion efficiency in Mysql

>>:  Complete steps to quickly configure HugePages under Linux system

Recommend

Native js to achieve simple carousel effect

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

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

Summary and practice of javascript prototype chain diagram

Table of contents Prototype chain We can implemen...

What to do if the online MySQL auto-increment ID is exhausted

Table of contents Table definition auto-increment...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Common CSS Errors and Solutions

Copy code The code is as follows: Difference betw...

Docker cleanup environment operation

Start cleaning carefully! List unused volumes doc...

Summary of MySQL character sets

Table of contents Character Set Comparison Rules ...

Example code for implementing auto-increment sequence in mysql

1. Create a sequence table CREATE TABLE `sequence...

The difference between HTML iframe and frameset_PowerNode Java Academy

Introduction 1.<iframe> tag: iframe is an i...

CSS3 to achieve timeline effects

Recently, when I turned on my computer, I saw tha...