恋爱倒计时组件添加教程

分钟
恋爱倒计时组件添加教程
AI 概括

点击按钮,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,
},
// ... 其他组件
],

五、配置说明

配置参数

参数类型必填说明
startDatestring恋爱开始日期,格式:YYYY-MM-DD
name1string第一个人的名字
name2string第二个人的名字
avatar1string第一个人的头像 URL
avatar2string第二个人的头像 URL
titlestring小组件标题,默认”恋爱时光”

自定义配置

修改 src/config/relationshipConfig.ts 文件:

export const relationshipConfig: RelationshipConfig = {
startDate: "2024-02-14", // 修改为你的恋爱开始日期
name1: "小明", // 修改为第一个人的名字
name2: "小红", // 修改为第二个人的名字
avatar1: "/images/avatar1.jpg", // 修改为第一个人的头像
avatar2: "/images/avatar2.jpg", // 修改为第二个人的头像
title: "我们在一起已经", // 修改为自定义标题
};

六、使用方法

启用组件

  1. 确保 sidebarConfig.tsrelationship 组件的 enable 设置为 true
  2. 重新构建或重启开发服务器

查看效果

  1. 访问博客首页
  2. 查看右侧边栏(桌面端)或页面底部(移动端)
  3. 应该能看到恋爱倒计时组件

测试功能

  • ✅ 计时器每秒更新
  • ✅ 鼠标悬停头像有放大效果
  • ✅ 鼠标悬停爱心有旋转效果
  • ✅ 深色模式自动切换

七、注意事项

1. 日期格式

startDate 必须使用 YYYY-MM-DD 格式,例如:

  • ✅ 正确:"2026-01-06"
  • ❌ 错误:"2026/01/06""01-06-2026"

2. 头像图片

  • 使用网络图片 URL(推荐)
  • 或使用本地图片路径(放在 public 目录下)
  • 建议使用正方形图片,效果最佳

3. 组件位置

组件可以配置在以下位置:

位置说明
top固定在顶部
sticky粘性定位,跟随滚动

4. 显示页面

可以通过 showOnPostPageshowOnNonPostPage 控制显示页面:

  • showOnPostPage: true - 在文章详情页显示
  • showOnNonPostPage: true - 在非文章详情页显示

5. Swup 兼容

组件已适配 Swup 的 SPA 导航,页面切换时会自动更新计时器。


八、常见问题

Q1: 计时器不显示?

可能原因:

  • 组件未正确注册
  • 配置文件未创建
  • enable 设置为 false

解决方案:

  1. 检查 SideBar.astro 中是否导入了组件
  2. 检查 sidebarConfig.ts 中是否启用了组件
  3. 确认 relationshipConfig.ts 文件存在

Q2: 头像不显示?

可能原因:

  • 图片 URL 错误
  • 图片跨域问题
  • 图片路径错误

解决方案:

  1. 检查图片 URL 是否正确
  2. 尝试使用网络图片 URL
  3. 检查浏览器控制台是否有错误

Q3: 计时器数字不更新?

可能原因:

  • JavaScript 执行错误
  • 定时器未启动

解决方案:

  1. 检查浏览器控制台是否有错误
  2. 刷新页面重试
  3. 检查 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 文件即可自定义双方信息!🎉

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
恋爱倒计时组件添加教程
https://f3f3.top/posts/relationship-timer/
作者
lyf
发布于
2026-06-07
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
lyf
Hello, I'm LyF.
公告
欢迎来到一飞的博客!。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
我和宝宝在一起已经
---------TSH ❤️ CXY---------
---------TSH
❤️
CXY---------
0 0 0 0 0 00
分类
标签
站点统计
文章
17
分类
5
标签
24
总字数
51,329
运行时长
0
最后活动
0 天前

文章目录

🤖 AI 助手

👋 你好!

我可以帮你解答关于这篇文章的问题