js+html+css实现简单日历效果

yizhihongxing

下面是 "js+html+css实现简单日历效果"的攻略:

1. 导入CSS和JS文件

在head标签中导入显示日历所需的CSS和JS文件

<head>
  <link rel="stylesheet" type="text/css" href="calendarStyle.css">
  <script type="text/javascript" src="calendar.js"></script>
</head>

2. HTML的元素和结构

在页面中添加一个包含日历的div元素:

<div class="calendar-container">
  <div class="calendar-header">
    <button class="btn prev">上一月</button>
    <h2 class="calendar-title"></h2>
    <button class="btn next">下一月</button>
  </div>
  <table class="calendar-table">
    <thead>
      <tr class="calendar-weekdays"></tr>
    </thead>
    <tbody class="calendar-days"></tbody>
  </table>
</div>

容器元素有一个类“calendar-container”,有两个子元素 - 头部和表格。头部包括一个标题、前进按钮和后退按钮。表格通过thead元素显示哪些日期在哪些列。在tbody元素中,将它们的标头(列)显示“MON”、“TUE”、“WED”等式。

3. JS的逻辑实现

JavaScript文件将创建日期,以填充日历表。定义一个名为“Calendar”类的构造函数,并将其用于创建新的日历实例。

创建Calendar的步骤:

  1. 获取当前月份和年份;
  2. 创建一个填充表格的函数;
  3. 创建一个新的日期对象,设置年份和月份,然后设置为当前月份的第一天;
  4. 根据当月第一日是星期几,确定日历的初始列数;
  5. 表格每个单元格的循环是为期一个月的日期。
  6. 将每个表格单元格填充到html日历表中;
  7. 自增一个月,直到日历结束。
class Calendar {
  constructor(selector, events) {
    this.selector = selector;
    this.events = events;
    this.date = new Date();
    this.currentYear = this.date.getFullYear();
    this.currentMonth = this.date.getMonth();
    this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
    this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
    this.container = document.querySelector(selector);
    this.title = this.container.querySelector(".calendar-title");
    this.weekdays = this.container.querySelector(".calendar-weekdays");
    this.days = this.container.querySelector(".calendar-days");
    this.prevBtn = this.container.querySelector(".prev");
    this.nextBtn = this.container.querySelector(".next");
    this.createCalendar();
  }

  createCalendar() {
    this.title.textContent = `${this.currentYear}年${this.currentMonth + 1}月`;
    this.weekdays.innerHTML = "";
    this.days.innerHTML = "";

    let weekdayName = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for (let i = 0; i < weekdayName.length; i++) {
      let weekday = weekdayName[i];
      let th = document.createElement("th");
      th.textContent = weekday;
      this.weekdays.appendChild(th);
    }

    let day = 1;
    let row = document.createElement("tr");
    for (let i = 0; i < this.currentDay; i++) {
      let td = document.createElement("td");
      row.appendChild(td);
    }

    while (day <= this.daysInMonth) {
      let td = document.createElement("td");
      td.textContent = day;
      row.appendChild(td);

      if ((day + this.currentDay) % 7 === 0) {
        this.days.appendChild(row);
        row = document.createElement("tr");
      }

      day++;
    }

    if (row.children.length > 0) {
      this.days.appendChild(row);
    }

    this.prevBtn.addEventListener("click", () => {
      this.currentMonth--;
      if (this.currentMonth === -1) {
        this.currentMonth = 11;
        this.currentYear--;
      }
      this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
      this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
      this.createCalendar();
    });

    this.nextBtn.addEventListener("click", () => {
      this.currentMonth++;
      if (this.currentMonth === 12) {
        this.currentMonth = 0;
        this.currentYear++;
      }
      this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
      this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
      this.createCalendar();
    });
  }
}

let calendar = new Calendar(".calendar-container");

4. CSS的样式设置

最后,为日历设置CSS样式来使其更美观和易读。以本文为例给出样式修改:

