<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery滑块画廊特效</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: #f5f5f5;
padding: 20px;
}
.gallery-container {
max-width: 1000px;
margin: 0 auto;
position: relative;
overflow: hidden;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
border-radius: 10px;
}
.gallery-slider {
display: flex;
transition: transform 0.5s ease;
height: 500px;
}
.slide {
min-width: 100%;
position: relative;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.slide-caption {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,0.7);
color: white;
padding: 20px;
text-align: center;
}
.slider-controls {
position: absolute;
top: 50%;
width: 100%;
display: flex;
justify-content: space-between;
transform: translateY(-50%);
}
.control-btn {
background: rgba(255,255,255,0.5);
border: none;
color: #333;
font-size: 24px;
padding: 10px 15px;
cursor: pointer;
transition: all 0.3s;
}
.control-btn:hover {
background: rgba(255,255,255,0.8);
}
.slider-dots {
position: absolute;
bottom: 20px;
left: 0;
right: 0;
display: flex;
justify-content: center;
gap: 10px;
}
.dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: rgba(255,255,255,0.5);
cursor: pointer;
transition: all 0.3s;
}
.dot.active {
background: white;
transform: scale(1.2);
}
.credit {
text-align: center;
margin-top: 20px;
color: #666;
}
.credit a {
color: #0066cc;
text-decoration: none;
}
</style>
</head>
<body>
<div class="gallery-container">
<div class="gallery-slider">
<div class="slide">
<img src="https://picsum.photos/1000/500?random=1" alt="图片1">
<div class="slide-caption">
<h3>美丽的风景1</h3>
<p>这是第一张展示图片的描述文字</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=2" alt="图片2">
<div class="slide-caption">
<h3>壮丽的山脉</h3>
<p>这是第二张展示图片的描述文字</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=3" alt="图片3">
<div class="slide-caption">
<h3>宁静的湖泊</h3>
<p>这是第三张展示图片的描述文字</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=4" alt="图片4">
<div class="slide-caption">
<h3>日落美景</h3>
<p>这是第四张展示图片的描述文字</p>
</div>
</div>
</div>
<div class="slider-controls">
<button class="control-btn prev-btn">❮</button>
<button class="control-btn next-btn">❯</button>
</div>
<div class="slider-dots">
<div class="dot active" data-index="0"></div>
<div class="dot" data-index="1"></div>
<div class="dot" data-index="2"></div>
<div class="dot" data-index="3"></div>
</div>
</div>
<div class="credit">
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const $slider = $('.gallery-slider');
const $slides = $('.slide');
const $dots = $('.dot');
const slideCount = $slides.length;
let currentIndex = 0;
// 设置初始位置
function updateSlider() {
$slider.css('transform', `translateX(-${currentIndex * 100}%)`);
$dots.removeClass('active');
$dots.eq(currentIndex).addClass('active');
}
// 下一张
$('.next-btn').click(function() {
currentIndex = (currentIndex + 1) % slideCount;
updateSlider();
});
// 上一张
$('.prev-btn').click(function() {
currentIndex = (currentIndex - 1 + slideCount) % slideCount;
updateSlider();
});
// 点击圆点跳转
$dots.click(function() {
currentIndex = $(this).data('index');
updateSlider();
});
// 自动轮播
let autoSlide = setInterval(function() {
currentIndex = (currentIndex + 1) % slideCount;
updateSlider();
}, 3000);
// 鼠标悬停时暂停自动轮播
$('.gallery-container').hover(
function() {
clearInterval(autoSlide);
},
function() {
autoSlide = setInterval(function() {
currentIndex = (currentIndex + 1) % slideCount;
updateSlider();
}, 3000);
}
);
});
</script>
</body>
</html>
566

被折叠的 条评论
为什么被折叠?



