angular日历

news/2025/2/25 14:50:12

说明:
写一个简单的日历功能
效果图:
在这里插入图片描述

step1:C:\Users\Administrator\WebstormProjects\untitled4\src\app\calendar\calendar.component.ts

javascript">import { Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';

interface CalendarDay {
  date: Date;
  isToday: boolean;
  isHoliday: boolean;
  events: string[];
  isWeekend: boolean;
}

@Component({
  selector: 'app-calendar',
  standalone: true,
  imports: [CommonModule, MatButtonModule, MatIconModule, MatDialogModule],
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css']
})
export class CalendarComponent {
  currentDate = signal(new Date());
  days = signal<CalendarDay[]>([]);
  darkMode = false;

  constructor(private dialog: MatDialog) {
    this.generateCalendar();
  }

  private generateCalendar() {
    const daysArray: CalendarDay[] = [];
    const year = this.currentDate().getFullYear();
    const month = this.currentDate().getMonth();
    const daysInMonth = new Date(year, month + 1, 0).getDate();

    for (let day = 1; day <= daysInMonth; day++) {
      const date = new Date(year, month, day);
      const dayOfWeek = date.getDay();

      daysArray.push({
        date,
        isToday: this.isSameDay(date, new Date()),
        isHoliday: this.checkHoliday(date),
        isWeekend: dayOfWeek === 0 || dayOfWeek === 6,
        events: []
      });
    }
    this.days.set(daysArray);
  }

  changeMonth(offset: number) {
    this.currentDate.update(d => new Date(d.getFullYear(), d.getMonth() + offset, 1));
    this.generateCalendar();
  }

  addEvent(day: CalendarDay) {
    // 事件添加逻辑(示例)
    day.events.push('New Event');
    this.days.update(days => [...days]);
  }

  private isSameDay(d1: Date, d2: Date) {
    return d1.toDateString() === d2.toDateString();
  }

  private checkHoliday(date: Date) {
    const holidays = [
      '2024-01-01', '2024-05-01', '2024-10-01', // 示例节假日
      '2024-02-10', '2024-04-04', '2024-06-10'
    ];
    const dateString = date.toISOString().split('T')[0];
    return holidays.includes(dateString);
  }
}

step2: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calendar\calendar.component.html

<div class="calendar-container" [class.dark-mode]="darkMode">
  <div class="calendar-header">
    <button mat-icon-button (click)="changeMonth(-1)">
      <mat-icon>chevron_left</mat-icon>
    </button>

    <h2>{{ currentDate() | date: 'yyyy年MM月' }}</h2>

    <div class="control-buttons">
      <button mat-icon-button (click)="darkMode = !darkMode">
        <mat-icon>{{ darkMode ? 'dark_mode' : 'light_mode' }}</mat-icon>
      </button>
    </div>

    <button mat-icon-button (click)="changeMonth(1)">
      <mat-icon>chevron_right</mat-icon>
    </button>
  </div>

  <div class="calendar-grid">
    <div *ngFor="let day of days()"
         class="calendar-day"
         [class.today]="day.isToday"
         [class.holiday]="day.isHoliday"
         [class.weekend]="day.isWeekend"
         (click)="addEvent(day)">
      <div class="solar">{{ day.date | date: 'd' }}</div>
      <div class="weekday">{{ day.date | date: 'EEE' }}</div>
      <div class="events">
        <mat-icon *ngFor="let e of day.events">circle</mat-icon>
      </div>
    </div>
  </div>
</div>

step3: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calendar\calendar.component.css

.calendar-container {
  --bg-color: #ffffff;
  --text-color: #333;
  --header-bg: #f8f9fa;
  --border-color: #dee2e6;
  --today-bg: #e3f2fd;
  --holiday-bg: #ffebee;
  --weekend-bg: #f8f9fa;
  --event-color: #2196f3;
  --button-hover: rgba(0, 0, 0, 0.05);

  background: var(--bg-color);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  max-width: 800px;
  margin: 2rem auto;
}

/* 暗黑模式 */
.calendar-container.dark-mode {
  --bg-color: #2d2d2d;
  --text-color: #e0e0e0;
  --header-bg: #1a1a1a;
  --border-color: #404040;
  --today-bg: #004d80;
  --holiday-bg: #4d1f1f;
  --weekend-bg: #333;
  --event-color: #64b5f6;
  --button-hover: rgba(255, 255, 255, 0.1);
}

