前端开发攻略---封装日历calendar组件(纯手搓),可以根据您的需求任意改变,可玩性强

1、演示

2、代码

<template><div class="box" v-if="startMonth.year"><div class="left"><div class="top"><span class="iconfont" @click="changeMonth(-1)">左</span><span>{{ startMonth.year }}年{{ startMonth.month }}月</span><span></span></div><div class="calendarMain"><div class="weeks"><span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span></div><div class="days"><divclass="day"v-for="item in startMonth.dates":class="monthDaysClass(item)":style="[{ '--ml': item.week }]"@mouseenter="dayMouseMove(item)"@click="dayMouseClick(item)">{{ item.day }}</div></div></div></div><div class="right"><div class="top"><span></span><span>{{ endMonth.year }}年{{ endMonth.month }}月</span><span class="iconfont" @click="changeMonth(1)">右</span></div><div class="calendarMain"><div class="weeks"><span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span></div><div class="days"><divclass="day"v-for="item in endMonth.dates":class="monthDaysClass(item)":style="[{ '--ml': item.week }]"@mouseenter="dayMouseMove(item)"@click="dayMouseClick(item)">{{ item.day }}</div></div></div></div></div>
</template><script setup>
import { ref, reactive, onMounted } from 'vue'
const currentDateIndex = ref(0)
const startMonth = ref({})
const endMonth = ref({})
const selectDate = ref([])
const isMove = ref(false)onMounted(() => {initCalendar()
})const initCalendar = () => {getCalendarData()const startIndex = startMonth.value.dates.findIndex(item => !item.isTodayBefore)if (startIndex == startMonth.value.dates.length - 1) {selectDate.value[0] = startMonth.value.dates[startIndex].yymmddselectDate.value[1] = endMonth.value.dates[0].yymmdd} else {selectDate.value[0] = startMonth.value.dates[startIndex].yymmddselectDate.value[1] = startMonth.value.dates[startIndex + 1].yymmdd}
}const getCalendarData = () => {startMonth.value = getMonthDates(currentDateIndex.value)endMonth.value = getMonthDates(currentDateIndex.value + 1)
}
const changeMonth = num => {currentDateIndex.value += numgetCalendarData()
}const monthDaysClass = item => {if (item.isTodayBefore) return 'disabled'if (item.yymmdd == selectDate.value[0]) return 'active'if (item.yymmdd == selectDate.value[1]) return 'active'if (getDatesBetween(selectDate.value[0], selectDate.value[1]).includes(item.yymmdd)) return 'middle'
}const getDatesBetween = (date1, date2) => {let dates = []let currentDate = new Date(date1)let endDate = new Date(date2)while (currentDate < endDate) {let dateString = currentDate.toISOString().substr(0, 10)dates.push(dateString)currentDate.setDate(currentDate.getDate() + 1)}if (dates.length > 0) {dates.shift()}return dates
}const dayMouseClick = item => {if (item.isTodayBefore) returnlet arr = [...selectDate.value]if (!isMove.value) {if (arr.length == 1) {arr[1] = item.yymmdd} else {arr = []arr[0] = item.yymmdd}isMove.value = true} else {isMove.value = false}if (arr[0] > arr[1]) {selectDate.value = arr.reverse()} else {selectDate.value = arr}console.log(selectDate.value)
}const dayMouseMove = item => {if (item.isTodayBefore) returnif (!isMove.value) returnselectDate.value[1] = item.yymmdd
}function getMonthDates(monthOffset) {const today = new Date()const targetDate = new Date(today.getFullYear(), today.getMonth() + monthOffset, 1)const year = targetDate.getFullYear()let month = targetDate.getMonth() + 1 // 月份是从0开始的,所以要加1month = month >= 10 ? month : '0' + monthconst firstDay = new Date(year, targetDate.getMonth(), 1)const lastDay = new Date(year, targetDate.getMonth() + 1, 0)const monthDates = []for (let d = firstDay; d <= lastDay; d.setDate(d.getDate() + 1)) {const day = d.getDate()const dayOfWeek = d.getDay() // 返回0到6,0代表星期日const isTodayBefore = d.getTime() < today.setHours(0, 0, 0, 0) // 判断是否是今天之前的日期monthDates.push({day,week: dayOfWeek,isTodayBefore,yymmdd: `${year}-${month}-${day >= 10 ? day : '0' + day}`,})}return { year, month, dates: monthDates }
}
</script><style scoped lang="scss">
.box {width: 793px;height: 436px;box-shadow: 2px 2px 6px #0003;display: flex;justify-content: space-between;padding: 30px 15px;.left,.right {width: 46%;height: 100%;.top {display: flex;justify-content: space-between;font-weight: bold;.iconfont {cursor: pointer;user-select: none;}}.calendarMain {.weeks {font-weight: bold;margin-top: 20px;display: flex;justify-content: space-between;& > span {display: inline-block;width: 50px;height: 50px;line-height: 50px;text-align: center;}}.days {display: flex;flex-wrap: wrap;cursor: pointer;.day {width: 50px;height: 50px;height: 50px;text-align: center;line-height: 50px;color: #111;font-size: 14px;}.day:nth-child(1) {margin-left: calc(var(--ml) * 50px);}.disabled {color: #ccc;cursor: not-allowed;}.active {background-color: #266fff;color: #fff;}.middle {background-color: rgba(38, 111, 255, 0.3);color: #fff;}}}}
}
</style>

