1. 响应式设计:在桌面、平板和手机上都有良好的显示效果。
  2. 核心功能模块:顶部导航栏、搜索栏、轮播图、商品分类、商品网格展示、页脚等。
  3. 交互效果:轮播图自动播放、导航栏悬停效果、商品卡片悬停效果、购物车数量更新等。
  4. 清晰的代码结构:使用 HTML5 语义化标签,CSS 采用 BEM 命名规范,便于理解和维护。

如何使用这份源码?

  1. 创建文件:在您的项目文件夹中创建以下三个文件:
    • index.html
    • style.css
    • script.js
  2. 复制代码:将下面提供的代码分别粘贴到对应的文件中。
  3. 预览:用浏览器打开 index.html 文件即可看到效果。

index.html (HTML 结构)

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">潮流数码 - 您的专属数码商城</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- 顶部导航栏 -->
    <header class="main-header">
        <div class="container">
            <div class="header-top">
                <div class="logo">
                    <a href="#"><i class="fas fa-bolt"></i> 潮流数码</a>
                </div>
                <div class="search-bar">
                    <form action="#">
                        <input type="text" placeholder="搜索商品、品牌...">
                        <button type="submit"><i class="fas fa-search"></i></button>
                    </form>
                </div>
                <div class="header-actions">
                    <a href="#" class="action-link"><i class="far fa-user"></i> 登录</a>
                    <a href="#" class="action-link"><i class="far fa-heart"></i> 收藏</a>
                    <a href="#" class="action-link cart-link">
                        <i class="fas fa-shopping-cart"></i>
                        <span class="cart-count">0</span>
                    </a>
                </div>
            </div>
            <nav class="main-nav">
                <ul>
                    <li><a href="#" class="active">首页</a></li>
                    <li><a href="#">手机数码</a></li>
                    <li><a href="#">电脑办公</a></li>
                    <li><a href="#">家用电器</a></li>
                    <li><a href="#">生活百货</a></li>
                    <li><a href="#">限时秒杀</a></li>
                </ul>
            </nav>
        </div>
    </header>
    <main>
        <!-- 轮播图部分 -->
        <section class="hero-slider">
            <div class="slider-container">
                <div class="slide">
                    <img src="https://via.placeholder.com/1200x400.png?text=Banner+1" alt="促销横幅1">
                </div>
                <div class="slide">
                    <img src="https://via.placeholder.com/1200x400.png?text=Banner+2" alt="促销横幅2">
                </div>
                <div class="slide">
                    <img src="https://via.placeholder.com/1200x400.png?text=Banner+3" alt="促销横幅3">
                </div>
                <!-- 轮播控制按钮 -->
                <button class="slider-btn prev-btn"><i class="fas fa-chevron-left"></i></button>
                <button class="slider-btn next-btn"><i class="fas fa-chevron-right"></i></button>
                <!-- 轮播指示器 -->
                <div class="slider-dots"></div>
            </div>
        </section>
        <!-- 商品分类导航 -->
        <section class="category-nav">
            <div class="container">
                <ul>
                    <li><a href="#"><i class="fas fa-mobile-alt"></i> 手机</a></li>
                    <li><a href="#"><i class="fas fa-laptop"></i> 电脑</a></li>
                    <li><a href="#"><i class="fas fa-headphones"></i> 音频</a></li>
                    <li><a href="#"><i class="fas fa-camera"></i> 摄影</a></li>
                    <li><a href="#"><i class="fas fa-gamepad"></i> 游戏</a></li>
                    <li><a href="#"><i class="fas fa-tv"></i> 智能</a></li>
                </ul>
            </div>
        </section>
        <!-- 新品上市 -->
        <section class="featured-products">
            <div class="container">
                <h2 class="section-title">新品上市</h2>
                <div class="product-grid">
                    <!-- 商品卡片 1 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+1" alt="智能手机">
                            <span class="product-tag">新品</span>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">旗舰智能手机 Pro Max</h3>
                            <div class="product-price">
                                <span class="current-price">¥5,999</span>
                                <span class="old-price">¥6,999</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="旗舰智能手机 Pro Max" data-price="5999">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                    <!-- 商品卡片 2 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+2" alt="无线耳机">
                            <span class="product-tag">热卖</span>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">降噪无线耳机</h3>
                            <div class="product-price">
                                <span class="current-price">¥1,299</span>
                                <span class="old-price">¥1,599</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="降噪无线耳机" data-price="1299">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                    <!-- 商品卡片 3 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+3" alt="智能手表">
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">运动智能手表</h3>
                            <div class="product-price">
                                <span class="current-price">¥2,199</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="运动智能手表" data-price="2199">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                    <!-- 商品卡片 4 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+4" alt="平板电脑">
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">轻薄平板电脑</h3>
                            <div class="product-price">
                                <span class="current-price">¥3,499</span>
                                <span class="old-price">¥3,999</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="轻薄平板电脑" data-price="3499">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                    <!-- 商品卡片 5 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+5" alt="笔记本电脑">
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">高性能游戏本</h3>
                            <div class="product-price">
                                <span class="current-price">¥8,999</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="高性能游戏本" data-price="8999">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                    <!-- 商品卡片 6 -->
                    <div class="product-card">
                        <div class="product-img">
                            <img src="https://via.placeholder.com/300x300.png?text=Product+6" alt="相机">
                        </div>
                        <div class="product-info">
                            <h3 class="product-name">微单相机套装</h3>
                            <div class="product-price">
                                <span class="current-price">¥6,799</span>
                                <span class="old-price">¥7,499</span>
                            </div>
                            <button class="add-to-cart-btn" data-name="微单相机套装" data-price="6799">
                                <i class="fas fa-shopping-cart"></i> 加入购物车
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>
    <!-- 页脚 -->
    <footer class="main-footer">
        <div class="container">
            <div class="footer-content">
                <div class="footer-column">
                    <h4>关于我们</h4>
                    <ul>
                        <li><a href="#">公司简介</a></li>
                        <li><a href="#">招贤纳士</a></li>
                        <li><a href="#">联系我们</a></li>
                    </ul>
                </div>
                <div class="footer-column">
                    <h4>客户服务</h4>
                    <ul>
                        <li><a href="#">帮助中心</a></li>
                        <li><a href="#">售后服务</a></li>
                        <li><a href="#">配送说明</a></li>
                    </ul>
                </div>
                <div class="footer-column">
                    <h4>商家合作</h4>
                    <ul>
                        <li><a href="#">入驻条件</a></li>
                        <li><a href="#">商家后台</a></li>
                        <li><a href="#">广告服务</a></li>
                    </ul>
                </div>
                <div class="footer-column">
                    <h4>关注我们</h4>
                    <p>获取最新优惠信息</p>
                    <div class="social-links">
                        <a href="#"><i class="fab fa-weixin"></i></a>
                        <a href="#"><i class="fab fa-weibo"></i></a>
                        <a href="#"><i class="fab fa-tiktok"></i></a>
                    </div>
                </div>
            </div>
            <div class="footer-bottom">
                <p>&copy; 2025 潮流数码. All rights reserved.</p>
            </div>
        </div>
    </footer>
    <!-- 购物车提示弹窗 -->
    <div id="cart-notification" class="cart-notification">
        <p><i class="fas fa-check-circle"></i> <span id="notification-text">已成功加入购物车!</span></p>
    </div>
    <script src="script.js"></script>
