uniapp 微信小程序 自定义日历组件

news2025/3/3 4:41:06

效果图

功能:可以记录当天是否有某些任务或者某些记录

具体使用:

子组件代码

<template>
	<view class="Accumulate">
		<view class="bx">
			<view class="bxx">
				<view class="plank">
				</view>
				<view class="calendar">
					<view class="calendarbx">
						<view class="calendarview flex jc ac">
							<u-icon name="arrow-left" color="#232323" size="18" @click="addmonth(1)"></u-icon>
							<view class="title">
								{{viewday}}
							</view>
							<u-icon name="arrow-right" color="#232323" size="18" @click="addmonth(2)"></u-icon>
						</view>
						<view class="week flex  ac">
							<view class="weekli" v-for="(item,index) in week">
								{{item.title}}
							</view>
						</view>
						<view class="data">
							<view class=" flex  ac flexwrap" v-if="show">
								<view class="datali flex jc ac" v-for="(item,index) in days" :key="item">
									<!-- 三层判断条件 -->
									<view class="dataliradius flex jc ac flexwrap"
										:class="newday==item&&isnewmonth&&chooseday==item?'dataliradiusc':newday==item&&isnewmonth&&chooseday!=item?'dataliradiuscc':newday!=item&&chooseday==item?'dataliradiusc':''"
										@click.stop="funchoosedayy(item)">
										{{item==newday&&isnewmonth?'今':item}}
										<view class="rounds">
											<view class="round" v-if="recordlist.includes(item)&&item!=chooseday">
											</view>
										</view>
									</view>
								</view>
							</view>
							<!-- 以下是为了防止切换闪烁而复制了一份用于展示 -->
							<view class=" flex  ac flexwrap" v-else>
								<view class="datali flex jc ac" v-for="(item,index) in days" :key="item">
									<!-- 三层判断条件 -->
									<view class="dataliradius flex jc ac flexwrap"
										:class="newday==item&&isnewmonth&&chooseday==item?'dataliradiusc':newday==item&&isnewmonth&&chooseday!=item?'dataliradiuscc':newday!=item&&chooseday==item?'dataliradiusc':''">
										{{item==newday&&isnewmonth?'今':item}}
										<view class="rounds" v-if="recordlist.includes(item)&&item!=chooseday">
											<view class="round">
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				day: Number(new Date()), //用于记录今天
				showday: null, //用来存储要加减的日期
				viewday: null, //页面上展示的日期
				newday: null, //当天几号
				isnewmonth: true, //是否是当月
				chooseday: null, //选中的那一天
				show: true,
				week: [{
						title: '一'
					},
					{
						title: '二'
					},
					{
						title: '三'
					},
					{
						title: '四'
					},
					{
						title: '五'
					},
					{
						title: '六'
					},
					{
						title: '日'
					}
				],
				days: [], //天数数组
				height: 0
			};
		},