3、代码解释

import { ref, reactive, onMounted } from 'vue'
import { useDatesBetween } from '@/hooks/time.js'

这里引入了 Vue 中的 ref 和 reactive 函数,以及 onMounted 钩子函数,以及从 time.js 模块中引入了 useDatesBetween 函数。

const currentDateIndex = ref(0)
const startMonth = ref({})
const endMonth = ref({})
const selectDate = ref([])
const getDatesBetween = useDatesBetween()
const isMove = ref(false)

创建了一些响应式变量,包括当前日期索引 currentDateIndex,起始月份 startMonth 和结束月份 endMonth 的引用,以及选择的日期范围 selectDateuseDatesBetween 函数的引用 getDatesBetween,以及一个标志 isMove,用于标记是否在移动状态。

onMounted(() => {initCalendar()
})

使用 onMounted 钩子,在组件挂载后执行 initCalendar 函数。

const initCalendar = () => {getCalendarData()const startIndex = startMonth.value.dates.findIndex(item => !item.isTodayBefore)if (startIndex == startMonth.value.dates.length - 1) {selectDate.value[0] = startMonth.value.dates[startIndex].yymmddselectDate.value[1] = endMonth.value.dates[0].yymmdd} else {selectDate.value[0] = startMonth.value.dates[startIndex].yymmddselectDate.value[1] = startMonth.value.dates[startIndex + 1].yymmdd}
}

initCalendar 函数初始化日历,获取日历数据并设置选择的日期范围。

const getCalendarData = () => {startMonth.value = getMonthDates(currentDateIndex.value)endMonth.value = getMonthDates(currentDateIndex.value + 1)
}

getCalendarData 函数获取当前月份和下个月份的日历数据。

const changeMonth = num => {currentDateIndex.value += numgetCalendarData()
}

changeMonth 函数根据给定的数字改变当前月份索引,并重新获取日历数据。

const monthDaysClass = item => {if (item.isTodayBefore) return 'disabled'if (item.yymmdd == selectDate.value[0]) return 'active'if (item.yymmdd == selectDate.value[1]) return 'active'if (getDatesBetween(selectDate.value[0], selectDate.value[1]).includes(item.yymmdd)) return 'middle'
}

monthDaysClass 函数根据日期项目返回对应的 CSS 类名,用于渲染日历中的日期。

const dayMouseClick = item => {if (item.isTodayBefore) returnlet arr = [...selectDate.value]if (!isMove.value) {if (arr.length == 1) {arr[1] = item.yymmdd} else {arr = []arr[0] = item.yymmdd}isMove.value = true} else {isMove.value = false}if (arr[0] > arr[1]) {selectDate.value = arr.reverse()} else {selectDate.value = arr}console.log(selectDate.value)
}

dayMouseClick 函数处理日期的点击事件,根据点击选择日期范围。

const dayMouseMove = item => {if (item.isTodayBefore) returnif (!isMove.value) returnselectDate.value[1] = item.yymmdd
}

dayMouseMove 函数处理鼠标移动事件,根据鼠标移动选择日期范围。

