恋爱倒计时组件添加教程
点击按钮,AI 将为你生成这篇文章的摘要
一、功能介绍
恋爱倒计时组件是一个浪漫的侧边栏小组件,可以实时显示双方在一起的时间,支持双人头像和个性化问候。
功能特点
- ✅ 实时计时:每秒更新,显示年、月、天、时、分、秒
- ✅ 双人头像:显示双方头像和爱心
- ✅ 个性化问候:显示双方名字
- ✅ 响应式设计:适配桌面端和移动端
- ✅ 深色模式支持:自动切换主题样式
- ✅ Swup 兼容:页面切换时自动更新
二、效果演示
| 场景 | 效果 |
|---|---|
| 桌面端 | 显示在右侧边栏,包含双人头像、爱心和计时器 |
| 移动端 | 显示在页面底部,宽度自适应 |
| 深色模式 | 自动切换深色主题 |
三、涉及的文件
整个组件涉及 5 个文件:
| 文件 | 路径 | 说明 |
|---|---|---|
| 组件本身 | src/components/widget/RelationshipTimer.astro | 核心组件代码 |
| 配置文件 | src/config/relationshipConfig.ts | 双方信息配置 |
| 类型定义 | src/types/config.ts | 添加 relationship 类型 |
| 侧边栏映射 | src/components/layout/SideBar.astro | 注册组件 |
| 侧边栏配置 | src/config/sidebarConfig.ts | 启用组件 |
四、完整代码实现
步骤 1:创建组件文件
创建 src/components/widget/RelationshipTimer.astro:
<!-- 恋爱倒计时组件 -->---import WidgetLayout from "@/components/common/WidgetLayout.astro";import { relationshipConfig } from "@/config/relationshipConfig";
const config = relationshipConfig;
interface Props { class?: string; style?: string;}const className = Astro.props.class;const style = Astro.props.style;---
<WidgetLayout name={config.title || "恋爱时光"} id="relationship" class={className} style={style}> <div class="flex flex-col items-center p-2"> <!-- 顶部名称栏 --> <div class="flex items-center justify-center gap-2 mb-4 text-sm font-medium text-neutral-600 dark:text-neutral-400"> <span>{config.name1}</span> <span class="text-red-500 animate-pulse">❤️</span> <span>{config.name2}</span> </div>
<!-- 头像和爱心 --> <div class="flex items-center justify-center gap-4 mb-6"> <div class="w-16 h-16 rounded-full overflow-hidden border-2 border-white dark:border-neutral-800 shadow-md transition-transform hover:scale-110"> <img src={config.avatar1} alt={config.name1} class="w-full h-full object-cover" /> </div> <div class="text-2xl text-red-500 transform transition-all hover:scale-125 hover:rotate-12 cursor-pointer"> ❤️ </div> <div class="w-16 h-16 rounded-full overflow-hidden border-2 border-white dark:border-neutral-800 shadow-md transition-transform hover:scale-110"> <img src={config.avatar2} alt={config.name2} class="w-full h-full object-cover" /> </div> </div>
<!-- 计时器 --> <div class="relationship-timer flex items-center justify-center gap-x-1 text-sm whitespace-nowrap overflow-x-auto"> <span class="timer-num" data-timer-years>0</span><span class="timer-unit bg-red-400">年</span> <span class="timer-num" data-timer-months>0</span><span class="timer-unit bg-red-300">月</span> <span class="timer-num" data-timer-days>0</span><span class="timer-unit bg-blue-400">天</span> <span class="timer-num" data-timer-hours>0</span><span class="timer-unit bg-orange-400">时</span> <span class="timer-num" data-timer-minutes>0</span><span class="timer-unit bg-blue-500">分</span> <span class="timer-num" data-timer-seconds>00</span><span class="timer-unit bg-purple-400">秒</span> </div> </div></WidgetLayout>
<style> @reference "tailwindcss";
.timer-num { @apply font-bold text-neutral-700 dark:text-neutral-200 min-w-[0rem] text-center; color: var(--primary); } .timer-unit { @apply px-1 py-0.5 rounded text-[10px] text-white font-medium shrink-0; }</style>
<script is:inline define:vars={{ startDateStr: config.startDate }}> function updateRelationshipTimer() { const startDate = new Date(startDateStr); const now = new Date();
let diff = now.getTime() - startDate.getTime();
if (diff < 0) return;
// 计算年月日时分秒的差值 let years = now.getFullYear() - startDate.getFullYear(); let months = now.getMonth() - startDate.getMonth(); let days = now.getDate() - startDate.getDate(); let hours = now.getHours() - startDate.getHours(); let minutes = now.getMinutes() - startDate.getMinutes(); let seconds = now.getSeconds() - startDate.getSeconds();
// 处理借位逻辑 if (seconds < 0) { seconds += 60; minutes--; } if (minutes < 0) { minutes += 60; hours--; } if (hours < 0) { hours += 24; days--; } if (days < 0) { const lastMonth = new Date(now.getFullYear(), now.getMonth(), 0); days += lastMonth.getDate(); months--; } if (months < 0) { months += 12; years--; }
// 更新所有实例,避免移动端和电脑端 ID 冲突 const update = (attr, value) => { const elements = document.querySelectorAll(`[data-timer-${attr}]`); elements.forEach(el => { el.innerText = value; }); };
update('years', years); update('months', months); update('days', days); update('hours', hours); update('minutes', minutes); update('seconds', seconds.toString().padStart(2, '0')); }
// 立即运行并每秒更新 updateRelationshipTimer(); if (!window.relationshipTimerInterval) { window.relationshipTimerInterval = setInterval(updateRelationshipTimer, 1000); }
// 监听 Swup 页面切换 document.addEventListener('swup:content:replace', updateRelationshipTimer);</script>步骤 2:创建配置文件
创建 src/config/relationshipConfig.ts:
import type { RelationshipConfig } from "../types/config";
export const relationshipConfig: RelationshipConfig = { // 恋爱开始日期 startDate: "2026-01-06", // 双方名称 name1: "---------TSH", name2: "CXY---------", // 双方头像 avatar1: "https://re.tsh520.cn/zl/tsh.jpg", avatar2: "https://re.tsh520.cn/zl/cxy.jpg", // 小组件标题 title: "我和宝宝在一起已经",};步骤 3:修改类型定义
打开 src/types/config.ts,添加以下内容:
添加到 WidgetComponentType:
// 组件配置类型定义export type WidgetComponentType = | "profile" | "announcement" | "categories" | "tags" | "sidebarToc" | "advertisement" | "stats" | "calendar" | "music" | "relationship"; // 新增添加 RelationshipConfig 类型:
// 恋爱倒计时配置export type RelationshipConfig = { startDate: string; // 恋爱开始日期,格式:YYYY-MM-DD name1: string; // 第一个人的名字 name2: string; // 第二个人的名字 avatar1: string; // 第一个人的头像 URL avatar2: string; // 第二个人的头像 URL title?: string; // 小组件标题};步骤 4:修改侧边栏组件映射
打开 src/components/layout/SideBar.astro:
添加导入:
---import RelationshipTimer from "@/components/widget/RelationshipTimer.astro";// ... 其他导入---添加到组件映射表:
// 组件映射表const componentMap = { // ... 其他组件 relationship: RelationshipTimer,} satisfies Record<WidgetComponentType, typeof Profile>;步骤 5:修改侧边栏配置
打开 src/config/sidebarConfig.ts,在 rightComponents 中添加:
// 右侧边栏组件配置列表rightComponents: [ // ... 其他组件 { // 组件类型:恋爱计时组件 type: "relationship", // 是否启用该组件 enable: true, // 组件位置 position: "top", // 是否在文章详情页显示 showOnPostPage: true, }, // ... 其他组件],在 mobileBottomComponents 中添加:
// 移动端底部组件配置列表mobileBottomComponents: [ // ... 其他组件 { // 组件类型:恋爱计时组件 type: "relationship", // 是否启用该组件 enable: true, // 是否在文章详情页显示 showOnPostPage: true, }, // ... 其他组件],五、配置说明
配置参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| startDate | string | ✅ | 恋爱开始日期,格式:YYYY-MM-DD |
| name1 | string | ✅ | 第一个人的名字 |
| name2 | string | ✅ | 第二个人的名字 |
| avatar1 | string | ✅ | 第一个人的头像 URL |
| avatar2 | string | ✅ | 第二个人的头像 URL |
| title | string | ❌ | 小组件标题,默认”恋爱时光” |
自定义配置
修改 src/config/relationshipConfig.ts 文件:
export const relationshipConfig: RelationshipConfig = { startDate: "2024-02-14", // 修改为你的恋爱开始日期 name1: "小明", // 修改为第一个人的名字 name2: "小红", // 修改为第二个人的名字 avatar1: "/images/avatar1.jpg", // 修改为第一个人的头像 avatar2: "/images/avatar2.jpg", // 修改为第二个人的头像 title: "我们在一起已经", // 修改为自定义标题};六、使用方法
启用组件
- 确保
sidebarConfig.ts中relationship组件的enable设置为true - 重新构建或重启开发服务器
查看效果
- 访问博客首页
- 查看右侧边栏(桌面端)或页面底部(移动端)
- 应该能看到恋爱倒计时组件
测试功能
- ✅ 计时器每秒更新
- ✅ 鼠标悬停头像有放大效果
- ✅ 鼠标悬停爱心有旋转效果
- ✅ 深色模式自动切换
七、注意事项
1. 日期格式
startDate 必须使用 YYYY-MM-DD 格式,例如:
- ✅ 正确:
"2026-01-06" - ❌ 错误:
"2026/01/06"或"01-06-2026"
2. 头像图片
- 使用网络图片 URL(推荐)
- 或使用本地图片路径(放在
public目录下) - 建议使用正方形图片,效果最佳
3. 组件位置
组件可以配置在以下位置:
| 位置 | 说明 |
|---|---|
top | 固定在顶部 |
sticky | 粘性定位,跟随滚动 |
4. 显示页面
可以通过 showOnPostPage 和 showOnNonPostPage 控制显示页面:
showOnPostPage: true- 在文章详情页显示showOnNonPostPage: true- 在非文章详情页显示
5. Swup 兼容
组件已适配 Swup 的 SPA 导航,页面切换时会自动更新计时器。
八、常见问题
Q1: 计时器不显示?
可能原因:
- 组件未正确注册
- 配置文件未创建
enable设置为false
解决方案:
- 检查
SideBar.astro中是否导入了组件 - 检查
sidebarConfig.ts中是否启用了组件 - 确认
relationshipConfig.ts文件存在
Q2: 头像不显示?
可能原因:
- 图片 URL 错误
- 图片跨域问题
- 图片路径错误
解决方案:
- 检查图片 URL 是否正确
- 尝试使用网络图片 URL
- 检查浏览器控制台是否有错误
Q3: 计时器数字不更新?
可能原因:
- JavaScript 执行错误
- 定时器未启动
解决方案:
- 检查浏览器控制台是否有错误
- 刷新页面重试
- 检查
startDate格式是否正确
Q4: 如何修改计时器样式?
解决方案:
修改组件中的 <style> 部分:
.timer-num { /* 数字样式 */ color: var(--primary);}
.timer-unit { /* 单位标签样式 */ background: #ef4444;}Q5: 如何禁用此功能?
解决方案:
在 sidebarConfig.ts 中将 enable 设置为 false:
{ type: "relationship", enable: false, // 禁用组件 // ...}九、总结
恋爱倒计时组件是一个浪漫的侧边栏小组件,通过修改 5 个文件实现:
| 文件 | 修改内容 |
|---|---|
src/components/widget/RelationshipTimer.astro | 新建组件文件 |
src/config/relationshipConfig.ts | 新建配置文件 |
src/types/config.ts | 添加类型定义 |
src/components/layout/SideBar.astro | 注册组件 |
src/config/sidebarConfig.ts | 启用组件 |
功能特点:
- 实时计时:每秒更新
- 双人头像:显示双方头像和爱心
- 个性化问候:显示双方名字
- 响应式设计:适配桌面端和移动端
- 深色模式支持:自动切换主题样式
- Swup 兼容:页面切换时自动更新
配置简单,只需修改 relationshipConfig.ts 文件即可自定义双方信息!🎉
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!