props:{
			recordlist:{
				default: [], //是否有练习记录,需要每次切换月份的时候查询,那几天有练习记录,下标会记录
				stype:Array
			}
		},
		onLoad() {

		},
		onReady() {
			let time = this.yearFormat(this.day, 'obj')
			this.newday = time.day; //当天日期只用初始化一次就不用再初始化了
			this.showday = time.year + '-' + time.month + '-' + time.day; //用来存储要加减的日期
			this.viewday = this.yearFormat(this.showday) //页面上展示的日期
			this.chooseday = time.day;
			this.addmonth(0) //初始化时间
		},
		methods: {

			//选中日期
			funchoosedayy(item) {
				// console.log(item, '选中几号');
				if (item == '') {
					return
				}
				this.chooseday = item;
				let dataobj = this.showday.split('-');
				let month;
				let year;
				let day;
				let sendtime = dataobj[0] + '-' + dataobj[1] + '-' + this.chooseday;
				this.$emit('sendtime', '选中的时间:' + sendtime);
			},
			//加减月份,初始化月份
			addmonth(type) {
				this.show = false;
				setTimeout(() => {
					this.show = true;
				}, 100)
				let newmonth = this.yearFormat(this.day, 'obj')
				let dataobj = this.showday.split('-');
				let month;
				let year;
				let day;
				//加
				if (type == 2) {
					if (dataobj[1] == 12) {
						month = 1;
						year = (dataobj[0] * 1) + 1
					} else {
						year = (dataobj[0] * 1);
						month = (dataobj[1] * 1) + 1
					}
				}
				//减
				if (type == 1) {
					if (dataobj[1] == 1) {
						month = 12;
						year = (dataobj[0] * 1) - 1
					} else {
						year = (dataobj[0] * 1);
						month = (dataobj[1] * 1) - 1
					}
				}

				//初始化使用
				if (type == 0) {
					month = (dataobj[1] * 1);
					year = (dataobj[0] * 1);
				}

				let daynum = this.getDaysInYearMonth(year, month);
				this.days = daynum; //获取天数
				let week = this.getDayOfWeek(year, month, 1) //获取每个月1号星期几
				//0 相当于星期日
				let addday;
				if (week == 0) {
					addday = 6;
				} else {
					addday = (week - 1)
				}
				for (let i = 0; i < addday; i++) {
					this.days.unshift('')
					this.$forceUpdate()
				}
				//判断是否是当月,是当月的话,选中当天日期,不是则是当月1号
				if (newmonth.month == month) {
					day = dataobj[2];
					this.chooseday = newmonth.day;
					this.isnewmonth = true;
					this.showday = year + '-' + month + '-' + day;
					this.viewday = year + '年' + month + '月';
					// console.log(this.showday, '当月');
					this.$forceUpdate()
				} else {
					day = 1;
					this.chooseday = day;
					this.isnewmonth = false;
					this.showday = year + '-' + month + '-' + day;
					this.viewday = year + '年' + month + '月';
					this.$forceUpdate()
				}
				// console.log(this.days);
				this.$emit('state_sendtime', '初始化时间:' + this.showday);
				this.$forceUpdate()
			},
			//获取年月
			yearFormat(num, obj) {
				//过滤器 用于格式化时间
				let date = new Date(num); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
				let year = date.getFullYear();
				let month = ("0" + (date.getMonth() + 1)).slice(-2);
				let sdate = ("0" + date.getDate()).slice(-2);
				let hour = ("0" + date.getHours()).slice(-2);
				let minute = ("0" + date.getMinutes()).slice(-2);
				let second = ("0" + date.getSeconds()).slice(-2);
				let result;
				if (obj) {
					// 拼接
					result = {
						year: year,
						month: month,
						day: sdate
					}
				} else {
					result = year + '年' + month + '月'

				}
				// 返回
				return result;
			},
			// 获取该年该月的天数
			getDaysInYearMonth(year, month) {
				// 每一次进行更新前,先清空日期数组
				month = parseInt(month, 10);
				// javascript中Date()对象的getDate()是获取时间对象对应于一个月中的某一天(1~31),当设置为0的时候,getDate()返回的就是最后一天,刚好对应我们需要的值。
				var date = new Date(year, month, 0);
				let arr = [];
				for (var i = 1; i <= date.getDate(); i++) {
					arr.push(i)
				}
				return arr;
			},
			//获取周几
			// console.log(getDayOfWeek(2023, 10, 5)); // 输出:星期四
			// 0就是 周日
			getDayOfWeek(year, month, day) {
				console.log(year, (month * 1), day);
				const date = new Date(year + '-' + (month * 1) + '-' + day).getDay(); // 注意月份是从0开始计数
				const options = {
					weekday: 'long'
				};
				//new Intl.DateTimeFormat('zh-CN', options).format(date)
				return date;
			}
		}
	}
</script>