</body>
</html>

style.css (CSS 样式)

/* --- 全局样式和变量 --- */
:root {
    --primary-color: #e5484d;
    --secondary-color: #333;
    --text-color: #333;
    --light-gray: #f4f4f4;
    --border-color: #ddd;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    --transition: all 0.3s ease;
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    color: var(--text-color);
    background-color: #fff;
}
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 15px;
}
a {
    text-decoration: none;
    color: inherit;
}
ul {
    list-style: none;
}
img {
    max-width: 100%;
    height: auto;
    display: block;
}
.section-title {
    font-size: 2rem;
    margin-bottom: 2rem;
    text-align: center;
    color: var(--secondary-color);
}
/* --- 顶部导航栏 --- */
.main-header {
    background: #fff;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 1000;
}
.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 0;
}
.logo a {
    font-size: 1.8rem;
    font-weight: bold;
    color: var(--primary-color);
}
.search-bar form {
    display: flex;
    width: 500px;
}
.search-bar input {
    flex: 1;
    padding: 0.8rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 4px 0 0 4px;
    font-size: 1rem;
}
.search-bar button {
    padding: 0 1.5rem;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 0 4px 4px 0;
    cursor: pointer;
    transition: var(--transition);
}
.search-bar button:hover {
    background: #c73538;
}
.header-actions {
    display: flex;
    gap: 1.5rem;
}
.action-link {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    color: var(--secondary-color);
    font-weight: 500;
    transition: var(--transition);
}
.action-link:hover {
    color: var(--primary-color);
}
.cart-link {
    position: relative;
}
.cart-count {
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--primary-color);
    color: white;
    font-size: 0.7rem;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.main-nav ul {
    display: flex;
    justify-content: center;
    gap: 2rem;
    padding: 1rem 0;
    border-top: 1px solid var(--border-color);
}
.main-nav a {
    font-weight: 500;
    color: var(--secondary-color);
    transition: var(--transition);
    padding: 0.5rem 0;
    border-bottom: 2px solid transparent;
}
.main-nav a:hover, .main-nav a.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}
/* --- 轮播图 --- */
.hero-slider {
    position: relative;
    width: 100%;
    height: 400px;
    overflow: hidden;
    margin-bottom: 3rem;
}
.slider-container {
    position: relative;
    width: 100%;
    height: 100%;
}
.slide {
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0;
    transition: opacity 0.5s ease-in-out;
}
.slide.active {
    opacity: 1;
}
.slide img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.slider-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.2rem;
    transition: var(--transition);
    z-index: 10;
}
.slider-btn:hover {
    background: rgba(0, 0, 0, 0.8);
}
.prev-btn { left: 20px; }
.next-btn { right: 20px; }
.slider-dots {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 10px;
    z-index: 10;
}
.dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.5);
    cursor: pointer;
    transition: var(--transition);
}
.dot.active {
    background: white;
}
/* --- 商品分类导航 --- */
.category-nav {
    background: var(--light-gray);
    padding: 1rem 0;
    margin-bottom: 3rem;
}
.category-nav ul {
    display: flex;
    justify-content: space-around;
    text-align: center;
}
.category-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    color: var(--secondary-color);
    font-weight: 500;
    transition: var(--transition);
}
.category-nav a:hover {
    color: var(--primary-color);
}
.category-nav i {
    font-size: 1.5rem;
}
/* --- 商品展示区 --- */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 2rem;
}
.product-card {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    transition: var(--transition);
    box-shadow: var(--shadow);
}
.product-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
}
.product-img {
    position: relative;
    height: 250px;
    overflow: hidden;
}
.product-img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: var(--transition);
}
.product-card:hover .product-img img {
    transform: scale(1.1);
}
.product-tag {
    position: absolute;
    top: 10px;
    left: 10px;
    background: var(--primary-color);
    color: white;
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: bold;
}
.product-info {
    padding: 1.5rem;
}
.product-name {
    font-size: 1.1rem;
    margin-bottom: 0.8rem;
    color: var(--secondary-color);
    height: 2.5em; /* 固定高度防止文字换行导致布局跳动 */
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
.product-price {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}
.current-price {
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--primary-color);
}
.old-price {
    color: #999;
    text-decoration: line-through;
}
.add-to-cart-btn {
    width: 100%;
    padding: 0.8rem;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}
