<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>炫酷图片放大切换效果</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: #111;
overflow-x: hidden;
}
.gallery-container {
width: 100vw;
height: 100vh;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 15px;
width: 80%;
max-width: 1200px;
}
.gallery-item {
position: relative;
width: 100%;
height: 200px;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
cursor: pointer;
transition: all 0.3s ease;
}
.gallery-item img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s ease;
}
.gallery-item:hover {
transform: translateY(-10px);
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.5);
}
.gallery-item:hover img {
transform: scale(1.1);
}
.gallery-item.active {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 100;
transform: none;
border-radius: 0;
animation: zoomIn 0.5s forwards;
}
.gallery-item.active img {
object-fit: contain;
background: #000;
}
.close-btn {
position: fixed;
top: 30px;
right: 30px;
color: white;
font-size: 30px;
z-index: 101;
cursor: pointer;
opacity: 0;
transition: opacity 0.3s;
}
.gallery-item.active ~ .close-btn {
opacity: 1;
}
.nav-btn {
position: fixed;
top: 50%;
transform: translateY(-50%);
color: white;
font-size: 40px;
z-index: 101;
cursor: pointer;
opacity: 0;
transition: opacity 0.3s;
background: rgba(0, 0, 0, 0.5);
width: 60px;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
}
.prev-btn {
left: 30px;
}
.next-btn {
right: 30px;
}
.gallery-item.active ~ .nav-btn {
opacity: 1;
}
@keyframes zoomIn {
from {
transform: scale(0.5);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
.credit {
position: fixed;
bottom: 20px;
left: 0;
width: 100%;
text-align: center;
color: #666;
font-size: 14px;
z-index: 1;
}
</style>
</head>
<body>
<div class="gallery-container">
<div class="gallery">
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=1" alt="Image 1">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=2" alt="Image 2">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=3" alt="Image 3">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=4" alt="Image 4">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=5" alt="Image 5">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=6" alt="Image 6">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=7" alt="Image 7">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=8" alt="Image 8">
</div>
<div class="gallery-item">
<img src="https://picsum.photos/800/600?random=9" alt="Image 9">
</div>
</div>
<div class="close-btn">×</div>
<div class="nav-btn prev-btn">‹</div>
<div class="nav-btn next-btn">›</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let currentIndex = 0;
const items = $('.gallery-item');
const totalItems = items.length;
// 点击缩略图放大
items.on('click', function() {
currentIndex = $(this).index();
$(this).addClass('active');
$('body').css('overflow', 'hidden');
});
// 关闭大图
$('.close-btn').on('click', function(e) {
e.stopPropagation();
items.removeClass('active');
$('body').css('overflow', 'auto');
});
// 上一张
$('.prev-btn').on('click', function(e) {
e.stopPropagation();
items.removeClass('active');
currentIndex = (currentIndex - 1 + totalItems) % totalItems;
items.eq(currentIndex).addClass('active');
});
// 下一张
$('.next-btn').on('click', function(e) {
e.stopPropagation();
items.removeClass('active');
currentIndex = (currentIndex + 1) % totalItems;
items.eq(currentIndex).addClass('active');
});
// 键盘导航
$(document).on('keydown', function(e) {
if ($('.gallery-item.active').length) {
switch(e.keyCode) {
case 27: // ESC
$('.close-btn').click();
break;
case 37: // 左箭头
$('.prev-btn').click();
break;
case 39: // 右箭头
$('.next-btn').click();
break;
}
}
});
// 点击大图区域关闭
$(document).on('click', function(e) {
if ($(e.target).hasClass('gallery-item active')) {
$('.close-btn').click();
}
});
});
</script>
</body>
</html>