jquery轮播图代码
时间: 2024-02-18 09:57:18 浏览: 97
以下是一个简单的jquery轮播图代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>jQuery轮播图</title>
<style>
.slider {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
}
.slider img {
width: 100%;
height: 100%;
}
.slider .prev, .slider .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
padding: 10px;
cursor: pointer;
}
.slider .prev {
left: 10px;
}
.slider .next {
right: 10px;
}
</style>
</head>
<body>
<div class="slider">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<img src="image4.jpg" alt="Image 4">
img src="image5.jpg" alt="Image 5">
<div class="prev">Previous</div>
<div class="next">Next</div>
</div>
<script src="js/jquery-1.12.3.min.js"></script>
<script>
$(document).ready(function() {
var slider = $('.slider');
var images = slider.find('img');
var prevBtn = slider.find('.prev');
var nextBtn = slider.find('.next');
var currentIndex = 0;
function showImage(index) {
images.hide();
images.eq(index).show();
}
function showNextImage() {
currentIndex++;
if (currentIndex >= images.length) {
currentIndex = 0;
}
showImage(currentIndex);
}
function showPrevImage() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = images.length - 1;
}
showImage(currentIndex);
}
prevBtn.click(function() {
showPrevImage();
});
nextBtn.click(function() {
showNextImage();
});
showImage(currentIndex);
});
</script>
</body>
</html>
```
这段代码实现了一个简单的jquery轮播图,包括图片切换和上一页、下一页按钮。你可以根据需要修改样式和图片路径来适应你的项目。
阅读全文