<style lang="less" scoped>
	.Accumulate {
		// position: relative;
	}

	.back {
		width: 100%;
		height: 360rpx;
		border-radius: 0 0 60rpx 60rpx;
		background: #3366ff;
		position: absolute;
	}

	.Evaluationlist {
		margin-top: 30rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		overflow: scroll;

		.Evaluationlistli {
			padding: 30rpx 20rpx;
			box-sizing: border-box;
			border-radius: 30rpx;
			background: #fff;
			margin-bottom: 30rpx;

			.left {
				// display: block;
				width: 70%;
				max-height: 100rpx;
			}

			.right {
				padding: 10rpx 30rpx;
				height: 60rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				background: #3366ff;
				color: #fff;

			}

			.rightb {
				padding: 10rpx 30rpx;
				height: 60rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				background: #fff;
				color: #3366ff;
				border: 1rpx solid #3366ff;
			}
		}
	}

	.bx {
		width: 100%;
		// position: absolute;
		z-index: 999;

		.plank {
			width: 100%;
			height: 30rpx;
		}
	}

	.viewdata {
		width: 100%;
		height: 130rpx;
		padding: 0 30rpx;
		box-sizing: border-box;

		.viewdatali {
			width: 46%;
			height: 100%;
			border-radius: 30rpx;

			.num {
				width: 100%;
				font-size: 38rpx;
				font-weight: bold;
				text-align: center;
			}

			.title {
				width: 100%;
				font-size: 24rpx;
				margin-top: 5rpx;
				text-align: center;
			}
		}

		.viewdataliA {
			background: #f5f5f5;
			color: skyblue;
		}

		.viewdataliB {
			background: #f5f5f5;
			color: pink;
		}



	}

	.calendar {
		width: 100%;
		// padding: 30rpx 30rpx 0rpx 30rpx;
		box-sizing: border-box;

		// border-radius: 30rpx;
		.calendarbx {
			width: 100%;
			padding: 30rpx 30rpx 0rpx 30rpx;
			box-sizing: border-box;
			border-radius: 30rpx;
			background: #fff;

			.calendarview {
				background: lightgoldenrodyellow;
				padding: 10rpx 20rpx;
				box-sizing: border-box;

				.title {
					color: #232323;
					font-size: 28rpx;
					margin-left: 100rpx;
					margin-right: 100rpx;
				}
			}

			.week {
				font-size: 26rpx;
				color: #999;
				margin-top: 30rpx;

				.weekli {
					width: 12.2%;
					height: 30rpx;
					text-align: center;
					margin-right: 2.4%;
					line-height: 30rpx;
				}

				.weekli:nth-child(7n+7) {
					margin-right: 0% !important;
				}
			}

			.data {
				font-size: 28rpx;
				color: #999;
				margin-top: 40rpx;
				font-weight: bold;
				min-height: 410rpx;

				.datali {
					width: 12.2%;
					height: 50rpx;
					// text-align: center;
					margin-right: 2.4%;
					margin-bottom: 40rpx;

					.dataliradius {
						width: 50rpx;
						height: 50rpx;
						border-radius: 999%;
						background: #fff;
						line-height: 50rpx;
						color: #232323 !important;

						.rounds {
							width: 100%;
							height: 10rpx;

							.round {
								width: 10rpx;
								height: 10rpx;
								border-radius: 999%;
								background: #3366ff;
								margin: auto;
							}
						}
					}

					.dataliradiuscc {
						color: #3366ff !important;


					}

					.dataliradiusc {
						width: 50rpx;
						height: 50rpx;
						border-radius: 999%;
						background: #3366ff;
						color: #fff !important;
						line-height: 50rpx;


					}
				}

				.datali:nth-child(7n+7) {
					margin-right: 0% !important;
				}
			}
		}
	}
</style>

父组件

			<customCalendar @sendtime="sendtime" @state_sendtime="state_sendtime" :recordlist="[27, 25, 26, 29, 28]" ></customCalendar>

有需求可以自行修改。

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

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

相关文章

Mysql--基础篇--函数(字符串函数,日期函数,数值函数,聚合函数,自定义函数及与存储过程的区别等)

