Title (标题Headline)
章节标题组件,用于展示页面章节标题,支持GSAP滚动动画、倒计时、链接按钮等功能。【✅ 已发布】
加载中...
当前视口: 1920px × 600px场景: 基础标题(左对齐,浅色)
打开链接功能特性
- ✅ 滚动动画 - GSAP SplitText 逐词淡入动画,增强视觉吸引力
- ✅ 倒计时功能 - 支持活动倒计时展示,可自定义显示单位
- ✅ 链接按钮 - 可选的”查看更多”链接按钮
- ✅ 语义化标签 - 支持 h1-h6 语义化 HTML 标签,利于 SEO
- ✅ 主题支持 - 亮色和暗色两种主题
- ✅ 多对齐方式 - 左对齐、居中、右对齐
- ✅ 副标题 - 支持显示副标题文本
- ✅ 富文本 - 标题和副标题支持 HTML 格式
- ✅ 性能优化 - 使用 IntersectionObserver 仅在可见时触发动画
- ✅ 倒计时自动隐藏 - 倒计时结束后自动隐藏
Props 参数
TitleProps
| Prop | 类型 | 默认值 | 必需 | 说明 |
|---|---|---|---|---|
data | TitleData | - | ✅ | 标题数据配置对象 |
as | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'h2' | - | HTML 标签 |
weight | 'bold' | 'medium' | 'normal' | 'bold' | - | 字重 |
className | string | '' | - | 自定义类名 |
TitleData 配置
| 参数 | 类型 | 默认值 | 必需 | 说明 |
|---|---|---|---|---|
title | string | - | ✅ | 主标题(支持 HTML) |
caption | string | - | - | 标题文本(优先于 title 显示) |
subtitle | string | - | - | 副标题 |
theme | 'light' | 'dark' | 'light' | - | 主题 |
align | 'left' | 'center' | 'right' | 'left' | - | 对齐方式 |
showCountdown | boolean | false | - | 是否显示倒计时 |
countdown | CountdownConfig | - | - | 倒计时配置 |
extensions | object | - | - | 扩展配置(链接按钮等) |
CountdownConfig
| 参数 | 类型 | 默认值 | 必需 | 说明 |
|---|---|---|---|---|
targetDate | string | - | ✅ | 目标日期(ISO 8601 格式) |
showDays | boolean | true | - | 显示天数 |
showHours | boolean | true | - | 显示小时 |
showMinutes | boolean | true | - | 显示分钟 |
showSeconds | boolean | true | - | 显示秒数 |
labels | object | - | - | 自定义单位标签 |
使用示例
最简示例
import { Title } from '@anker-in/headless-ui/biz'
import scenarios from './scenarios.json'
<Title data={{ title: 'Featured Products' }} />带副标题
<Title
data={{
title: 'New Arrivals',
subtitle: 'Check out our latest products'
}}
/>居中标题
<Title
data={{
title: 'Why Choose Us',
align: 'center'
}}
/>暗色主题
<Title
data={{
title: 'Premium Quality',
theme: 'dark',
subtitle: 'Excellence in every detail'
}}
/>带倒计时
<Title
data={{
title: 'Limited Time Offer',
showCountdown: true,
countdown: {
targetDate: '2026-12-31T23:59:59',
showDays: true,
showHours: true,
showMinutes: true,
showSeconds: false,
}
}}
/>自定义倒计时单位标签
<Title
data={{
title: 'Flash Sale Ends In',
showCountdown: true,
countdown: {
targetDate: '2026-12-25T00:00:00',
showDays: true,
showHours: true,
showMinutes: true,
showSeconds: false,
labels: {
days: 'Days',
hours: 'Hours',
minutes: 'Min'
}
}
}}
/>带链接按钮
<Title
data={{
title: 'Best Sellers',
extensions: {
textLink: 'View All',
link: '/products'
}
}}
/>使用不同 HTML 标签
// 页面主标题
<Title
data={{ title: 'Welcome to Our Store' }}
as="h1"
/>
// 区块标题
<Title
data={{ title: 'Featured Categories' }}
as="h2"
/>
// 小节标题
<Title
data={{ title: 'Customer Reviews' }}
as="h3"
weight="medium"
/>富文本标题
<Title
data={{
title: `The World's <span className="text-primary">No.1</span> Charging Brand`,
subtitle: 'Trusted by millions worldwide'
}}
/>响应式行为
对齐方式
| align 值 | 效果 | 适用场景 |
|---|---|---|
| left | 左对齐 | 默认,大部分内容场景 |
| center | 居中 | 页面标题、特殊展示区域 |
| right | 右对齐 | 特殊布局需求 |
字体大小
组件会根据屏幕尺寸自动调整字体大小:
| 断点 | 屏幕宽度 | 主标题 | 副标题 |
|---|---|---|---|
| Mobile | < 1025px | 基础大小 | 基础大小 |
| Laptop+ | ≥ 1025px | 较大 | 较大 |
具体字体大小由 as 参数(h1-h6)决定。
设计规范
标题长度建议
- 主标题: 建议不超过 50 个字符(中文 25 个字)
- 副标题: 建议不超过 100 个字符(中文 50 个字)
- 过长的标题会影响可读性和视觉效果
语义化标签选择
- h1: 页面主标题(每页仅一个)
- h2: 页面子标题或区块标题
- h3-h6: 更小的标题层级
选择合适的标签有利于 SEO 和可访问性。
对齐方式选择
- 左对齐: 默认,适合大部分内容场景
- 居中: 适合页面标题、英雄区、特殊展示区域
- 右对齐: 特殊布局需求
倒计时使用场景
- 限时优惠活动
- 新品发布倒计时
- 活动截止提醒
- 会员权益到期提醒
链接按钮使用建议
- 文本简短明了(如”查看更多”、“了解详情”)
- 明确指向目标页面
- 避免过度使用(每个章节最多一个)
无障碍性
语义化标签
- ✅ 使用语义化 HTML 标签(h1-h6可配置)
- ✅ 文字颜色对比度符合 WCAG AA 标准
- ✅ 支持富文本 HTML,保持结构清晰
- ✅ GSAP 动画使用 scrub 模式,跟随滚动,用户可控
倒计时
- ✅ 倒计时结束后自动隐藏
- ✅ 时间格式清晰易读
- ⚠️ 建议添加
aria-live="polite"用于倒计时更新通知
链接按钮
- ✅ 链接文本清晰描述目标
- ✅ 支持键盘导航
- ✅ 足够的点击区域(移动端最小 44x44px)
性能优化
- ✅ IntersectionObserver - 使用 react-intersection-observer 仅在可见时触发 GSAP 动画
- ✅ GSAP ScrollTrigger - 使用 scrub 模式,性能优于 JavaScript 定时器
- ✅ 清理机制 - 组件卸载时自动清理 ScrollTrigger 和 SplitText 实例
- ✅ 倒计时优化 - 倒计时结束后自动停止定时器
- ✅ 条件渲染 - 不需要的功能不会渲染额外 DOM 节点
GSAP 性能说明
组件使用 GSAP 的 SplitText 和 ScrollTrigger 插件,这是业界领先的动画库:
- SplitText: 将文本拆分为单词,实现逐词动画
- ScrollTrigger: 滚动驱动动画,性能优异
- scrub 模式: 动画进度与滚动位置同步,无延迟
常见问题
标题文字为什么是逐词淡入?
组件使用 GSAP 的 SplitText 插件将标题拆分为单词,然后根据滚动进度逐词淡入。这种渐进式动画可以吸引用户注意力,增强品牌感。
如何禁用滚动动画?
当前版本滚动动画是内置的,无法直接禁用。如需禁用,可以修改源代码注释掉 GSAP 相关逻辑。
倒计时结束后会发生什么?
倒计时到达 targetDate 后会自动隐藏。标题和副标题仍然正常显示。
如何自定义倒计时样式?
通过全局 CSS 覆盖倒计时相关的类名。具体类名请参考组件源代码。
链接按钮支持哪些属性?
extensions 对象支持以下属性:
textLink: 链接文本link: 链接 URL- 其他标准的
<a>标签属性
caption 和 title 有什么区别?
caption: 优先显示,如果存在则使用 captiontitle: caption 不存在时使用 title- 建议只使用其中一个,避免混淆
如何在标题中添加换行?
使用 HTML 的 <br> 标签:
<Title
data={{
title: `Best Charging<br />Technology`
}}
/>倒计时可以自定义格式吗?
可以通过 labels 对象自定义单位标签:
countdown: {
targetDate: '2026-12-31T23:59:59',
labels: {
days: '天',
hours: '时',
minutes: '分',
seconds: '秒'
}
}技术实现
基于 GSAP
组件基于 GSAP (GreenSock Animation Platform) 实现,使用以下插件:
- SplitText: 文本拆分工具,将标题拆分为单词
- ScrollTrigger: 滚动触发器,监听滚动位置控制动画
倒计时实现
倒计时基于 dayjs 实现,每秒更新一次:
useEffect(() => {
const interval = setInterval(() => {
const now = dayjs()
const diff = targetDate.diff(now)
if (diff <= 0) {
setIsCountdownEnded(true)
clearInterval(interval)
return
}
updateCountdown(diff)
}, 1000)
return () => clearInterval(interval)
}, [targetDate])GSAP 滚动动画
useEffect(() => {
gsap.registerPlugin(ScrollTrigger, SplitText)
if (inView) {
// 拆分标题文本为单词
splitTextInstance.current = new SplitText(titleRef.current, {
type: 'words',
wordsClass: 'word',
})
const words = splitTextInstance.current.words
gsap.set(words, { opacity: 0 })
// 创建滚动触发器
scrollTriggerRef.current = ScrollTrigger.create({
trigger: titleRef.current,
start: 'bottom bottom-=4%',
end: `bottom+=${height * 1.5}px bottom-=4%`,
scrub: true,
onUpdate: (self: any) => {
const progress = self.progress
words.forEach((word: any, i: number) => {
const start = (i / total) * (1 - overlap)
const width = interval * (1 + overlap)
let opacity = (progress - start) / width
opacity = Math.max(0, Math.min(opacity, 1))
gsap.set(word, { opacity })
})
},
})
}
// 组件卸载时清理
return () => {
splitTextInstance.current && splitTextInstance.current.revert()
scrollTriggerRef.current && scrollTriggerRef.current.kill()
}
}, [inView])相关资源
- 完整 README 文档 - 详细的 API 文档和使用指南
- Figma 设计稿 - 查看设计规范
- GSAP 文档 - GSAP 动画库文档
- SplitText 文档 - SplitText 插件文档
- ScrollTrigger 文档 - ScrollTrigger 插件文档
- 源代码 - GitHub 源代码
Last updated on