在本地创建一个名为“taobao-landing-page”的文件夹,并建立以下目录结构:
在css目录下创建style.css文件,在js目录下创建main.js文件。
打开index.html文件,输入以下完整代码:
```在main标签内添加商品展示区域:
```
在商品展示区下方添加详情区域:
```| 参数名称 | 参数值 |
|---|---|
| 材质 | 纯棉 |
在style.css文件中添加以下基础样式:
``` / 基础重置 / { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: 333; } / 商品展示区响应式布局 / .product-showcase { display: flex; flex-wrap: wrap; gap: 2rem; padding: 1rem; max-width: 1200px; margin: 0 auto; } .product-gallery { flex: 1; min-width: 300px; } .product-info { flex: 1; min-width: 300px; } / 移动端适配 / @media (max-width: 768px) { .product-showcase { flex-direction: column; } .product-gallery, .product-info { width: 100%; } } ```添加图片优化样式:
``` / 图片基础样式 / img { max-width: 100%; height: auto; display: block; } .main-image img { width: 100%; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); } .thumbnails { display: flex; gap: 0.5rem; margin-top: 1rem; } .thumbnails img { width: 80px; height: 80px; object-fit: cover; border: 2px solid transparent; border-radius: 4px; cursor: pointer; transition: border-color 0.3s; } .thumbnails img:hover, .thumbnails img.active { border-color: ff4400; } / 懒加载占位符 / img[loading="lazy"] { background-color: f5f5f5; } ```添加按钮样式增强用户体验:
``` / 按钮基础样式 / button { border: none; padding: 12px 24px; border-radius: 4px; font-size: 16px; cursor: pointer; transition: all 0.3s ease; } .buy-now { background-color: ff4400; color: white; font-weight: bold; width: 100%; margin-bottom: 1rem; } .buy-now:hover { background-color: e63c00; transform: translateY(-2px); box-shadow: 0 4px 12px rgba(255, 68, 0, 0.3); } .add-cart { background-color: fff; color: ff4400; border: 2px solid ff4400; width: 100%; } .add-cart:hover { background-color: fff5f2; } .sku-option { background-color: f5f5f5; margin-right: 0.5rem; margin-bottom: 0.5rem; padding: 8px 16px; } .sku-option.active { background-color: ff4400; color: white; } ```在main.js文件中添加图片切换逻辑:
``` // 缩略图切换功能 document.addEventListener('DOMContentLoaded', function() { const mainImage = document.querySelector('.main-image img'); const thumbnails = document.querySelectorAll('.thumbnails img'); thumbnails.forEach(thumb => { thumb.addEventListener('click', function() { // 更新主图 const newSrc = this.src.replace('thumb', 'product-main'); mainImage.src = newSrc; mainImage.alt = this.alt; // 更新激活状态 thumbnails.forEach(t => t.classList.remove('active')); this.classList.add('active'); }); }); }); ```添加规格选择功能:
``` // SKU选择功能 const skuOptions = document.querySelectorAll('.sku-option'); let selectedSku = null; skuOptions.forEach(option => { option.addEventListener('click', function() { // 移除其他选项的激活状态 skuOptions.forEach(opt => opt.classList.remove('active')); // 设置当前选项为激活状态 this.classList.add('active'); selectedSku = this.dataset.sku; // 这里可以添加价格更新逻辑 updatePrice(selectedSku); }); }); function updatePrice(sku) { // 根据SKU更新价格 const priceMap = { 'sku1': '299.00', 'sku2': '349.00' }; if (priceMap[sku]) { document.querySelector('.current-price').textContent = `¥${priceMap[sku]}`; } } ```实现详情页标签切换功能:
``` // 详情页导航 const detailNavLinks = document.querySelectorAll('.detail-nav a'); const detailSections = document.querySelectorAll('.detail-section'); detailNavLinks.forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); // 更新导航状态 detailNavLinks.forEach(l => l.classList.remove('active')); this.classList.add('active'); // 显示对应内容区域 const targetId = this.getAttribute('href').substring(1); detailSections.forEach(section => { if (section.id === targetId) { section.style.display = 'block'; } else { section.style.display = 'none'; } }); }); }); ```使用以下命令通过命令行工具压缩图片:
``` 安装ImageMagick(macOS) brew install imagemagick 压缩JPEG图片到80%质量 convert input.jpg -quality 80 output.jpg 转换为WebP格式(需要安装webp工具) cwebp -q 80 input.jpg -o output.webp ```创建package.json并安装必要依赖:
``` { "name": "taobao-landing-page", "version": "1.0.0", "scripts": { "build": "npm run build:css && npm run build:js", "build:css": "postcss css/style.css -o dist/css/style.min.css", "build:js": "terser js/main.js -o dist/js/main.min.js" }, "devDependencies": { "autoprefixer": "^10.4.0", "postcss": "^8.4.0", "postcss-cli": "^9.0.0", "terser": "^5.10.0" } } ```创建postcss.config.js配置文件:
``` module.exports = { plugins: [ require('autoprefixer')({ overrideBrowserslist: ['last 2 versions'] }) ] } ```在HTML头部添加关键性能优化标签:
``` ```使用以下命令启动本地服务器:
``` 使用Python启动简单HTTP服务器 python3 -m http.server 8000 或使用Node.js的http-server npx http-server -p 8000 ```访问 http://localhost:8000 测试页面功能。
在Chrome浏览器中:
创建.htaccess文件(Apache服务器):
``` 启用Gzip压缩对于Nginx服务器,在配置文件中添加:
``` location ~ \.(jpg|jpeg|png|gif|webp)$ { expires 1y; add_header Cache-Control "public, immutable"; } location ~ \.(css|js)$ { expires 1M; add_header Cache-Control "public"; } ```完成以上所有步骤后,你的淘宝落地页已经具备完整的展示功能、良好的用户体验和优秀的性能表现。将构建后的文件上传到服务器即可投入使用。
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图