MySQL提供了丰富的内置函数&#xff0c;涵盖了字符串处理、数值计算、日期和时间操作、聚合统计、控制流等多种功能。这些函数可以帮助你简化SQL查询&#xff0c;提升开发效率。 除了内置函数&#xff0c;MySQL还支持自定义函数&#xff08;User-Defined Functions&#xff09;…

关于Mac中的shell

1 MacOS中的shell 介绍&#xff1a; 在 macOS 系统中&#xff0c;Shell 是命令行与系统交互的工具&#xff0c;用于执行命令、运行脚本和管理系统。macOS 提供了多种 Shell&#xff0c;主要包括 bash 和 zsh。在 macOS Catalina&#xff08;10.15&#xff09;之前&#xff0c…

外卖院长帝恩以专业打法,开启外卖运营新格局

据中国饭店协会数据显示&#xff0c;2023年我国在线餐饮外卖市场规模已达到15254亿元&#xff0c;同比增长36.7%。预计到2027年&#xff0c;我国在线餐饮外卖市场规模有望达到19567亿元&#xff0c;行业渗透率有望达到30.4%。在这一蓬勃发展的行业背后&#xff0c;离不开无数从…

高清绘画素材3600多张动漫线稿线描上色练习参考插画原画

工作之余来欣赏一波线稿&#xff0c;不务正业版... 很多很多的线稿... 百度网盘 请输入提取码

Power BI如何连接Azure Databricks数据源?

故事背景: 近期有朋友询问&#xff0c;自己公司有一些项目使用了Azure Databricks用于数据存储。如何使用Power BI Desktop桌面开发软件连接Azure Databricks的数据源呢&#xff1f; 解决方案: 其实Power BI是提供了连接Azure Databricks数据源的选项的&#xff0c;只是配置…

了解RabbitMQ中的Exchange:深入解析与实践应用

在分布式系统设计中&#xff0c;消息队列&#xff08;Message Queue&#xff09;扮演着至关重要的角色&#xff0c;而RabbitMQ作为开源消息代理软件的佼佼者&#xff0c;以其高性能、高可用性和丰富的功能特性&#xff0c;成为了众多开发者的首选。在RabbitMQ的核心组件中&…

前端通过后端返回的数据流下载文件

