CSS Grid布局实战:构建响应式产品展示页面 | 前端开发教程

2025-09-26 0 556

产品1

智能手表

多功能智能手表,支持健康监测和通知提醒。

¥899

产品2

无线耳机

高保真音质,降噪功能,持久续航。

¥599

产品3

便携充电宝

20000mAh大容量,支持快充技术。

¥199

产品4

平板电脑

高性能处理器,高清显示屏。

¥2499

产品5

智能音箱

语音助手,智能家居控制中心。

¥399

产品6

运动相机

4K高清拍摄,防水防震设计。

¥1299

// 添加简单的交互效果
document.addEventListener(‘DOMContentLoaded’, function() {
const addToCartButtons = document.querySelectorAll(‘.add-to-cart’);

addToCartButtons.forEach(button => {
button.addEventListener(‘click’, function() {
const productName = this.closest(‘.product-card’).querySelector(‘h2’).textContent;
alert(`已将 ${productName} 添加到购物车`);

// 添加简单的动画效果
this.style.transform = ‘scale(0.9)’;
setTimeout(() => {
this.style.transform = ‘scale(1)’;
}, 150);
});
});
});

/* 基础样式重置 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: ‘Segoe UI’, Tahoma, Geneva, Verdana, sans-serif;
}

body {
background-color: #f5f7fa;
color: #333;
line-height: 1.6;
padding: 20px;
}

/* 页面头部样式 */
.page-header {
text-align: center;
margin-bottom: 40px;
padding: 30px 0;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.page-header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}

.page-header p {
font-size: 1.2rem;
opacity: 0.9;
}

/* CSS Grid布局容器 */
.product-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 30px;
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}

/* 产品卡片样式 */
.product-card {
background: white;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
transition: transform 0.3s ease, box-shadow 0.3s ease;
display: flex;
flex-direction: column;
}

.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

.product-image {
height: 200px;
overflow: hidden;
}

.product-image img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s ease;
}

.product-card:hover .product-image img {
transform: scale(1.05);
}

.product-info {
padding: 20px;
flex-grow: 1;
display: flex;
flex-direction: column;
}

.product-info h2 {
font-size: 1.4rem;
margin-bottom: 10px;
color: #2c3e50;
}

.product-description {
color: #7f8c8d;
margin-bottom: 15px;
flex-grow: 1;
}

.product-price {
font-size: 1.5rem;
font-weight: bold;
color: #e74c3c;
margin-bottom: 15px;
}

.add-to-cart {
background: #3498db;
color: white;
border: none;
padding: 12px 20px;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
font-weight: 600;
transition: background 0.3s ease, transform 0.2s ease;
}

.add-to-cart:hover {
background: #2980b9;
}

/* 页脚样式 */
.page-footer {
text-align: center;
margin-top: 50px;
padding: 20px;
color: #7f8c8d;
font-size: 0.9rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
.product-container {
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}

.page-header h1 {
font-size: 2rem;
}

.page-header p {
font-size: 1rem;
}
}

@media (max-width: 480px) {
.product-container {
grid-template-columns: 1fr;
}

body {
padding: 10px;
}
}

CSS Grid布局实战:构建响应式产品展示页面 | 前端开发教程
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

淘吗网 css CSS Grid布局实战:构建响应式产品展示页面 | 前端开发教程 https://www.taomawang.com/web/css/1120.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务