<!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;
}
.slider-container {
position: relative;
width: 100%;
max-width: 1200px;
height: 500px;
margin: 20px auto;
overflow: hidden;
}
.slider {
position: relative;
width: 100%;
height: 100%;
}
.slide {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
background-size: cover;
background-position: center;
}
.slide.active {
opacity: 1;
}
.slide-content {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 30px;
background: rgba(0, 0, 0, 0.5);
color: white;
}
.slide-content h2 {
margin-bottom: 10px;
}
.slider-nav {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
z-index: 10;
}
.slider-dot {
width: 12px;
height: 12px;
margin: 0 5px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.5);
cursor: pointer;
}
.slider-dot.active {
background: white;
}
.slider-arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 40px;
height: 40px;
background: rgba(0, 0, 0, 0.5);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 10;
}
.slider-arrow.prev {
left: 20px;
}
.slider-arrow.next {
right: 20px;
}
.credit {
text-align: center;
margin: 20px 0;
font-size: 12px;
color: #666;
}
.credit a {
color: #0066cc;
text-decoration: none;
}
</style>
</head>
<body>
<div class="slider-container">
<div class="slider">
<div class="slide active" style="background-image: url('https://picsum.photos/1200/500?random=1')">
<div class="slide-content">
<h2>第一张幻灯片标题</h2>
<p>这里是第一张幻灯片的描述内容。</p>
</div>
</div>
<div class="slide" style="background-image: url('https://picsum.photos/1200/500?random=2')">
<div class="slide-content">
<h2>第二张幻灯片标题</h2>
<p>这里是第二张幻灯片的描述内容。</p>
</div>
</div>
<div class="slide" style="background-image: url('https://picsum.photos/1200/500?random=3')">
<div class="slide-content">
<h2>第三张幻灯片标题</h2>
<p>这里是第三张幻灯片的描述内容。</p>
</div>
</div>
<div class="slide" style="background-image: url('https://picsum.photos/1200/500?random=4')">
<div class="slide-content">
<h2>第四张幻灯片标题</h2>
<p>这里是第四张幻灯片的描述内容。</p>
</div>
</div>
</div>
<div class="slider-arrow prev">❮</div>
<div class="slider-arrow next">❯</div>
<div class="slider-nav">
<div class="slider-dot active" data-index="0"></div>
<div class="slider-dot" data-index="1"></div>
<div class="slider-dot" data-index="2"></div>
<div class="slider-dot" data-index="3"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const slides = $('.slide');
const dots = $('.slider-dot');
const prevBtn = $('.prev');
const nextBtn = $('.next');
let currentIndex = 0;
let interval;
// 初始化轮播
function initSlider() {
updateSlider();
startAutoPlay();
}
// 更新幻灯片显示
function updateSlider() {
slides.removeClass('active');
$(slides[currentIndex]).addClass('active');
dots.removeClass('active');
$(dots[currentIndex]).addClass('active');
}
// 下一张幻灯片
function nextSlide() {
currentIndex = (currentIndex + 1) % slides.length;
updateSlider();
}
// 上一张幻灯片
function prevSlide() {
currentIndex = (currentIndex - 1 + slides.length) % slides.length;
updateSlider();
}
// 开始自动播放
function startAutoPlay() {
clearInterval(interval);
interval = setInterval(nextSlide, 3000);
}
// 点击导航点切换
dots.on('click', function() {
currentIndex = $(this).data('index');
updateSlider();
startAutoPlay();
});
// 点击上一张/下一张按钮
prevBtn.on('click', function() {
prevSlide();
startAutoPlay();
});
nextBtn.on('click', function() {
nextSlide();
startAutoPlay();
});
// 鼠标悬停暂停轮播
$('.slider-container').hover(
function() {
clearInterval(interval);
},
function() {
startAutoPlay();
}
);
// 初始化
initSlider();
});
</script>
</body>
</html>