function getMonthDates(monthOffset) {const today = new Date()const targetDate = new Date(today.getFullYear(), today.getMonth() + monthOffset, 1)const year = targetDate.getFullYear()let month = targetDate.getMonth() + 1month = month >= 10 ? month : '0' + monthconst firstDay = new Date(year, targetDate.getMonth(), 1)const lastDay = new Date(year, targetDate.getMonth() + 1, 0)const monthDates = []for (let d = firstDay; d <= lastDay; d.setDate(d.getDate() + 1)) {const day = d.getDate()const dayOfWeek = d.getDay()const isTodayBefore = d.getTime() < today.setHours(0, 0, 0, 0)monthDates.push({day,week: dayOfWeek,isTodayBefore,yymmdd: `${year}-${month}-${day >= 10 ? day : '0' + day}`,})}return { year, month, dates: monthDates }
}

getMonthDates 函数根据月份偏移量获取该月的日期数据,包括年份、月份以及日期数组。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/827527.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个基于更新频率和卡片等级、浏览量的动态推荐排序算法

需求背景 真实场景会更复杂一些&#xff0c;下面抽象出一个简单的示例来举栗子&#xff1a; 比如现在有一个卡片列表&#xff0c;卡片自身有卡片的创建时间、卡片等级、浏览量几个关键字段。 如果单纯的根据卡片等级去排序&#xff0c;那么很容易导致一些高等级的旧卡片一直霸…

jsoncpp解析文件

背景&#xff1a;先用wireshark抓数据帧&#xff0c;过滤自己需要的之后转换为json文件&#xff0c;然后使用jsoncpp工具解析&#xff0c;获取其中有用的数据&#xff0c;最后把数据写入到文件中&#xff0c;之后分析数据或根据数据画图。 我分析的json文件格式如下&#xff0c…

Python学习从0开始——项目一day02数据库连接

Python学习从0开始——项目一day02数据库连接 一、在线云数据库二、测试数据库连接三、数据库驱动介绍四、SQL执行4.1插入测试数据4.2安装数据库连接模块4.3测试SQL语句执行4.4执行SQL的固定步骤及示例 一、在线云数据库 找了一个在线数据库&#xff0c;需要邮箱注册&#xff…

甄美天使1+3退休模式开发|最新升级模式

我是新零售商业架构师肖琳&#xff0c;易创客社交新零售创始‌‌‌‌‌人&#xff0c;擅长品牌从0-1、1-10起盘全过程。易创客新零售&#xff0c;提供商业模式解决方案系统&#xff0c;包括分销系统、微商系统、新零售系统&#xff1b;提供社交新零售陪跑代运营&#xff0c;提供…

Uds的0x10服务

0x10为诊断会话控制&#xff0c;该服务下有三种子功能&#xff0c;分别为默认会话&#xff0c;扩展会话&#xff0c;编程会话。 如果长时间在非默认会话下&#xff0c;没有用诊断仪跟ECU通讯&#xff0c;那么ECU会调回或者是返回到默认会话下。一般UDS协议上推荐是5000毫秒&…

Ubuntu搭建RP2040开发环境-1

Ubuntu搭建RP2040开发环境-1 2024-4-23 hongxi.zhu 前置依赖&#xff08;可选&#xff09; sudo apt-get install vim sudo apt-get install cmake sudo apt-get install libncurses-dev sudo apt-get install libssl-dev sudo apt-get install git一、下载SDK和编译链 下载SD…

【OpenGL概念】QOpenGLWidget类详述--此文档基于Qt5.14.2

一、说明 QOpenGLWidget提供了在Qt应用程序中显示集成OpenGL图形的功能。使用它非常简单&#xff1a;让您的类继承它&#xff0c;并像使用任何其他QWidget一样使用子类&#xff0c;除了您可以选择使用QPainter和标准OpenGL渲染命令之间的区别。 二、QOpenGLWidget概述 QOpenGL…

云原生的基石:containerd引领未来容器发展趋势

文章目录 一、Containerd简介&#xff1a;容器技术的心脏二、Containerd核心原理解析三、Containerd与Docker的关系四、Containerd在云原生应用部署中的作用五、Containerd的扩展性和插件机制六、Containerd的安全特性七、Containerd的性能优化八、Containerd的社区和生态系统九…

c++ primer plus(1)

前言 本次博客将要通过文字以及代码来介绍 c primer plus 没有C语言基础的放心&#xff0c;我还是会把自己当成傻子&#xff0c;一步一步的来 主要是理解这本书的第二章重点内容&#xff0c;仅仅是笔者自己的鄙见&#xff0c;有问题请指出 OK&#xff0c;开始 第二章 首…

