css制作相册
使用CSS Grid布局创建相册
通过CSS Grid可以轻松实现响应式相册布局。定义一个容器为grid布局,设置行列间距和自适应列数。
.photo-gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-gap: 15px;
padding: 20px;
}
.photo-item {
position: relative;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
aspect-ratio: 1;
}
.photo-item img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.3s ease;
}
.photo-item:hover img {
transform: scale(1.05);
}
使用Flexbox实现瀑布流效果
Flexbox结合column布局可以创建瀑布流相册,特别适合高度不一的图片展示。
.masonry-gallery {
display: flex;
flex-direction: column;
flex-wrap: wrap;
max-height: 1200px;
}
.masonry-item {
width: 32%;
margin-bottom: 15px;
break-inside: avoid;
}
@media (max-width: 768px) {
.masonry-item {
width: 48%;
}
}
添加悬停效果增强交互
为相册图片添加悬停效果可以提升用户体验,显示标题或叠加层。
.photo-overlay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,0.7);
color: white;
padding: 15px;
transform: translateY(100%);
transition: transform 0.3s ease;
}
.photo-item:hover .photo-overlay {
transform: translateY(0);
}
实现图片滤镜效果
CSS滤镜可以为相册图片添加统一的视觉效果,保持风格一致性。
.photo-item img {
filter: brightness(0.95) contrast(1.05);
transition: filter 0.3s ease;
}
.photo-item:hover img {
filter: brightness(1) contrast(1.1);
}
响应式设计调整
确保相册在不同设备上都能良好显示,通过媒体查询调整布局。
@media (max-width: 600px) {
.photo-gallery {
grid-template-columns: repeat(2, 1fr);
grid-gap: 10px;
}
.masonry-gallery {
max-height: none;
flex-direction: row;
}
.masonry-item {
width: 100%;
}
}
添加加载动画
在图片加载时显示占位符或加载动画,提升用户体验。
.photo-item {
background: #f5f5f5;
position: relative;
}
.loading-spinner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border: 4px solid rgba(0,0,0,0.1);
border-radius: 50%;
border-top-color: #3498db;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: translate(-50%, -50%) rotate(360deg); }
}






