jQuery圆形轮播自动切换图文
以下是一个使用jQuery实现圆形轮播图片的简单示例,包括自动轮播功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery圆形轮播</title>
<style>
.carousel {
position: relative;
width: 500px;
height: 300px;
margin: auto;
}
.carousel-inner {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}
.carousel-inner img {
width: 100%;
height: 100%;
}
.carousel-dots {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.carousel-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
cursor: pointer;
}
.carousel-dot.active {
background-color: #333;
}
</style>
</head>
<body>
<div class="carousel">
<div class="carousel-inner">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<!-- 更多图片... -->
</div>
<div class="carousel-dots">
<span class="carousel-dot active"></span>
<span class="carousel-dot"></span>
<span class="carousel-dot"></span>
<!-- 更多点... -->
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var $carousel = $('.carousel');
var $carouselInner = $carousel.find('.carousel-inner');
var $carouselDots = $carousel.find('.carousel-dots');
var $carouselDotsItems = $carouselDots.find('.carousel-dot');
var currentIndex = 0;
function goToNextSlide() {
currentIndex = (currentIndex + 1) % $carouselDotsItems.length;
$carouselInner.animate({ top: -currentIndex * 100 + '%' }, 500);
$carouselDotsItems.removeClass('active').eq(currentIndex).addClass('active');
}
setInterval(goToNextSlide, 3000);
$carouselDotsItems.on('click', function() {
currentIndex = $(this).index();
$carouselDotsItems.removeClass('active').eq(currentIndex).addClass('active');
$carouselInner.stop().animate({ top: -currentIndex * 100 + '%' }, 500);
});
});
</script>
</body>
</html>
这段代码实现了一个基本的圆形轮播,其中包括自动轮播功能和用户可以
评论已关闭