你想在 HTML+CSS+少量 JS 的基础上快速复刻一个类似 淘宝首页 的静态页面。这里给你一个 10秒级效果示例(简化版),重点展示:
- 顶部搜索栏
- 轮播图
- 分类导航
- 商品列表
纯 前端静态 HTML+CSS+少量 JS,可以直接在 H5 浏览器打开。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>仿淘宝首页 Demo</title>
<style>
body { margin:0; font-family: Arial, sans-serif; }
header { background: #ff0036; color:#fff; padding:10px; display:flex; align-items:center; }
header input { flex:1; padding:5px; border:none; border-radius:4px; margin-left:10px; }
nav { display:flex; justify-content:space-around; background:#fff; padding:10px 0; border-bottom:1px solid #ddd; }
nav div { text-align:center; }
nav img { width:40px; height:40px; display:block; margin:0 auto 5px; }
.carousel { width:100%; height:180px; overflow:hidden; position:relative; }
.carousel img { width:100%; height:100%; position:absolute; top:0; left:100%; transition:0.5s; }
.carousel img.active { left:0; }
.goods { display:flex; flex-wrap:wrap; padding:10px; background:#f5f5f5; }
.goods div { width:48%; margin:1%; background:#fff; padding:5px; box-sizing:border-box; }
.goods img { width:100%; }
.goods p { margin:5px 0; font-size:14px; }
</style>
</head>
<body>
<header>
<h2>淘宝</h2>
<input type="text" placeholder="搜索商品">
</header>
<nav>
<div><img src="https://img.icons8.com/color/48/000000/t-shirt.png"/><span>服饰</span></div>
<div><img src="https://img.icons8.com/color/48/000000/laptop.png"/><span>数码</span></div>
<div><img src="https://img.icons8.com/color/48/000000/home.png"/><span>家居</span></div>
<div><img src="https://img.icons8.com/color/48/000000/gift.png"/><span>礼物</span></div>
</nav>
<div class="carousel">
<img src="https://via.placeholder.com/400x180/ff0036/fff?text=轮播1" class="active">
<img src="https://via.placeholder.com/400x180/ff7a45/fff?text=轮播2">
<img src="https://via.placeholder.com/400x180/ffd666/fff?text=轮播3">
</div>
<div class="goods">
<div>
<img src="https://via.placeholder.com/200x200/73d13d/fff?text=商品1">
<p>商品名称1</p>
<p>¥99</p>
</div>
<div>
<img src="https://via.placeholder.com/200x200/40a9ff/fff?text=商品2">
<p>商品名称2</p>
<p>¥199</p>
</div>
<div>
<img src="https://via.placeholder.com/200x200/f759ab/fff?text=商品3">
<p>商品名称3</p>
<p>¥299</p>
</div>
<div>
<img src="https://via.placeholder.com/200x200/9254de/fff?text=商品4">
<p>商品名称4</p>
<p>¥399</p>
</div>
</div>
<script>
// 简单轮播
const imgs = document.querySelectorAll('.carousel img');
let index = 0;
setInterval(() => {
imgs[index].classList.remove('active');
index = (index + 1) % imgs.length;
imgs[index].classList.add('active');
}, 3000);
</script>
</body>
</html>
🔹 特性说明
- 顶部搜索栏
- 红色背景 + 搜索输入框
- 轮播图
- 使用绝对定位 +
transition实现简单切换 - 自动轮播,每 3 秒切换
- 使用绝对定位 +
- 分类导航
- 4 个常见分类
- 图片 + 文本
- 商品列表
- 两列布局 + margin
- 商品图片 + 名称 + 价格
🔹 小技巧
- 图片可换成真实商品图或 CDN
- 轮播可升级成左右滑动手势支持移动端
- 商品列表可改成 水平滚动,类似淘宝 APP 首页效果
明白了,阿杰!下面给你一个 升级版淘宝首页 H5 模板,特点:
- 响应式布局(适配手机)
- 轮播图支持自动播放 + 手势滑动 + 指示器
- 商品列表动态加载数据
- 顶部搜索框带简单联想提示
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>升级版淘宝首页 H5 模板</title>
<style>
body { margin:0; font-family: Arial, sans-serif; background:#f5f5f5; }
header { background: #ff0036; color:#fff; padding:10px; display:flex; align-items:center; position:sticky; top:0; z-index:10; }
header h2 { margin:0; }
header input { flex:1; margin-left:10px; padding:5px; border:none; border-radius:4px; }
nav { display:flex; justify-content:space-around; background:#fff; padding:10px 0; border-bottom:1px solid #ddd; position:sticky; top:50px; z-index:9; }
nav div { text-align:center; }
nav img { width:40px; height:40px; display:block; margin:0 auto 5px; }
.carousel { width:100%; height:180px; overflow:hidden; position:relative; }
.carousel img { width:100%; height:100%; position:absolute; top:0; left:100%; transition:0.5s; }
.carousel img.active { left:0; }
.carousel .dots { position:absolute; bottom:10px; left:50%; transform:translateX(-50%); display:flex; }
.carousel .dots div { width:8px; height:8px; border-radius:50%; background:#fff; margin:0 4px; opacity:0.6; }
.carousel .dots div.active { opacity:1; background:#ff0036; }
.goods { display:flex; flex-wrap:wrap; padding:10px; }
.goods div { width:48%; margin:1%; background:#fff; padding:5px; box-sizing:border-box; border-radius:4px; }
.goods img { width:100%; border-radius:4px; }
.goods p { margin:5px 0; font-size:14px; }
.suggestions { position:absolute; top:50px; left:10px; right:10px; background:#fff; border:1px solid #ccc; border-radius:4px; display:none; z-index:20; }
.suggestions div { padding:5px; border-bottom:1px solid #eee; cursor:pointer; }
.suggestions div:last-child { border-bottom:none; }
.suggestions div:hover { background:#f0f0f0; }
</style>
</head>
<body>
<header>
<h2>淘宝</h2>
<div style="position:relative; flex:1;">
<input type="text" id="searchInput" placeholder="搜索商品">
<div class="suggestions" id="suggestions"></div>
</div>
</header>
<nav>
<div><img src="https://img.icons8.com/color/48/000000/t-shirt.png"/><span>服饰</span></div>
<div><img src="https://img.icons8.com/color/48/000000/laptop.png"/><span>数码</span></div>
<div><img src="https://img.icons8.com/color/48/000000/home.png"/><span>家居</span></div>
<div><img src="https://img.icons8.com/color/48/000000/gift.png"/><span>礼物</span></div>
</nav>
<div class="carousel" id="carousel">
<img src="https://via.placeholder.com/400x180/ff0036/fff?text=轮播1" class="active">
<img src="https://via.placeholder.com/400x180/ff7a45/fff?text=轮播2">
<img src="https://via.placeholder.com/400x180/ffd666/fff?text=轮播3">
<div class="dots" id="dots"></div>
</div>
<div class="goods" id="goods"></div>
<script>
// ------------------- 轮播图 -------------------
const carousel = document.getElementById('carousel');
const imgs = carousel.querySelectorAll('img');
const dotsContainer = document.getElementById('dots');
let index = 0;
let touchStartX = 0;
let touchEndX = 0;
// 初始化圆点
imgs.forEach((img, i)=>{
const dot = document.createElement('div');
if(i===0) dot.classList.add('active');
dot.addEventListener('click', ()=>showSlide(i));
dotsContainer.appendChild(dot);
});
function showSlide(i){
imgs[index].classList.remove('active');
dotsContainer.children[index].classList.remove('active');
index = i;
imgs[index].classList.add('active');
dotsContainer.children[index].classList.add('active');
}
// 自动轮播
setInterval(()=>{ showSlide((index+1)%imgs.length); }, 3000);
// 手势滑动
carousel.addEventListener('touchstart', e=>{ touchStartX = e.touches[0].clientX; });
carousel.addEventListener('touchend', e=>{
touchEndX = e.changedTouches[0].clientX;
if(touchEndX - touchStartX > 50) showSlide((index-1+imgs.length)%imgs.length);
else if(touchStartX - touchEndX > 50) showSlide((index+1)%imgs.length);
});
// ------------------- 商品列表 -------------------
const goodsData = [
{name:'商品1', price:'¥99', img:'https://via.placeholder.com/200x200/73d13d/fff?text=商品1'},
{name:'商品2', price:'¥199', img:'https://via.placeholder.com/200x200/40a9ff/fff?text=商品2'},
{name:'商品3', price:'¥299', img:'https://via.placeholder.com/200x200/f759ab/fff?text=商品3'},
{name:'商品4', price:'¥399', img:'https://via.placeholder.com/200x200/9254de/fff?text=商品4'},
{name:'商品5', price:'¥499', img:'https://via.placeholder.com/200x200/ff85c0/fff?text=商品5'},
{name:'商品6', price:'¥599', img:'https://via.placeholder.com/200x200/ffc069/fff?text=商品6'}
];
const goodsContainer = document.getElementById('goods');
goodsData.forEach(item=>{
const div = document.createElement('div');
div.innerHTML = `<img src="${item.img}"><p>${item.name}</p><p>${item.price}</p>`;
goodsContainer.appendChild(div);
});
// ------------------- 搜索联想 -------------------
const searchInput = document.getElementById('searchInput');
const suggestions = document.getElementById('suggestions');
const suggestList = ['服装','数码','家居','礼物','鞋子','手表','手机','电脑'];
searchInput.addEventListener('input', ()=>{
const val = searchInput.value.trim();
suggestions.innerHTML = '';
if(val===''){ suggestions.style.display='none'; return; }
const filtered = suggestList.filter(item=>item.includes(val));
filtered.forEach(item=>{
const div = document.createElement('div');
div.textContent = item;
div.addEventListener('click', ()=>{
searchInput.value = item;
suggestions.style.display='none';
});
suggestions.appendChild(div);
});
suggestions.style.display = filtered.length ? 'block' : 'none';
});
document.addEventListener('click', e=>{
if(!searchInput.contains(e.target)) suggestions.style.display='none';
});
</script>
</body>
</html>
🔹 升级版特性
- 响应式布局:适配手机屏幕,顶部导航固定
- 轮播图:
- 自动播放
- 手势滑动支持移动端
- 下方指示点显示当前页
- 商品列表动态生成:可替换成真实商品数据
- 搜索框联想:输入关键字显示匹配结果,点击可选中
发表回复