<!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;
overflow: hidden;
}
.slider-container {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
.slider {
display: flex;
width: 100%;
height: 100%;
transition: transform 0.5s ease-in-out;
}
.slide {
min-width: 100%;
height: 100%;
position: relative;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.slide-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-align: center;
z-index: 10;
}
.slide-content h2 {
font-size: 3rem;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
.slide-content p {
font-size: 1.5rem;
margin-bottom: 30px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}
.btn {
display: inline-block;
padding: 10px 30px;
background: #3498db;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
}
.btn:hover {
background: #2980b9;
}
.controls {
position: absolute;
top: 50%;
width: 100%;
display: flex;
justify-content: space-between;
z-index: 10;
}
.controls button {
background: rgba(0, 0, 0, 0.5);
color: white;
border: none;
padding: 15px;
cursor: pointer;
font-size: 1.5rem;
transition: background 0.3s;
}
.controls button:hover {
background: rgba(0, 0, 0, 0.8);
}
.dots {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
z-index: 10;
}
.dot {
width: 15px;
height: 15px;
margin: 0 5px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.5);
cursor: pointer;
transition: background 0.3s;
}
.dot.active {
background: white;
}
/* 您要求的网址样式 */
.website-link {
position: absolute;
bottom: 10px;
right: 10px;
color: white;
z-index: 100;
text-decoration: none;
font-size: 14px;
background: rgba(0, 0, 0, 0.5);
padding: 5px 10px;
border-radius: 3px;
}
.website-link:hover {
background: rgba(0, 0, 0, 0.8);
}
</style>
</head>
<body>
<div class="slider-container">
<div class="slider">
<div class="slide">
<img src="https://picsum.photos/1920/1080?random=1" alt="Slide 1">
<div class="slide-content">
<h2>欢迎来到我们的网站</h2>
<p>发现更多精彩内容</p>
<a href="#" class="btn">了解更多</a>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1920/1080?random=2" alt="Slide 2">
<div class="slide-content">
<h2>优质产品展示</h2>
<p>为您提供最好的选择</p>
<a href="#" class="btn">查看产品</a>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1920/1080?random=3" alt="Slide 3">
<div class="slide-content">
<h2>联系我们</h2>
<p>随时为您服务</p>
<a href="#" class="btn">联系方式</a>
</div>
</div>
</div>
<div class="controls">
<button id="prevBtn">❮</button>
<button id="nextBtn">❯</button>
</div>
<div class="dots">
<div class="dot active"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const slider = $('.slider');
const slides = $('.slide');
const dots = $('.dot');
const prevBtn = $('#prevBtn');
const nextBtn = $('#nextBtn');
let currentSlide = 0;
const slideCount = slides.length;
// 初始化轮播
function initSlider() {
slides.each(function(index) {
$(this).css('transform', `translateX(${index * 100}%)`);
});
}
initSlider();
// 转到指定幻灯片
function goToSlide(slideIndex) {
slider.css('transform', `translateX(-${slideIndex * 100}%)`);
currentSlide = slideIndex;
updateDots();
}
// 更新指示点状态
function updateDots() {
dots.removeClass('active');
dots.eq(currentSlide).addClass('active');
}
// 下一张幻灯片
function nextSlide() {
if (currentSlide === slideCount - 1) {
goToSlide(0);
} else {
goToSlide(currentSlide + 1);
}
}
// 上一张幻灯片
function prevSlide() {
if (currentSlide === 0) {
goToSlide(slideCount - 1);
} else {
goToSlide(currentSlide - 1);
}
}
// 自动轮播
let autoSlide = setInterval(nextSlide, 5000);
// 鼠标悬停时暂停自动轮播
$('.slider-container').hover(
function() {
clearInterval(autoSlide);
},
function() {
autoSlide = setInterval(nextSlide, 5000);
}
);
// 按钮点击事件
nextBtn.click(nextSlide);
prevBtn.click(prevSlide);
// 指示点点击事件
dots.click(function() {
const slideIndex = $(this).index();
goToSlide(slideIndex);
});
// 键盘导航
$(document).keydown(function(e) {
if (e.key === 'ArrowLeft') {
prevSlide();
} else if (e.key === 'ArrowRight') {
nextSlide();
}
});
});
</script>
</body>
</html>
7018

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



