01三个项目,三种能力
项目不在多,而在于每个项目都解决一类问题。下面三个项目分别对应:静态页面排版、状态驱动交互、Canvas 数据可视化。
| 项目 | 核心能力 | 难度 | 涉及章节 |
|---|---|---|---|
| 小清新个人主页 | 语义化、Flex/Grid、响应式 | 入门 | HTML + CSS |
| 待办清单 | 状态管理、DOM 渲染、localStorage | 进阶 | JavaScript |
| Canvas 数据面板 | 绘图 API、动画、高分屏适配 | 高级 | 进阶篇 |
统一的目录建议:每个项目独立一个文件夹,内部只放 index.html、style.css、app.js 三个文件。不要过早引入构建工具。
02项目一:小清新个人主页
一个安静的单页个人介绍:左文右图、柔和配色、纸张纹理。目标是用最少的代码做出「有质感」的排版。
HTML 结构
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>拾光 · 前端学习者</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="site-header">
<a class="logo" href="#">拾光</a>
<nav aria-label="主导航">
<ul class="nav">
<li><a href="#about">关于</a></li>
<li><a href="#works">作品</a></li>
<li><a href="#contact">联系</a></li>
</ul>
</nav>
</header>
<main>
<section class="hero" id="about">
<div class="hero__text">
<p class="eyebrow">Front-end Learner</p>
<h1>你好,<br>我是<em>拾光</em>。</h1>
<p class="lead">
一个喜欢在清晨写代码的人。相信慢一点、稳一点,
也能抵达想去的地方。
</p>
<a class="btn" href="#works">看看我的作品 →</a>
</div>
<figure class="hero__art">
<div class="paper-card">
<span>慢慢来<br>比较快</span>
</div>
</figure>
</section>
<section class="works" id="works">
<h2>最近在做</h2>
<ul class="work-list">
<li class="work">
<h3>拾光学习手记</h3>
<p>一个纯手写的前端学习网站。</p>
<span>HTML / CSS / JS</span>
</li>
<li class="work">
<h3>山中日历</h3>
<p>记录每个节气的颜色。</p>
<span>Canvas</span>
</li>
<li class="work">
<h3>纸页笔记</h3>
<p>极简的本地便签应用。</p>
<span>localStorage</span>
</li>
</ul>
</section>
<section class="contact" id="contact">
<h2>写封信给我</h2>
<p><a href="mailto:hi@example.com">hi@example.com</a></p>
</section>
</main>
<footer>
<p>© 2026 拾光 · 用 HTML + CSS 手写而成</p>
</footer>
</body>
</html>
样式与响应式
:root {
--paper: #faf7f0;
--ink: #33302b;
--ink-soft: #6f6a61;
--green: #4c6b57;
--green-light: #e7efe6;
--pink: #dcb3b0;
--serif: "Songti SC", "Noto Serif SC", serif;
}
*, *::before, *::after { box-sizing: border-box; }
body {
margin: 0;
font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
color: var(--ink);
background: var(--paper);
line-height: 1.9;
}
a { color: var(--green); text-decoration: none; }
ul { list-style: none; padding: 0; margin: 0; }
/* 页头 */
.site-header {
display: flex;
align-items: center;
justify-content: space-between;
max-width: 1080px;
margin: 0 auto;
padding: 26px 24px;
}
.logo {
font-family: var(--serif);
font-size: 20px;
letter-spacing: 3px;
}
.nav {
display: flex;
gap: 26px;
font-size: 14px;
}
/* Hero:左文右图 */
.hero {
display: grid;
grid-template-columns: 1.1fr 0.9fr;
gap: 56px;
align-items: center;
max-width: 1080px;
margin: 0 auto;
padding: 60px 24px 80px;
}
.eyebrow {
font-size: 11px;
letter-spacing: 4px;
color: var(--green);
text-transform: uppercase;
margin: 0 0 18px;
}
.hero h1 {
font-family: var(--serif);
font-size: clamp(36px, 5vw, 56px);
line-height: 1.35;
letter-spacing: 2px;
margin: 0 0 22px;
font-weight: 600;
}
.hero h1 em {
font-style: normal;
color: var(--green);
border-bottom: 8px solid var(--green-light);
}
.lead {
color: var(--ink-soft);
max-width: 420px;
margin: 0 0 30px;
}
.btn {
display: inline-block;
padding: 12px 28px;
border-radius: 999px;
background: var(--green);
color: #fff;
font-size: 14px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.btn:hover {
transform: translateY(-3px);
box-shadow: 0 12px 26px rgba(76, 107, 87, 0.25);
}
/* 右侧纸张装饰 */
.hero__art {
margin: 0;
display: grid;
place-items: center;
aspect-ratio: 1 / 1;
background:
radial-gradient(circle at 30% 25%, #fff, transparent 60%),
linear-gradient(145deg, var(--green-light), #f7ecea);
border-radius: 50%;
}
.paper-card {
width: 62%;
aspect-ratio: 3 / 4;
display: grid;
place-items: center;
background: #fffdf8;
border: 1px solid rgba(76, 107, 87, 0.15);
border-radius: 4px 14px 14px 4px;
box-shadow: 0 18px 40px rgba(80, 72, 55, 0.12);
transform: rotate(-4deg);
font-family: var(--serif);
font-size: 20px;
letter-spacing: 5px;
color: var(--green);
line-height: 2.1;
text-align: center;
writing-mode: vertical-rl;
}
/* 作品列表 */
.works { max-width: 1080px; margin: 0 auto; padding: 0 24px 80px; }
.works h2,
.contact h2 {
font-family: var(--serif);
font-size: 26px;
letter-spacing: 2px;
margin: 0 0 26px;
}
.work-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 18px;
}
.work {
padding: 24px 22px;
background: #fffdf8;
border: 1px solid rgba(76, 107, 87, 0.12);
border-radius: 14px;
transition: transform 0.35s ease, box-shadow 0.35s ease;
}
.work:hover {
transform: translateY(-5px);
box-shadow: 0 16px 34px rgba(80, 72, 55, 0.09);
}
.work h3 {
font-family: var(--serif);
font-size: 17px;
margin: 0 0 8px;
letter-spacing: 1px;
}
.work p { font-size: 13px; color: var(--ink-soft); margin: 0 0 12px; }
.work span { font-size: 11px; color: #948d81; letter-spacing: 1px; }
/* 联系与页脚 */
.contact { text-align: center; padding: 0 24px 90px; }
footer {
text-align: center;
font-size: 12px;
color: #948d81;
padding: 26px 0;
border-top: 1px solid rgba(76, 107, 87, 0.1);
}
/* 响应式:窄屏改为单列 */
@media (max-width: 760px) {
.hero {
grid-template-columns: 1fr;
gap: 36px;
padding-top: 30px;
}
.hero__art { max-width: 320px; width: 100%; margin: 0 auto; }
.work-list { grid-template-columns: 1fr; }
.nav { gap: 18px; }
}
拆解思路:先写语义结构,再确定「一屏一屏」的布局,最后才调颜色与字体。顺序反过来,很容易在细节里迷路。
03项目二:待办清单应用
这是一个真正「能用」的小应用:支持添加、勾选、删除、筛选与本地持久化。核心思想是单一数据源 + 状态驱动渲染。
完整 JavaScript
// ============ 1. 状态 ============
const STORAGE_KEY = 'shiguang-todos';
let todos = load();
let filter = 'all';
function load() {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
} catch (err) {
return [];
}
}
function save() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
}
// ============ 2. DOM ============
const form = document.querySelector('#todo-form');
const input = document.querySelector('#todo-input');
const list = document.querySelector('#todo-list');
const countEl = document.querySelector('#todo-count');
const filters = document.querySelector('#filters');
const clearBtn = document.querySelector('#clear-done');
// ============ 3. 渲染 ============
function getVisibleTodos() {
if (filter === 'active') return todos.filter(t => !t.done);
if (filter === 'done') return todos.filter(t => t.done);
return todos;
}
function render() {
const visible = getVisibleTodos();
list.innerHTML = '';
if (!visible.length) {
const empty = document.createElement('li');
empty.className = 'empty';
empty.textContent = '这里空空的,添加一件小事吧。';
list.append(empty);
}
visible.forEach(todo => {
const li = document.createElement('li');
li.className = 'todo' + (todo.done ? ' is-done' : '');
li.dataset.id = todo.id;
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = todo.done;
checkbox.className = 'todo__check';
const text = document.createElement('span');
text.className = 'todo__text';
text.textContent = todo.text;
label.append(checkbox, text);
const del = document.createElement('button');
del.type = 'button';
del.className = 'todo__del';
del.textContent = '删除';
del.setAttribute('aria-label', '删除:' + todo.text);
li.append(label, del);
list.append(li);
});
const left = todos.filter(t => !t.done).length;
countEl.textContent = `剩余 ${left} 项 / 共 ${todos.length} 项`;
// 同步筛选按钮状态
filters.querySelectorAll('button').forEach(btn => {
btn.classList.toggle('is-active', btn.dataset.filter === filter);
});
}
// ============ 4. 操作 ============
function addTodo(text) {
todos.unshift({
id: Date.now(),
text,
done: false,
createdAt: new Date().toISOString()
});
save();
render();
}
function toggleTodo(id) {
todos = todos.map(todo =>
todo.id === id ? { ...todo, done: !todo.done } : todo
);
save();
render();
}
function removeTodo(id) {
todos = todos.filter(todo => todo.id !== id);
save();
render();
}
// ============ 5. 事件 ============
form.addEventListener('submit', event => {
event.preventDefault();
const text = input.value.trim();
if (!text) {
input.focus();
return;
}
addTodo(text);
input.value = '';
input.focus();
});
// 事件委托:处理勾选与删除
list.addEventListener('click', event => {
const li = event.target.closest('.todo');
if (!li) return;
const id = Number(li.dataset.id);
if (event.target.matches('.todo__check')) {
toggleTodo(id);
}
if (event.target.matches('.todo__del')) {
li.classList.add('is-leaving');
setTimeout(() => removeTodo(id), 160);
}
});
// 筛选
filters.addEventListener('click', event => {
const btn = event.target.closest('button[data-filter]');
if (!btn) return;
filter = btn.dataset.filter;
render();
});
// 清除已完成
clearBtn.addEventListener('click', () => {
todos = todos.filter(todo => !todo.done);
save();
render();
});
// ============ 6. 初始化 ============
render();
配套 CSS
.todo {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 16px;
background: #fffdf8;
border: 1px solid rgba(76, 107, 87, 0.12);
border-radius: 10px;
transition: transform 0.2s ease, opacity 0.2s ease;
}
.todo.is-done .todo__text {
text-decoration: line-through;
color: #a9a396;
}
.todo.is-leaving {
transform: translateX(12px);
opacity: 0;
}
.todo label {
display: flex;
align-items: center;
gap: 10px;
flex: 1;
cursor: pointer;
}
.todo__check {
width: 16px;
height: 16px;
accent-color: #4c6b57;
}
.todo__del {
font-size: 11.5px;
color: #a3716d;
border: 1px solid rgba(220, 179, 176, 0.5);
background: transparent;
border-radius: 6px;
padding: 3px 9px;
cursor: pointer;
}
.todo__del:hover {
background: #f7ecea;
}
.empty {
padding: 20px;
text-align: center;
font-size: 13px;
color: #948d81;
}
/* 筛选按钮的选中态 */
#filters button.is-active {
background: #e7efe6;
border-color: #6f8f7a;
color: #4c6b57;
}
关于箭头函数:代码中的 todo => ... 是箭头函数写法,等价于 function (todo) { return ... }。它没有自己的 this,非常适合用于回调。
为什么这样写
- 单一数据源:所有 UI 都由
todos派生,避免「按钮改了但数据没改」的不一致。 - 先保存再渲染:任何修改都先更新数据并持久化,再调用
render()。 - 事件委托:列表再长也只有一个监听器,新增条目自动生效。
- 存储容错:读取时用
try/catch,防止本地数据损坏导致页面白屏。
04项目三:Canvas 数据面板
不引入图表库,用 Canvas 手写一个会「生长」的柱状图。涉及坐标换算、高分屏适配、缓动动画与鼠标交互。
核心绘图代码
const canvas = document.querySelector('#chartCanvas');
const ctx = canvas.getContext('2d');
const tooltip = document.querySelector('#chartTip');
const data = [
{ label: '一月', value: 32 },
{ label: '二月', value: 48 },
{ label: '三月', value: 41 },
{ label: '四月', value: 67 },
{ label: '五月', value: 58 },
{ label: '六月', value: 84 },
{ label: '七月', value: 96 },
{ label: '八月', value: 72 }
];
const padding = { top: 28, right: 18, bottom: 36, left: 40 };
let width = 0;
let height = 0;
let progress = 0;
let bars = [];
// 1. 适配高分屏
function resize() {
const dpr = window.devicePixelRatio || 1;
const rect = canvas.getBoundingClientRect();
width = rect.width;
height = rect.height;
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
render();
}
// 2. 缓动函数:让柱子「长出来」
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
// 3. 绘制
function render() {
ctx.clearRect(0, 0, width, height);
const chartW = width - padding.left - padding.right;
const chartH = height - padding.top - padding.bottom;
const maxValue = Math.max(...data.map(d => d.value));
const slot = chartW / data.length;
const barW = Math.min(34, slot * 0.46);
// 网格线与 Y 轴刻度
ctx.font = '10px system-ui, sans-serif';
ctx.fillStyle = '#948d81';
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
for (let i = 0; i <= 4; i++) {
const y = padding.top + chartH - (chartH / 4) * i;
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(width - padding.right, y);
ctx.strokeStyle = 'rgba(76, 107, 87, 0.1)';
ctx.setLineDash([4, 4]);
ctx.stroke();
ctx.setLineDash([]);
ctx.fillText(Math.round((maxValue / 4) * i), padding.left - 8, y);
}
// 柱形
bars = [];
data.forEach((item, index) => {
const x = padding.left + slot * index + slot / 2;
const fullH = (item.value / maxValue) * chartH;
const h = fullH * easeOutCubic(progress);
const y = padding.top + chartH - h;
// 渐变填充
const gradient = ctx.createLinearGradient(0, y, 0, y + h);
gradient.addColorStop(0, 'rgba(76, 107, 87, 0.85)');
gradient.addColorStop(1, 'rgba(111, 143, 122, 0.35)');
ctx.beginPath();
ctx.roundRect(x - barW / 2, y, barW, h, [6, 6, 0, 0]);
ctx.fillStyle = gradient;
ctx.fill();
// X 轴标签
ctx.fillStyle = '#6f6a61';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.font = '10px system-ui, sans-serif';
ctx.fillText(item.label, x, padding.top + chartH + 12);
// 记录位置,供交互命中检测
bars.push({ x: x - barW / 2, y, w: barW, h, item });
});
}
// 4. 入场动画
function animate() {
progress = 0;
const start = performance.now();
const duration = 900;
function frame(now) {
const t = Math.min((now - start) / duration, 1);
progress = t;
render();
if (t < 1) requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
}
// 5. 悬停提示
canvas.addEventListener('pointermove', event => {
const rect = canvas.getBoundingClientRect();
const mx = event.clientX - rect.left;
const my = event.clientY - rect.top;
const hit = bars.find(b =>
mx >= b.x - 8 && mx <= b.x + b.w + 8 &&
my >= b.y - 8 && my <= b.y + b.h + 8
);
if (hit) {
tooltip.textContent = `${hit.item.label} · ${hit.item.value}`;
tooltip.style.left = (hit.x + hit.w / 2) + 'px';
tooltip.style.top = hit.y + 'px';
tooltip.style.opacity = '1';
canvas.style.cursor = 'pointer';
} else {
tooltip.style.opacity = '0';
canvas.style.cursor = 'default';
}
});
canvas.addEventListener('pointerleave', () => {
tooltip.style.opacity = '0';
});
// 6. 初始化
window.addEventListener('resize', resize);
resize();
animate();
三个关键点:① 画布尺寸要乘 devicePixelRatio,再用 CSS 控制显示尺寸,否则高分屏会模糊;② 动画不要直接改数据,而是用 progress 乘以目标高度;③ 命中检测用「记录矩形 + 坐标比较」,比重新遍历图形便宜得多。
05部署上线:让别人看见
纯静态站点部署成本几乎为零。下面以 GitHub Pages 与 Netlify 为例,两者都免费且支持自定义域名。
方式一:GitHub Pages
# 1. 初始化仓库
git init
git add .
git commit -m "feat: 初始化拾光前端学习站"
# 2. 关联远程仓库(替换成你的地址)
git remote add origin https://github.com/yourname/your-repo.git
git branch -M main
git push -u origin main
# 3. 在仓库 Settings → Pages 中选择 main 分支与根目录
# 4. 等待片刻,访问 https://yourname.github.io/your-repo/
方式二:Netlify 拖拽部署
- 打开 Netlify,登录后进入 Sites 面板。
- 把整个项目文件夹直接拖进页面上的部署区域。
- 几十秒后获得一个
xxx.netlify.app的地址。 - 后续可以在 Site settings 中绑定自定义域名并开启 HTTPS。
路径注意:GitHub Pages 的项目站点地址带有子路径,引用资源时请使用相对路径(如 assets/css/style.css),不要写死以 / 开头的绝对路径。
06复盘清单:做完之后问自己
- 这个页面在手机上打开,阅读与点击是否都舒服?
- 所有图片都有
alt吗?键盘能否走通全部交互? - 刷新页面后,数据是否还在?异常数据会不会导致白屏?
- 有没有可以删掉的代码、依赖或样式?
- 打开 DevTools 的 Lighthouse,性能与无障碍能到多少分?
- 如果把数据换一批、把文案换一遍,布局还稳吗?
最后:作品不是「学完再做」,而是「做着学会」。选一个你真正想做的主题,哪怕很小,把它完整地做完并发布出去,这一步的价值超过再读十篇教程。
如果还想继续深入,回到 进阶篇章 挑选一个方向:Canvas、组件化、PWA 或性能优化,逐个击破。