这个实战项目将带您构建一个现代化的个人作品集/博客网站,我们将遵循最佳实践,使用 语义化HTML5 构建内容结构,用CSS3进行样式美化,并加入一点基础的 JavaScript 来实现交互效果

html5网页前端设计实战源代码
(图片来源网络,侵删)

我们将创建一个包含以下部分的单页网站:

  1. 导航栏:固定在顶部,方便用户快速跳转。
  2. 主页/英雄区:展示网站标题和简介。
  3. 关于我:个人简介部分。
  4. 我的技能:以进度条形式展示技能。
  5. 我的项目:以卡片形式展示项目作品。
  6. 联系我:包含一个简单的联系表单。
  7. 页脚:版权信息。

第一步:项目文件结构

创建一个项目文件夹,并在其中创建以下三个文件:

my-portfolio/
├── index.html
├── style.css
└── script.js

第二步:HTML5 语义化结构 (index.html)

HTML5 的核心是语义化,即使用有意义的标签来描述内容,这有助于搜索引擎优化(SEO)和提升网站的可访问性。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="这是一个使用HTML5, CSS3和JavaScript构建的个人作品集网站。">我的个人作品集</title>
    <!-- 引入外部样式表 -->
    <link rel="stylesheet" href="style.css">
    <!-- 引入Google Fonts字体 -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
</head>
<body>
    <!-- 导航栏 -->
    <header>
        <nav class="navbar">
            <a href="#home" class="nav-logo">我的Logo</a>
            <ul class="nav-menu">
                <li class="nav-item">
                    <a href="#home" class="nav-link">主页</a>
                </li>
                <li class="nav-item">
                    <a href="#about" class="nav-link">关于我</a>
                </li>
                <li class="nav-item">
                    <a href="#skills" class="nav-link">技能</a>
                </li>
                <li class="nav-item">
                    <a href="#projects" class="nav-link">项目</a>
                </li>
                <li class="nav-item">
                    <a href="#contact" class="nav-link">联系我</a>
                </li>
            </ul>
            <div class="hamburger">
                <span class="bar"></span>
                <span class="bar"></span>
                <span class="bar"></span>
            </div>
        </nav>
    </header>
    <main>
        <!-- 主页/英雄区 -->
        <section id="home" class="hero">
            <div class="hero-content">
                <h1>你好,我是 <span class="highlight">张三</span></h1>
                <p>一名充满热情的前端开发工程师</p>
                <a href="#projects" class="cta-button">查看我的作品</a>
            </div>
        </section>
        <!-- 关于我 -->
        <section id="about" class="about">
            <h2 class="section-title">关于我</h2>
            <p>我是一名专注于创建响应式、用户友好型网站的前端开发者,我热爱将复杂的问题转化为优雅、直观的数字体验,我对新技术充满好奇,并致力于编写干净、高效的代码。</p>
        </section>
        <!-- 我的技能 -->
        <section id="skills" class="skills">
            <h2 class="section-title">我的技能</h2>
            <div class="skill-container">
                <div class="skill-item">
                    <span>HTML5 / CSS3</span>
                    <div class="skill-bar">
                        <div class="skill-percentage" data-skill="90"></div>
                    </div>
                </div>
                <div class="skill-item">
                    <span>JavaScript (ES6+)</span>
                    <div class="skill-bar">
                        <div class="skill-percentage" data-skill="85"></div>
                    </div>
                </div>
                <div class="skill-item">
                    <span>React</span>
                    <div class="skill-bar">
                        <div class="skill-percentage" data-skill="75"></div>
                    </div>
                </div>
                <div class="skill-item">
                    <span>响应式设计</span>
                    <div class="skill-bar">
                        <div class="skill-percentage" data-skill="95"></div>
                    </div>
                </div>
            </div>
        </section>
        <!-- 我的项目 -->
        <section id="projects" class="projects">
            <h2 class="section-title">我的项目</h2>
            <div class="project-grid">
                <div class="project-card">
                    <img src="https://via.placeholder.com/400x250.png?text=项目1" alt="项目1截图">
                    <h3>电商网站</h3>
                    <p>一个功能完整的响应式电商网站,包含商品展示、购物车和支付集成。</p>
                    <a href="#" class="project-link">查看详情</a>
                </div>
                <div class="project-card">
                    <img src="https://via.placeholder.com/400x250.png?text=项目2" alt="项目2截图">
                    <h3>任务管理应用</h3>
                    <p>一个基于Vue.js的在线任务管理工具,支持拖拽排序和实时同步。</p>
                    <a href="#" class="project-link">查看详情</a>
                </div>
                <div class="project-card">
                    <img src="https://via.placeholder.com/400x250.png?text=项目3" alt="项目3截图">
                    <h3>个人博客</h3>
                    <p>使用JAMstack架构构建的个人博客,速度快,安全性高。</p>
                    <a href="#" class="project-link">查看详情</a>
                </div>
            </div>
        </section>
        <!-- 联系我 -->
        <section id="contact" class="contact">
            <h2 class="section-title">联系我</h2>
            <form class="contact-form">
                <input type="text" name="name" placeholder="您的名字" required>
                <input type="email" name="email" placeholder="您的邮箱" required>
                <textarea name="message" rows="5" placeholder="请输入您的留言" required></textarea>
                <button type="submit" class="submit-btn">发送消息</button>
            </form>
        </section>
    </main>
    <!-- 页脚 -->
    <footer>
        <p>&copy; 2025 张三. 保留所有权利.</p>
    </footer>
    <!-- 引入外部脚本 -->
    <script src="script.js"></script>
