<!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: 'Microsoft YaHei', Arial, sans-serif;
background: #f5f5f5;
color: #333;
line-height: 1.6;
}
.slider-container {
max-width: 1000px;
margin: 50px auto;
position: relative;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
border-radius: 10px;
}
.slider {
display: flex;
transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
height: 500px;
}
.slide {
min-width: 100%;
position: relative;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.slide-content {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 30px;
transform: translateY(100%);
transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}
.slide.active .slide-content {
transform: translateY(0);
}
.slide h2 {
font-size: 28px;
margin-bottom: 15px;
opacity: 0;
transform: translateX(-50px);
transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.3s;
}
.slide p {
font-size: 16px;
opacity: 0;
transform: translateX(-50px);
transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.5s;
}
.slide.active h2,
.slide.active p {
opacity: 1;
transform: translateX(0);
}
.controls {
position: absolute;
top: 50%;
width: 100%;
display: flex;
justify-content: space-between;
transform: translateY(-50%);
z-index: 10;
}
.control-btn {
background: rgba(255, 255, 255, 0.7);
border: none;
width: 50px;
height: 50px;
border-radius: 50%;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
margin: 0 20px;
transition: all 0.3s;
}
.control-btn:hover {
background: rgba(255, 255, 255, 0.9);
transform: scale(1.1);
}
.control-btn i {
font-size: 24px;
color: #333;
}
.dots {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
z-index: 10;
}
.dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.5);
margin: 0 5px;
cursor: pointer;
transition: all 0.3s;
}
.dot.active {
background: rgba(255, 255, 255, 0.9);
transform: scale(1.2);
}
.credit {
text-align: center;
margin-top: 30px;
color: #666;
font-size: 14px;
}
.credit a {
color: #0066cc;
text-decoration: none;
}
.credit a:hover {
text-decoration: underline;
}
@media (max-width: 768px) {
.slider {
height: 400px;
}
.slide-content {
padding: 20px;
}
.slide h2 {
font-size: 22px;
}
.slide p {
font-size: 14px;
}
}
</style>
<!-- 引入jQuery和Font Awesome图标库 -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
<div class="slider-container">
<div class="slider">
<div class="slide active">
<img src="https://picsum.photos/1000/500?random=1" alt="图片1">
<div class="slide-content">
<h2>自然风光之美</h2>
<p>探索世界上最壮观的自然景观,感受大自然的鬼斧神工。</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=2" alt="图片2">
<div class="slide-content">
<h2>城市建筑艺术</h2>
<p>现代都市的建筑奇迹,展现人类智慧与创造力的结晶。</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=3" alt="图片3">
<div class="slide-content">
<h2>人文历史遗迹</h2>
<p>穿越时空,感受古老文明的魅力与历史的厚重。</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/1000/500?random=4" alt="图片4">
<div class="slide-content">
<h2>科技未来展望</h2>
<p>前沿科技改变生活,探索未来世界的无限可能。</p>
</div>
</div>
</div>
<div class="controls">
<button class="control-btn prev-btn"><i class="fas fa-chevron-left"></i></button>
<button class="control-btn next-btn"><i class="fas fa-chevron-right"></i></button>
</div>
<div class="dots">
<div class="dot active"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
<script>
$(document).ready(function() {
const $slider = $('.slider');
const $slides = $('.slide');
const $dots = $('.dot');
let currentIndex = 0;
let slideCount = $slides.length;
let isAnimating = false;
// 初始化
function initSlider() {
$slides.eq(currentIndex).addClass('active');
$dots.eq(currentIndex).addClass('active');
}
// 切换幻灯片
function goToSlide(index) {
if (isAnimating || index === currentIndex) return;
isAnimating = true;
const direction = index > currentIndex ? -1 : 1;
// 移除当前活动状态
$slides.eq(currentIndex).removeClass('active');
$dots.eq(currentIndex).removeClass('active');
// 更新索引
currentIndex = index;
// 添加新活动状态
$slides.eq(currentIndex).addClass('active');
$dots.eq(currentIndex).addClass('active');
// 执行滑动动画
$slider.css('transform', `translateX(${-currentIndex * 100}%)`);
// 重置动画状态
setTimeout(() => {
isAnimating = false;
}, 800);
}
// 下一张
function nextSlide() {
const newIndex = (currentIndex + 1) % slideCount;
goToSlide(newIndex);
}
// 上一张
function prevSlide() {
const newIndex = (currentIndex - 1 + slideCount) % slideCount;
goToSlide(newIndex);
}
// 自动播放
let autoPlay = setInterval(nextSlide, 5000);
// 鼠标悬停暂停自动播放
$('.slider-container').hover(
function() {
clearInterval(autoPlay);
},
function() {
autoPlay = setInterval(nextSlide, 5000);
}
);
// 按钮事件
$('.next-btn').click(nextSlide);
$('.prev-btn').click(prevSlide);
// 点导航事件
$dots.click(function() {
const dotIndex = $(this).index();
goToSlide(dotIndex);
});
// 键盘导航
$(document).keydown(function(e) {
if (e.keyCode === 37) { // 左箭头
prevSlide();
} else if (e.keyCode === 39) { // 右箭头
nextSlide();
}
});
// 初始化滑块
initSlider();
});
</script>
</body>
</html>
jQuery图片配文字动画切换特效
于 2025-06-18 14:23:37 首次发布

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



