使用jQuery的方法实现轮播图。实现的轮播图最少由3幅图组成,每隔一段时间自动切换到下一幅图,鼠标停留时显示左右切候的按钮,点击左右按钮可以切换到上一幅或下幅图;点击图下面的圆形下标,切换到对应的图。图片切换需无缝切换,首尾图片切换时无违和感.
时间: 2023-06-27 21:08:27 浏览: 153
以下是一个简单的实现轮播图的jQuery代码:
HTML代码:
```html
<div class="slider">
<div class="slider-content">
<img src="img1.jpg" alt="">
<img src="img2.jpg" alt="">
<img src="img3.jpg" alt="">
</div>
<div class="slider-nav">
<a href="#" class="prev">‹</a>
<a href="#" class="next">›</a>
<div class="dots">
<a href="#" class="dot active"></a>
<a href="#" class="dot"></a>
<a href="#" class="dot"></a>
</div>
</div>
</div>
```
CSS代码:
```css
.slider {
position: relative;
}
.slider-content {
position: relative;
overflow: hidden;
}
.slider-content img {
display: inline-block;
vertical-align: top;
width: 100%;
}
.slider-nav {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 1;
}
.slider-nav a {
display: inline-block;
width: 30px;
height: 30px;
background-color: #fff;
color: #333;
text-align: center;
line-height: 30px;
font-size: 20px;
text-decoration: none;
border-radius: 50%;
opacity: 0.5;
transition: opacity 0.3s ease;
}
.slider-nav a:hover {
opacity: 1;
}
.slider-nav .prev {
margin-right: 10px;
}
.slider-nav .dots {
display: inline-block;
margin-left: 20px;
}
.slider-nav .dot {
display: inline-block;
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
margin-right: 5px;
opacity: 0.5;
transition: opacity 0.3s ease;
}
.slider-nav .dot.active {
opacity: 1;
}
```
jQuery代码:
```javascript
$(function() {
var $slider = $('.slider'),
$content = $slider.find('.slider-content'),
$imgs = $content.find('img'),
$nav = $slider.find('.slider-nav'),
$prev = $nav.find('.prev'),
$next = $nav.find('.next'),
$dots = $nav.find('.dot'),
imgCount = $imgs.length,
currentIndex = 0,
intervalId;
// 初始化
function init() {
$content.css({width: imgCount * 100 + '%'});
$imgs.css({width: 100 / imgCount + '%'});
$dots.eq(currentIndex).addClass('active');
startAutoPlay();
}
// 自动播放
function startAutoPlay() {
intervalId = setInterval(function() {
currentIndex++;
if (currentIndex === imgCount) {
currentIndex = 0;
}
gotoImg(currentIndex);
}, 3000);
}
// 停止自动播放
function stopAutoPlay() {
clearInterval(intervalId);
}
// 切换到指定的图片
function gotoImg(index) {
$content.animate({left: -index * 100 + '%'}, function() {
if (index === imgCount - 1) {
currentIndex = -1;
$content.css({left: 0});
}
});
$dots.removeClass('active').eq(index).addClass('active');
}
// 绑定事件
$slider.hover(function() {
stopAutoPlay();
$prev.show();
$next.show();
}, function() {
startAutoPlay();
$prev.hide();
$next.hide();
});
$prev.click(function() {
currentIndex--;
if (currentIndex === -1) {
currentIndex = imgCount - 1;
$content.css({left: -currentIndex * 100 + '%'});
}
gotoImg(currentIndex);
});
$next.click(function() {
currentIndex++;
if (currentIndex === imgCount) {
currentIndex = 0;
$content.css({left: 0});
}
gotoImg(currentIndex);
});
$dots.click(function() {
var index = $(this).index();
currentIndex = index;
gotoImg(currentIndex);
});
// 初始化
init();
});
```
以上代码包含了自动播放、停止自动播放、切换到指定的图片、绑定事件等功能,你可以根据自己的需要进行修改和扩展。
阅读全文