</body>
</html>

第三步:CSS3 样式美化 (style.css)

CSS 负责网站的视觉呈现,我们将使用 Flexbox 和 Grid 进行布局,并添加一些过渡和动画效果,使网站更生动。

html5网页前端设计实战源代码
(图片来源网络,侵删)
/* --- 全局样式和变量 --- */
:root {
    --primary-color: #007bff;
    --secondary-color: #343a40;
    --text-color: #333;
    --light-bg: #f4f4f4;
    --white: #fff;
    --box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: 'Poppins', sans-serif;
    line-height: 1.6;
    color: var(--text-color);
    background-color: var(--white);
}
h1, h2, h3 {
    font-weight: 600;
    line-height: 1.2;
}
.section-title {
    text-align: center;
    font-size: 2.5rem;
    margin-bottom: 3rem;
    color: var(--secondary-color);
}
/* --- 导航栏 --- */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1.5rem 5%;
    background: var(--white);
    box-shadow: var(--box-shadow);
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
    transition: all 0.3s ease;
}
.nav-logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
    text-decoration: none;
}
.nav-menu {
    display: flex;
    list-style: none;
}
.nav-item {
    margin-left: 2rem;
}
.nav-link {
    text-decoration: none;
    color: var(--text-color);
    font-weight: 600;
    transition: color 0.3s ease;
}
.nav-link:hover {
    color: var(--primary-color);
}
.hamburger {
    display: none;
    cursor: pointer;
}
.hamburger .bar {
    display: block;
    width: 25px;
    height: 3px;
    margin: 5px auto;
    background-color: var(--text-color);
    transition: all 0.3s ease-in-out;
}
/* --- 主要内容区 --- */
main {
    margin-top: 80px; /* 为固定导航栏留出空间 */
}
/* --- 主页/英雄区 --- */
.hero {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://images.unsplash.com/photo-1557682257-2f9c37a3a5f3?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1740&q=80') no-repeat center center/cover;
    color: var(--white);
    padding: 0 20px;
}
.hero-content h1 {
    font-size: 3.5rem;
    margin-bottom: 1rem;
}
.highlight {
    color: var(--primary-color);
}
.cta-button {
    display: inline-block;
    padding: 12px 30px;
    background: var(--primary-color);
    color: var(--white);
    border-radius: 5px;
    text-decoration: none;
    font-weight: 600;
    transition: background 0.3s ease;
}
.cta-button:hover {
    background: #0056b3;
}
/* --- 关于我 --- */
.about {
    padding: 100px 10%;
    text-align: center;
    max-width: 800px;
    margin: 0 auto;
}
/* --- 技能 --- */
.skills {
    padding: 100px 10%;
    background-color: var(--light-bg);
}
.skill-container {
    max-width: 800px;
    margin: 0 auto;
}
.skill-item {
    margin-bottom: 2rem;
}
.skill-item span {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
}
.skill-bar {
    height: 10px;
    background-color: #ddd;
    border-radius: 5px;
    overflow: hidden;
}
.skill-percentage {
    height: 100%;
    background-color: var(--primary-color);
    width: 0;
    transition: width 2s ease-in-out;
}
/* --- 项目 --- */
.projects {
    padding: 100px 10%;
}
.project-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
}
.project-card {
    background: var(--white);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: var(--box-shadow);
    transition: transform 0.3s ease;
}
.project-card:hover {
    transform: translateY(-10px);
}
.project-card img {
    width: 100%;
    height: 200px;
    object-fit: cover;
}
.project-card h3 {
    padding: 1.5rem 1.5rem 0.5rem;
}
.project-card p {
    padding: 0 1.5rem;
    color: #666;
}
.project-link {
    display: inline-block;
    padding: 10px 20px;
    margin: 1.5rem;
    color: var(--primary-color);
    text-decoration: none;
    border: 2px solid var(--primary-color);
    border-radius: 5px;
    transition: all 0.3s ease;
}
.project-link:hover {
    background: var(--primary-color);
    color: var(--white);
}
/* --- 联系我 --- */
.contact {
    padding: 100px 10%;
    text-align: center;
}
.contact-form {
    max-width: 600px;
    margin: 0 auto;
}
.contact-form input,
.contact-form textarea {
    width: 100%;
    padding: 15px;
    margin-bottom: 1rem;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-family: inherit;
}
.submit-btn {
    padding: 12px 30px;
    background: var(--primary-color);
    color: var(--white);
    border: none;
    border-radius: 5px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.3s ease;
}
.submit-btn:hover {
    background: #0056b3;
}
/* --- 页脚 --- */
footer {
    background: var(--secondary-color);
    color: var(--white);
    text-align: center;
    padding: 2rem 0;
}
/* --- 响应式设计 --- */
@media (max-width: 768px) {
    .hamburger {
        display: block;
    }
    .nav-menu {
        position: fixed;
        left: -100%;
        top: 70px;
        flex-direction: column;
        background-color: var(--white);
        width: 100%;
        text-align: center;
        transition: 0.3s;
        box-shadow: var(--box-shadow);
        padding: 2rem 0;
    }
    .nav-menu.active {
        left: 0;
    }
    .nav-item {
        margin: 1rem 0;
    }
    .hero-content h1 {
        font-size: 2.5rem;
    }
    .section-title {
        font-size: 2rem;
    }
}