.add-to-cart-btn:hover {
    background: #c73538;
}
/* --- 页脚 --- */
.main-footer {
    background: #333;
    color: #ccc;
    padding: 3rem 0 1rem;
    margin-top: 5rem;
}
.footer-content {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 2rem;
    margin-bottom: 2rem;
}
.footer-column h4 {
    color: white;
    margin-bottom: 1rem;
    font-size: 1.2rem;
}
.footer-column ul li {
    margin-bottom: 0.5rem;
}
.footer-column a {
    color: #ccc;
    transition: var(--transition);
}
.footer-column a:hover {
    color: white;
    padding-left: 5px;
}
.social-links {
    display: flex;
    gap: 1rem;
    margin-top: 1rem;
}
.social-links a {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: #444;
    color: white;
    border-radius: 50%;
    transition: var(--transition);
}
.social-links a:hover {
    background: var(--primary-color);
}
.footer-bottom {
    text-align: center;
    padding-top: 1rem;
    border-top: 1px solid #444;
    font-size: 0.9rem;
}
/* --- 购物车提示 --- */
.cart-notification {
    position: fixed;
    bottom: 30px;
    right: 30px;
    background: #333;
    color: white;
    padding: 1rem 2rem;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s ease;
    z-index: 2000;
}
.cart-notification.show {
    opacity: 1;
    transform: translateY(0);
}
.cart-notification i {
    color: #4caf50;
    font-size: 1.2rem;
}
/* --- 响应式设计 --- */
@media (max-width: 768px) {
    .header-top {
        flex-direction: column;
        gap: 1rem;
    }
    .search-bar form {
        width: 100%;
    }
    .main-nav ul {
        flex-wrap: wrap;
        gap: 1rem;
    }
    .hero-slider {
        height: 250px;
    }
    .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 1rem;
    }
    .footer-content {
        grid-template-columns: 1fr 1fr;
    }
}