.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: var(--header-bg);
  border-radius: 12px;
  margin-bottom: 24px;

  h2 {
    margin: 0;
    color: var(--text-color);
    font-weight: 600;
    font-size: 1.5rem;
  }

  button {
    transition: all 0.2s ease;
    border-radius: 8px;

    &:hover {
      background: var(--button-hover);
    }

    mat-icon {
      color: var(--text-color);
    }
  }
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
}

.calendar-day {
  position: relative;
  min-height: 120px;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: var(--bg-color);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  &.today {
    background: var(--today-bg);
    border-color: transparent;
  }

  &.holiday {
    background: var(--holiday-bg);
    border-color: transparent;

    .solar {
      color: #ff4444;
    }
  }

  &.weekend {
    background: var(--weekend-bg);

    .weekday {
      color: #888;
    }
  }
}

.solar {
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--text-color);
}

.weekday {
  font-size: 0.9rem;
  color: var(--text-color);
  opacity: 0.8;
  margin: 4px 0;
}

.events {
  position: absolute;
  bottom: 8px;
  right: 8px;

  mat-icon {
    font-size: 16px;
    color: var(--event-color);
    margin-left: 2px;
  }
}

.control-buttons {
  display: flex;
  gap: 8px;
}

@media (max-width: 768px) {
  .calendar-container {
    padding: 16px;
  }

  .calendar-day {
    min-height: 90px;
    padding: 8px;

    .solar {
      font-size: 1.2rem;
    }
  }
}

end


http://www.niftyadmin.cn/n/5865624.html

相关文章

中国的Cursor! 字节跳动推出Trae,开放Windows版(附资源),开发自己的网站,内置 GPT-4o 强大Al模型!

Trae是什么 Trae 是字节跳动推出的免费 AI IDE&#xff0c;通过 AI 技术提升开发效率。支持中文&#xff0c;集成了 Claude 3.5 和 GPT-4 等主流 AI 模型&#xff0c;完全免费使用。Trae 的主要功能包括 Builder 模式和 Chat 模式&#xff0c;其中 Builder 模式可帮助开发者从…

PostgreSQL数据库之pg_dump使用

目录 前言 1. 基础用法 1.1 备份整个数据库到 SQL 文件 2. 备份选项 2.1 仅备份结构&#xff08;不包含数据&#xff09; 2.2 仅备份数据&#xff08;不包含表结构&#xff09; 2.3 备份特定表 2.4 排除特定表 2.5 备份为自定义格式&#xff08;支持压缩和快速恢复&am…

将Ubuntu操作系统的安装源设置为阿里云

在使用Ubuntu操作系统时,默认的软件源通常是国外的仓库,这可能会导致软件安装和更新速度较慢。为了提高下载速度和稳定性,我们可以将Ubuntu的安装源设置为阿里云镜像源。以下是详细步骤: 一、准备工作 在开始之前,请确保您的Ubuntu系统可以正常上网,并且您拥有管理员权…

日常知识点之刷题一

1&#xff1a;流浪地球 0~n-1个发动机&#xff0c;计划启动m次&#xff0c;求最后启动的发动机的个数。 以及发动机的编号。&#xff08;模拟过程&#xff0c;每次手动启动的机器对应时间向两边扩散&#xff09; //输入每个启动的时间和编号 void test_liulang() {int n, m;ci…

数据库(MySQL)二

MySQL 六、MySQL索引视图6.1 索引底层原理6.1.1 索引hash算法6.1.2 索引二叉树算法6.1.3 索引平衡二叉树算法6.1.4 索引BTREE树算法6.1.5 普通SQL全表扫描过程 6.2 索引分类6.2.1 按数据结构层次分类6.2.2 按字段数量层次分类6.2.3 按功能逻辑层次分类&#xff08;面试题&#…

登录-10.Filter-登录校验过滤器

一.登录校验过滤器的实现思路 我们要实现登录校验过滤器&#xff0c;就要首先明白登录校验过滤器的实现思路。登录校验过滤器是用来实现登录校验的。那么首先思考第一个问题&#xff0c;所有的请求都需要校验吗&#xff1f; 答案是否定的&#xff0c;因为login请求就不需要过滤…

计算机毕业设计SpringBoot+Vue.js明星周边产品销售网站(源码+文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

C++判断回文字符串

C判断回文字符串 1、使用reverse()函数反转2、使用循环&#xff0c;借助变量首尾比较3、双指针方法1&#xff1a;while循环方法2&#xff1a;for循环 1、使用reverse()函数反转 思路&#xff1a; 首先读取一个字符串并存储在变量 s 中 将字符串 s 复制到另一个字符串变量 s1 …