vue+vite+elements

1.安装 1> 安装vite&#xff08;直接安装&#xff09; 2> 安装好elements 3> 安装 windicss windicss简单用法 <template> <button class"btn">按钮</button> </template> <style scoped> .btn { apply bg-purple-500 …

新能源行业数据防泄密解决方案

行业背景 随着全球能源危机和环境污染问题日益突出,节能、环保有关行业的发展被高度重视,发展新能源电池已经在全球范围内形成共识。新能源电池相关技术是人们当下最为关注、研究投入最大的科研项目。人们不断尝试新的技术和材料,创造出新型电池。未来生活中将会有越来越高…

顺序表(详解)

1.什么是数据结构 所谓数据结构也就是数据在内存中的储存结构&#xff0c;它有 线性表&#xff0c;队列&#xff0c;栈结构&#xff0c;树结构&#xff0c;图结构等等&#xff0c;顺序表是线性表的一种。 2.物理结构与逻辑结构 物理结构是指一个数据在内存实际的储存空间&am…

C++面向对象程序设计 - 运算符重载

函数重载就是对一个已有的函数赋予新的含义&#xff0c;使之实现新的功能。因此一个函数名就可以用来代表不同功能的函数&#xff0c;也就是一名多用。运算符也可以重载&#xff0c;即运算符重载&#xff08;operator overloading&#xff09;。 一、运算符重载的方法 运算符重…

代码随想录学习Day 29

1005.K次取反后最大化的数组和 题目链接 讲解链接 思路&#xff1a;先对数组进行排序&#xff0c;保证数组中最小的值&#xff08;也就是取反后损失最小的值&#xff09;位于数组最前端。然后对数组进行遍历&#xff0c;在k次内尽可能将负数全部取反。当数组中元素全部>0…

indexDB 大图缓存

背景 最近在项目中遇到了一个问题&#xff1a;由于大屏背景图加载速度过慢&#xff0c;导致页面黑屏时间过长&#xff0c;影响了用户的体验。从下图可以看出加载耗时将近一分钟 IndexDB 主要的想法就是利用indexDB去做缓存&#xff0c;优化加载速度&#xff1b;在这之前&am…

在选择试验台底座时,应注意哪些问题——河北北重

在选择试验台底座时&#xff0c;应注意以下几个方面&#xff1a; 底座尺寸和承载能力&#xff1a;底座的尺寸和承载能力应与试验台的尺寸和所需承载的设备重量相匹配&#xff0c;确保底座能够稳定承载试验台和设备。 材料和质量&#xff1a;底座的材料应具有足够的强度和耐久性…

软件测试之【合理的利用GPT来辅助软件测试一】

读者大大们好呀&#xff01;&#xff01;!☀️☀️☀️ &#x1f525; 欢迎来到我的博客 &#x1f440;期待大大的关注哦❗️❗️❗️ &#x1f680;欢迎收看我的主页文章➡️寻至善的主页 文章目录 前言GPT的原理及技巧GPT辅助接口自动化测试 前言 在编程基础栏目中&#xff…

Kafak详解(1)

简介 消息队列 为什么要有消息队列 图-1 消息队列的使用 消息队列 1)消息Message&#xff1a;网络中的两台计算机或者两个通讯设备之间传递的数据。例如说&#xff1a;文本、音乐、视频等内容。 2)队列Queue&#xff1a;一种特殊的线性表(数据元素首尾相接)&#xff0c;特…

2024平替电容笔买哪个品牌好?iPad电容笔全能榜单热门款TOP5分享!

2024年&#xff0c;随着科技的不断发展和消费者对生活品质的追求&#xff0c;电容笔作为一种创新的无纸化工具&#xff0c;逐渐走进人们的生活和工作中。然而&#xff0c;在电容笔市场的繁荣背后&#xff0c;也隐藏着品质良莠不齐的现象。众多品牌为了追求利润&#xff0c;推出…

python常用高阶函数

map函数 map(function, iterable1[, iterable2, …]) map() 是 Python 内置的高阶函数&#xff0c;它用于将一个或多个可迭代对象&#xff08;如列表、元组、字符串等&#xff09;中的每个元素按照指定的函数进行处理&#xff0c;并返回一个迭代器&#xff08;iterator&#xf…