script.js (JavaScript 交互)

document.addEventListener('DOMContentLoaded', () => {
    // --- 轮播图功能 ---
    const slides = document.querySelectorAll('.slide');
    const prevBtn = document.querySelector('.prev-btn');
    const nextBtn = document.querySelector('.next-btn');
    const dotsContainer = document.querySelector('.slider-dots');
    let currentSlide = 0;
    const slideInterval = 5000; // 5秒切换一次
    // 创建指示器
    slides.forEach((_, index) => {
        const dot = document.createElement('span');
        dot.classList.add('dot');
        if (index === 0) dot.classList.add('active');
        dot.addEventListener('click', () => goToSlide(index));
        dotsContainer.appendChild(dot);
    });
    const dots = document.querySelectorAll('.dot');
    function showSlide(index) {
        slides.forEach(slide => slide.classList.remove('active'));
        dots.forEach(dot => dot.classList.remove('active'));
        slides[index].classList.add('active');
        dots[index].classList.add('active');
        currentSlide = index;
    }
    function nextSlide() {
        const newIndex = (currentSlide + 1) % slides.length;
        showSlide(newIndex);
    }
    function prevSlide() {
        const newIndex = (currentSlide - 1 + slides.length) % slides.length;
        showSlide(newIndex);
    }
    function goToSlide(index) {
        showSlide(index);
    }
    nextBtn.addEventListener('click', nextSlide);
    prevBtn.addEventListener('click', prevSlide);
    // 自动播放
    let slideShow = setInterval(nextSlide, slideInterval);
    // 鼠标悬停时暂停自动播放
    const heroSlider = document.querySelector('.hero-slider');
    heroSlider.addEventListener('mouseenter', () => clearInterval(slideShow));
    heroSlider.addEventListener('mouseleave', () => {
        slideShow = setInterval(nextSlide, slideInterval);
    });
    // --- 购物车功能 ---
    const cartCountElement = document.querySelector('.cart-count');
    const notification = document.getElementById('cart-notification');
    const notificationText = document.getElementById('notification-text');
    let cartCount = 0;
    const addToCartButtons = document.querySelectorAll('.add-to-cart-btn');
    addToCartButtons.forEach(button => {
        button.addEventListener('click', () => {
            const productName = button.getAttribute('data-name');
            const productPrice = button.getAttribute('data-price');
            // 更新购物车数量
            cartCount++;
            cartCountElement.textContent = cartCount;
            // 显示提示
            notificationText.textContent = `${productName} 已成功加入购物车!`;
            notification.classList.add('show');
            // 3秒后隐藏提示
            setTimeout(() => {
                notification.classList.remove('show');
            }, 3000);
            // 按钮动画反馈
            button.style.backgroundColor = '#4caf50';
            button.innerHTML = '<i class="fas fa-check"></i> 已添加';
            setTimeout(() => {
                button.style.backgroundColor = '';
                button.innerHTML = '<i class="fas fa-shopping-cart"></i> 加入购物车';
            }, 1000);
        });
    });
});

总结与扩展建议

这份模板为您提供了一个非常棒的起点,您可以基于此进行扩展:

网页前端商城页面html模板源码
(图片来源网络,侵删)
  1. 数据源:目前所有数据都是硬编码的,您可以将其与后端 API(如 JSON、RESTful API)结合,使用 JavaScript 动态加载商品数据。
  2. 商品详情页:为每个商品卡片添加链接,跳转到独立的商品详情页。
  3. 购物车页面:创建一个专门的购物车页面,展示已添加的商品,并实现数量修改、删除和结算功能。
  4. 用户系统:完善登录/注册功能,并实现用户中心,包含订单管理、地址管理、收藏夹等。
  5. 更复杂的交互:实现商品筛选、排序、分页加载等功能。
  6. 动画和微交互:使用 CSS 动画或 JavaScript 库(如 GSAP)增加更丰富的动画效果,提升用户体验。

希望这份源码对您有帮助!

网页前端商城页面html模板源码
(图片来源网络,侵删)