后端返回文件流,下载的文件无法读取,损坏,原因是因为接口处理没有加 blob类型 downloadFile(row.fileId).then(res > { // res 即后端返回的数据流 const blob new Blob([res.data]) if (blob && blob.size 0) { this.$notify.error(内容为空&#xff0c;无法下载…

基于 GEE Sentinel-1 数据集提取水体

目录 1 水体提取原理 2 完整代码 3 运行结果 1 水体提取原理 水体提取是地理信息和遥感技术的关键应用之一&#xff0c;对于多个领域都具有重要的应用价值。它有助于更好地管理水资源&#xff0c;保护环境&#xff0c;减少灾害风险&#xff0c;促进可持续发展&#xff0c;以…

BloombergGPT: A Large Language Model for Finance——面向金融领域的大语言模型

这篇文章介绍了BloombergGPT&#xff0c;一个专门为金融领域设计的大语言模型&#xff08;LLM&#xff09;。以下是文章的主要内容总结&#xff1a; 背景与动机&#xff1a; 大语言模型&#xff08;如GPT-3&#xff09;在多个任务上表现出色&#xff0c;但尚未有针对金融领域的…

ansible-api分析(VariableManager变量)

一. 简述&#xff1a; ansible是一个非常强大的工具&#xff0c;可以支持多种类型(字符,数字,列表&#xff0c;字典等)的变量。除了有大量的内置变量及fact变量&#xff0c;也可以通过多种方式进行变量自定义 。不同方式定义的变量&#xff0c;优先级也不太一样&#xff0c;之…

2025年PMP考试最新报名通知

经PMI和中国国际人才交流基金会研究决定&#xff0c;中国大陆地区2025年第一期PMI认证考试定于3月15日举办。在基金会网站报名参加本次PMI认证考试的考生须认真阅读下文&#xff0c;知悉考试安排及注意事项&#xff0c;并遵守考试有关规定。 一、时间安排 &#xff08;一&#…

Mysql--基础篇--数据类型(整数,浮点数,日期,枚举,二进制,空间类型等)

MySQL提供了多种数据类型&#xff0c;用于定义表中列的数据格式。选择合适的数据类型不仅可以提高查询性能&#xff0c;还能确保数据的完整性和准确性。 一、数值类型 数值类型用于存储整数、浮点数和定点数。根据精度和范围的不同&#xff0c;数值类型可以分为以下几类&…

1-Transformer算法解读

目录 一.RNN与Transfrmer 二.word2vec 三.自注意力机制 四.辅助向量Q/K/V 五.计算过程 六.整体架构​编辑 七.Bert 一.RNN与Transfrmer RNN(循环神经网络)和Transformer都是深度学习中用于处理序列数据的模型,但它们在结构和性能上有显著的区别。以下是它们的一些…

Java Web开发进阶——Spring Boot与Spring Data JPA

Spring Data JPA 是 Spring 提供的一种面向数据访问的持久化框架&#xff0c;它简化了 JPA 的实现&#xff0c;为开发者提供了一种快速操作数据库的方式。在结合 Spring Boot 使用时&#xff0c;开发者能够快速完成数据库访问层的开发。 1. 介绍Spring Data JPA 1.1 什么是Spr…

【计算机操作系统:一、绪论】

第1章 绪论 1.1 操作系统在计算机系统中的地位 1.1.1 存储程序式计算机的结构和特点 存储程序式计算机&#xff08;Stored Program Computer&#xff09;是现代计算机的基础&#xff0c;其概念源于冯诺依曼&#xff08;John von Neumann&#xff09;提出的模型。这种计算机架…

如何查看服务器上的MySQL/Redis等系统服务状态和列表

如果呢你知道系统服务名称&#xff0c;要看状态很简单&#xff1a; systemctl status server-name 比如 systemctl status nginxsystemctl status redis # 等 这是一个nginx的示例&#xff1a; 那问题是 当你不知道服务名称时该怎么办。举个例子&#xff0c;比如mysql在启动…

安科瑞Acrel-1000DP分布式光伏监控系统在浙江安吉成3234.465kWp分布式光伏发电项目中的应用

摘 要&#xff1a;分布式光伏发电站是指将光伏发电组件安装在用户的建筑物屋顶、空地或其他适合的场地上&#xff0c;利用太阳能进行发电的一种可再生能源利用方式&#xff0c;与传统的大型集中式光伏电站相比&#xff0c;分布式光伏发电具有更灵活的布局、更低的建设成本和更高…

探索 Vue.js 的动态样式与交互:一个有趣的样式调整应用

修改日期备注2025.1.3初版 一、前言 今天和大家分享在 Vue.js 学习过程中开发的超酷的小应用。这个应用可以让我们通过一些简单的交互元素&#xff0c;如复选框、下拉菜单和输入框&#xff0c;来动态地改变页面上元素的样式哦 让我们一起深入了解一下这个项目的实现过程&…

大数据-268 实时数仓 - ODS层 将 Kafka 中的维度表写入 DIM

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; Java篇开始了&#xff01; MyBatis 更新完毕目前开始更新 Spring&#xff0c;一起深入浅出&#xff01; 目前已经更新到了&#xff1a; H…

把vue项目或者vue组件发布成npm包或者打包成lib库文件本地使用

将vue项目发布成npm库文件&#xff0c;第三方通过npm依赖安装使用&#xff1b;使用最近公司接了一个项目&#xff0c;这个项目需要集成到第三方页面&#xff0c;在第三方页面点击项目名称&#xff0c;页面变成我们的项目页面&#xff1b;要求以npm库文件提供给他们&#xff1b;…