<!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;
font-family: 'Arial', sans-serif;
}
body {
background-color: #222;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: #fff;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 40px 20px;
text-align: center;
}
h1 {
font-size: 2.5rem;
margin-bottom: 30px;
color: #fff;
position: relative;
text-shadow: 0 2px 5px rgba(0,0,0,0.5);
}
h1:after {
content: '';
display: block;
width: 80px;
height: 4px;
background: linear-gradient(to right, #3498db, #9b59b6);
margin: 15px auto;
border-radius: 2px;
}
.note {
background-color: rgba(52, 152, 219, 0.1);
padding: 15px;
border-left: 4px solid #3498db;
margin: 20px 0 40px;
border-radius: 0 4px 4px 0;
}
.note a {
color: #3498db;
text-decoration: none;
font-weight: bold;
}
.note a:hover {
text-decoration: underline;
}
.slider-container {
position: relative;
width: 800px;
height: 450px;
margin: 0 auto;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
border-radius: 10px;
}
.slider {
position: relative;
width: 100%;
height: 100%;
}
.slide {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
opacity: 0;
transition: opacity 0.5s ease;
}
.slide.active {
opacity: 1;
}
.slide-content {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 30px;
background: linear-gradient(to top, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0) 100%);
color: white;
text-align: left;
}
.slide-title {
font-size: 24px;
margin-bottom: 10px;
}
.slide-desc {
font-size: 16px;
opacity: 0.8;
}
.slider-nav {
margin-top: 20px;
display: flex;
justify-content: center;
gap: 10px;
}
.slider-btn {
width: 50px;
height: 50px;
background-color: rgba(255,255,255,0.2);
border: none;
border-radius: 50%;
color: white;
font-size: 18px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
.slider-btn:hover {
background-color: rgba(255,255,255,0.3);
transform: scale(1.1);
}
.transition-selector {
margin-top: 30px;
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 10px;
}
.transition-btn {
padding: 8px 15px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
.transition-btn:hover {
background-color: #2980b9;
}
.transition-btn.active {
background-color: #2ecc71;
}
/* 切片动画样式 */
.slice {
position: absolute;
background-size: cover;
background-position: center;
opacity: 0;
transition: all 0.8s cubic-bezier(0.5, 0, 0.5, 1);
}
/* 响应式设计 */
@media (max-width: 900px) {
.slider-container {
width: 100%;
height: 400px;
}
}
@media (max-width: 600px) {
.slider-container {
height: 300px;
}
.slide-content {
padding: 15px;
}
.slide-title {
font-size: 18px;
}
.slide-desc {
font-size: 14px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>jQuery多种切片过渡动画幻灯片</h1>
<div class="slider-container" id="slider">
<div class="slider">
<div class="slide active" style="background-image: url('https://source.unsplash.com/random/800x450?nature,1');">
<div class="slide-content">
<h3 class="slide-title">自然风光</h3>
<p class="slide-desc">探索大自然的壮丽景观和无限魅力</p>
</div>
</div>
<div class="slide" style="background-image: url('https://source.unsplash.com/random/800x450?city,2');">
<div class="slide-content">
<h3 class="slide-title">城市风光</h3>
<p class="slide-desc">感受现代都市的繁华与活力</p>
</div>
</div>
<div class="slide" style="background-image: url('https://source.unsplash.com/random/800x450?animal,3');">
<div class="slide-content">
<h3 class="slide-title">野生动物</h3>
<p class="slide-desc">了解各种野生动物的生活习性</p>
</div>
</div>
<div class="slide" style="background-image: url('https://source.unsplash.com/random/800x450?technology,4');">
<div class="slide-content">
<h3 class="slide-title">科技创新</h3>
<p class="slide-desc">探索科技发展的前沿</p>
</div>
</div>
</div>
</div>
<div class="slider-nav">
<button class="slider-btn" id="prev-btn"><i class="fas fa-chevron-left"></i></button>
<button class="slider-btn" id="next-btn"><i class="fas fa-chevron-right"></i></button>
</div>
<div class="transition-selector">
<button class="transition-btn active" data-transition="horizontal">水平切片</button>
<button class="transition-btn" data-transition="vertical">垂直切片</button>
<button class="transition-btn" data-transition="diagonal">对角线切片</button>
<button class="transition-btn" data-transition="random">随机方块</button>
<button class="transition-btn" data-transition="spiral">螺旋效果</button>
<button class="transition-btn" data-transition="fade">淡入淡出</button>
</div>
</div>
<!-- 引入jQuery和Font Awesome -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/js/all.min.js"></script>
<script>
$(document).ready(function() {
const $slider = $('.slider');
const $slides = $('.slide');
const $prevBtn = $('#prev-btn');
const $nextBtn = $('#next-btn');
const $transitionBtns = $('.transition-btn');
let currentSlide = 0;
let totalSlides = $slides.length;
let transitionType = 'horizontal';
let isAnimating = false;
// 初始化幻灯片
function initSlider() {
$slides.removeClass('active');
$slides.eq(currentSlide).addClass('active');
}
// 切换过渡效果
$transitionBtns.on('click', function() {
$transitionBtns.removeClass('active');
$(this).addClass('active');
transitionType = $(this).data('transition');
});
// 下一张幻灯片
function nextSlide() {
if (isAnimating) return;
isAnimating = true;
let nextSlide = (currentSlide + 1) % totalSlides;
animateTransition(currentSlide, nextSlide);
currentSlide = nextSlide;
}
// 上一张幻灯片
function prevSlide() {
if (isAnimating) return;
isAnimating = true;
let prevSlide = (currentSlide - 1 + totalSlides) % totalSlides;
animateTransition(currentSlide, prevSlide);
currentSlide = prevSlide;
}
// 按钮事件
$nextBtn.on('click', nextSlide);
$prevBtn.on('click', prevSlide);
// 自动播放
let autoPlay = setInterval(nextSlide, 5000);
// 鼠标悬停暂停自动播放
$slider.hover(
function() {
clearInterval(autoPlay);
},
function() {
autoPlay = setInterval(nextSlide, 5000);
}
);
// 动画过渡效果
function animateTransition(current, next) {
const $currentSlide = $slides.eq(current);
const $nextSlide = $slides.eq(next);
const sliderWidth = $slider.width();
const sliderHeight = $slider.height();
// 创建切片容器
const $slicesContainer = $('<div class="slices-container"></div>').css({
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 10
});
$slider.append($slicesContainer);
// 根据过渡类型创建切片
let slices = [];
switch(transitionType) {
case 'horizontal':
// 水平切片 (8个)
for (let i = 0; i < 8; i++) {
slices.push({
width: sliderWidth,
height: sliderHeight / 8,
top: i * (sliderHeight / 8),
left: 0,
delay: i * 0.1,
direction: 'left'
});
}
break;
case 'vertical':
// 垂直切片 (8个)
for (let i = 0; i < 8; i++) {
slices.push({
width: sliderWidth / 8,
height: sliderHeight,
top: 0,
left: i * (sliderWidth / 8),
delay: i * 0.1,
direction: 'up'
});
}
break;
case 'diagonal':
// 对角线切片 (12个)
for (let i = 0; i < 12; i++) {
slices.push({
width: sliderWidth,
height: sliderHeight / 12,
top: i * (sliderHeight / 12),
left: i % 2 === 0 ? 0 : -sliderWidth,
delay: i * 0.05,
direction: i % 2 === 0 ? 'right' : 'left'
});
}
break;
case 'random':
// 随机方块 (16个)
const cols = 4;
const rows = 4;
const cellWidth = sliderWidth / cols;
const cellHeight = sliderHeight / rows;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
slices.push({
width: cellWidth,
height: cellHeight,
top: r * cellHeight,
left: c * cellWidth,
delay: Math.random() * 0.5,
direction: Math.random() > 0.5 ? 'up' : 'down'
});
}
}
break;
case 'spiral':
// 螺旋效果 (20个)
const centerX = sliderWidth / 2;
const centerY = sliderHeight / 2;
const maxRadius = Math.sqrt(centerX * centerX + centerY * centerY);
for (let i = 0; i < 20; i++) {
const angle = (i / 20) * Math.PI * 2;
const radius = (i / 20) * maxRadius;
slices.push({
width: 40,
height: 40,
top: centerY + radius * Math.sin(angle) - 20,
left: centerX + radius * Math.cos(angle) - 20,
delay: i * 0.05,
direction: 'scale'
});
}
break;
case 'fade':
// 淡入淡出
$currentSlide.animate({ opacity: 0 }, 800);
$nextSlide.animate({ opacity: 1 }, 800, function() {
$currentSlide.removeClass('active');
$nextSlide.addClass('active');
isAnimating = false;
});
return;
}
// 创建切片元素
slices.forEach((slice, index) => {
const $slice = $('<div class="slice"></div>').css({
position: 'absolute',
width: slice.width,
height: slice.height,
top: slice.top,
left: slice.left,
backgroundImage: $nextSlide.css('background-image'),
opacity: 0
});
$slicesContainer.append($slice);
// 设置动画
setTimeout(() => {
switch(slice.direction) {
case 'left':
$slice.css({
left: -slice.width,
opacity: 1
}).animate({
left: 0
}, 800);
break;
case 'right':
$slice.css({
left: slice.width,
opacity: 1
}).animate({
left: 0
}, 800);
break;
case 'up':
$slice.css({
top: -slice.height,
opacity: 1
}).animate({
top: 0
}, 800);
break;
case 'down':
$slice.css({
top: slice.height,
opacity: 1
}).animate({
top: 0
}, 800);
break;
case 'scale':
$slice.css({
transform: 'scale(0)',
opacity: 1
}).animate({
transform: 'scale(1)'
}, 800);
break;
}
}, slice.delay * 1000);
});
// 动画完成后清理
setTimeout(() => {
$slicesContainer.remove();
$currentSlide.removeClass('active');
$nextSlide.addClass('active');
isAnimating = false;
}, 1500);
}
// 初始化
initSlider();
});
</script>
</body>
</html>
jQuery多种切片过渡动画幻灯片
于 2025-06-25 16:54:13 首次发布