第四步:JavaScript 交互效果 (script.js)

JavaScript 为网站添加动态行为,这里我们实现两个功能:

  1. 移动端菜单切换:点击汉堡菜单图标,显示/隐藏导航菜单。
  2. 技能条动画:当用户滚动到技能区域时,让技能进度条动起来。
document.addEventListener('DOMContentLoaded', () => {
    // --- 移动端菜单切换 ---
    const hamburger = document.querySelector(".hamburger");
    const navMenu = document.querySelector(".nav-menu");
    hamburger.addEventListener("click", () => {
        navMenu.classList.toggle("active");
    });
    // 点击导航链接后关闭菜单
    document.querySelectorAll(".nav-link").forEach(n => n.addEventListener("click", () => {
        navMenu.classList.remove("active");
    }));
    // --- 技能条动画 ---
    const skillBars = document.querySelectorAll('.skill-percentage');
    const skillsSection = document.querySelector('.skills');
    const animationTriggered = false;
    // 使用 Intersection Observer API 来检测元素是否进入视口
    const observerOptions = {
        threshold: 0.7 // 当 70% 的技能区域进入视口时触发
    };
    const animateSkillBars = () => {
        skillBars.forEach(bar => {
            const skill = bar.getAttribute('data-skill');
            bar.style.width = skill + '%';
        });
    };
    const resetSkillBars = () => {
        skillBars.forEach(bar => {
            bar.style.width = '0%';
        });
    };
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                if (!animationTriggered) {
                    animateSkillBars();
                }
            } else {
                // 当用户滚动离开时,可以重置动画,实现重复播放效果
                // resetSkillBars(); 
            }
        });
    }, observerOptions);
    // 开始观察技能区域
    if (skillsSection) {
        observer.observe(skillsSection);
    }
});

如何运行和使用

  1. 保存文件:将上面的代码分别复制并粘贴到 index.html, style.css, 和 script.js 文件中。
  2. 放置图片:为了在项目部分看到图片,你需要替换 https://via.placeholder.com/... 为你自己的图片链接或本地图片路径。
  3. 打开网页:直接用浏览器打开 index.html 文件,你就能看到这个网站了。

如何进一步学习和改进?

这个项目是一个很好的起点,你可以通过以下方式继续提升:

  • 添加更多页面:将单页应用改为多页应用,为每个部分创建独立的 HTML 文件。
  • 深化交互:为联系表单添加真正的提交功能(需要后端支持)。
  • 添加更多动画:使用 CSS 动画或 JavaScript 库(如 AOS - Animate On Scroll)为页面添加更多滚动动画。
  • 使用 CSS 预处理器:学习使用 Sass 或 Less 来更好地管理 CSS 变量和嵌套规则。
  • 构建工具:学习使用 Webpack 或 Vite 等现代前端构建工具来优化和打包你的项目。
  • 部署上线:将你的网站部署到 Netlify、Vercel 或 GitHub Pages 上,让全世界都能看到。

希望这份详细的实战源代码能帮助你更好地理解 HTML5、CSS3 和 JavaScript 的协同工作方式!祝你学习愉快!

html5网页前端设计实战源代码
(图片来源网络,侵删)