.calendar-container {
  width: 500px;/*日历宽度*/
  margin: auto;
  box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  background-color: #fff;
  padding: 20px;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.calendar-title {
  margin: 0;
  font-size: 20px;
}

.calendar-table {
  width: 100%;
  border-collapse: collapse;
}

.calendar-weekdays {
  text-align: center;
  font-weight: bold;
  background-color: #f6f6f6;
}

.calendar-weekdays th {
  padding: 5px;
}

.calendar-days td {
  padding: 5px;
  text-align: center;
}

.calendar-days td.is-today {
  color: white;
  background-color: #2e90d9;
}

.calendar-days td.is-event {
  color: white;
  background-color: #42c878;
}

.prev, .next {
  padding: 12px;
  border: none;
  border-radius: 2px;
  background-color: #f6f6f6;
  color: #333;
  cursor: pointer;
}

.prev:hover, .next:hover {
  background-color: #e9e9e9;
}

示例

示例1: 在HTML中修改日历样式:

<div class="calendar-container" style="width: 800px;">
  <div class="calendar-header">
    <button class="btn prev">上一月</button>
    <h2 class="calendar-title"></h2>
    <button class="btn next">下一月</button>
  </div>
  <table class="calendar-table">
    <thead>
      <tr class="calendar-weekdays"></tr>
    </thead>
    <tbody class="calendar-days"></tbody>
  </table>
</div>

此示例通过设置calendar-container的width,修改日历的宽度。

示例2: 在JS中修改日历的顺序

假设我们要在日历头部添加一个显示今日日期的元素,并且将日历的前进/后退按钮调整为标题下方。

修改JS代码如下:

class Calendar {
  constructor(selector, events) {
    this.selector = selector;
    this.events = events;
    this.date = new Date();
    this.currentYear = this.date.getFullYear();
    this.currentMonth = this.date.getMonth();
    this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
    this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
    this.container = document.querySelector(selector);
    this.header = document.createElement("div");// 添加的元素
    this.title = this.container.querySelector(".calendar-title");
    this.subTitle = document.createElement("p");// 添加的元素
    this.weekdays = this.container.querySelector(".calendar-weekdays");
    this.days = this.container.querySelector(".calendar-days");
    this.prevBtn = this.container.querySelector(".prev");
    this.nextBtn = this.container.querySelector(".next");
    this.createCalendar();
  }

  createCalendar() {
    this.header.classList.add("calendar-header");// 添加的元素
    this.subTitle.classList.add("calendar-subtitle");// 添加的元素
    this.header.appendChild(this.title);// 修改日历头部
    this.header.appendChild(this.subTitle);// 添加的元素
    this.container.insertBefore(this.header, this.weekdays);// 修改日历头部顺序
    this.title.textContent = `${this.currentYear}年${this.currentMonth + 1}月`;
    this.subTitle.textContent = `${this.date.getFullYear()}年${this.date.getMonth() + 1}月${this.date.getDate()}日`;
    this.weekdays.innerHTML = "";
    this.days.innerHTML = "";

    let weekdayName = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    for (let i = 0; i < weekdayName.length; i++) {
      let weekday = weekdayName[i];
      let th = document.createElement("th");
      th.textContent = weekday;
      this.weekdays.appendChild(th);
    }

    let day = 1;
    let row = document.createElement("tr");
    for (let i = 0; i < this.currentDay; i++) {
      let td = document.createElement("td");
      row.appendChild(td);
    }

    while (day <= this.daysInMonth) {
      let td = document.createElement("td");
      td.textContent = day;
      row.appendChild(td);

      if ((day + this.currentDay) % 7 === 0) {
        this.days.appendChild(row);
        row = document.createElement("tr");
      }

      day++;
    }

    if (row.children.length > 0) {
      this.days.appendChild(row);
    }

    this.prevBtn.addEventListener("click", () => {
      this.currentMonth--;
      if (this.currentMonth === -1) {
        this.currentMonth = 11;
        this.currentYear--;
      }
      this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
      this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
      this.createCalendar();
    });

    this.nextBtn.addEventListener("click", () => {
      this.currentMonth++;
      if (this.currentMonth === 12) {
        this.currentMonth = 0;
        this.currentYear++;
      }
      this.daysInMonth = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
      this.currentDay = new Date(this.currentYear, this.currentMonth, 1).getDay();
      this.createCalendar();
    });
  }
}

let calendar = new Calendar(".calendar-container");

在此示例中,我们创建了一个新的元素‘div’,并在标记中为其添加了类“calendar-header”(因为这是我们想要在页面顶部放置的)和“calendar-subtitle”,以便在日历头部中创建一个新元素。我们通过使用this.header.appendChild(this.title)和this.header.appendChild(this.subTitle)修改了日历头部。最后,通过使用this.container.insertBefore(this.header, this.weekdays)方法,将新元素添加到旧元素的前面。

通过以上两个示例,你需要了解以下几个方面来实现简单的日历:

  • 在HTML中添加元素和结构;
  • 在CSS中设置布局和样式;
  • 在JS中设置逻辑和动态效果。

总结:

这里给出了如何使用js+html+css实现简单日历的完整攻略,包括所需的HTML结构,CSS样式和Javascript逻辑实现。同时,我们还给出了两个示例,演示如何在HTML和JS中对日历样式和顺序进行更改。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:js+html+css实现简单日历效果 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • Javascript Global encodeURIComponent() 函数

    以下是关于JavaScript Global对象中encodeURIComponent()函数的完整攻略,包括两个示例说明。 JavaScript Global对象中的encodeURIComponent()函数 JavaScript Global对象中的encodeURIComponent()用于将一个编码URI组件字符串。URI(Uniform Reso…

    JavaScript 2023年5月11日
    00
  • JS替换字符串中指定位置的字符(多种方法)

    JS替换字符串中指定位置的字符(多种方法) 在JavaScript中,我们经常需要替换字符串中的字符,特别是需要替换某个位置的字符时。下面是几种替换字符串中指定位置的字符的方法。 1. 使用字符串的substr()和replace()方法 let str = "hello world"; let index = 6; // 替换第 7 个…

    JavaScript 2023年5月28日
    00
  • json对象与数组以及转换成js对象的简单实现方法

    下面是关于“json对象与数组以及转换成js对象的简单实现方法”的完整攻略: 1. 什么是JSON JSON,全称是JavaScript Object Notation,是一种轻量级的数据交换格式。它基于JavaScript语法的子集,包括对象、数组、字符串、数字、布尔值和null。 JSON被广泛应用于Web应用程序和API中,是一种常用的数据交换格式。现…

    JavaScript 2023年5月27日
    00
  • JS正则截取两个字符串之间及字符串前后内容的方法

    让我来为您详细讲解“JS正则截取两个字符串之间及字符串前后内容的方法”的完整攻略。 正则截取两个字符串之间 如果您需要截取两个字符串中间的内容,可以使用正则表达式来完成。使用正则表达式的match方法,可以传入一个正则表达式,返回匹配到的字符串数组。 示例: const str = ‘hello world, my name is Jack’; const …

    JavaScript 2023年5月28日
    00
  • JavaScript基础之函数详解

    JavaScript基础之函数详解 本篇攻略将详细讲解JavaScript中函数的相关知识,包括函数的定义、参数、返回值、作用域等内容。如果你刚刚开始学习JavaScript,或者想要加强对函数的理解,本篇攻略将是一个不错的选择。本篇攻略中的所有示例代码均可在浏览器中运行,方便调试和测试。 函数的定义 在JavaScript中定义一个函数通常有两种方式,分别…

    JavaScript 2023年5月17日
    00
  • 五种js判断是否为整数类型方式

    下面是”五种js判断是否为整数类型方式”的攻略。 一、用typeof判断 代码示例 function isInteger(num) { return typeof num === ‘number’ && num % 1 === 0; } 描述 通过typeof操作符可以判断变量的类型,如果是number类型,那么就可以继续判断是否为整数。利用…

    JavaScript 2023年6月10日
    00
  • 引入autocomplete组件时JS报未结束字符串常量错误

    引入autocomplete组件时JS报未结束字符串常量错误通常是因为代码中的字符串没有被正确引号包裹或者是引号嵌套错误,导致在解析代码时遇到了问题。以下是解决该问题的几个攻略: 1. 检查引号的嵌套问题 当代码中包含有引号(单引号或双引号)时,如果不注意嵌套问题,就会出现语法错误。例如: var options = "<option val…

    JavaScript 2023年5月18日
    00
  • JavaScript高级教程5.6之基本包装类型(详细)

    JavaScript高级教程5.6之基本包装类型(详细) 基本包装类型介绍 JavaScript中有三种基本类型:Number、String和Boolean。它们是原始值,不是对象。但是,在读取它们的属性时,会创建临时的基本包装类型对象,以便能够访问属性和方法。一旦访问结束,立即销毁这个临时对象。这个临时对象的行为类似于对象类型的实例。 基本包装类型方法 在…

    JavaScript 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部