要求一:jquery技术实现横向轮播图。要求二:jquery技术实现纵向轮播图
时间: 2024-06-14 18:03:28 浏览: 114
【JavaScript源代码】使用jquery实现轮播图效果.docx
以下是使用jQuery实现横向轮播图和纵向轮播图的示例代码:
1. 横向轮播图
HTML代码:
```html
<div class="slider">
<div class="slider-wrapper">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
</div>
```
CSS代码:
```css
.slider {
width: 500px;
height: 300px;
overflow: hidden;
}
.slider-wrapper {
width: 1500px;
height: 300px;
position: relative;
left: 0;
transition: left 1s ease-in-out;
}
.slider-wrapper img {
float: left;
width: 500px;
height: 300px;
}
```
JavaScript代码:
```javascript
$(document).ready(function() {
var sliderWrapper = $('.slider-wrapper');
var images = sliderWrapper.find('img');
var imageWidth = images.width();
var imageCount = images.length;
var totalWidth = imageWidth * imageCount;
var currentPosition = 0;
sliderWrapper.width(totalWidth);
function moveSlider() {
if (currentPosition == -totalWidth + imageWidth) {
currentPosition = 0;
} else {
currentPosition -= imageWidth;
}
sliderWrapper.css('left', currentPosition);
}
setInterval(moveSlider, 3000);
});
```
2. 纵向轮播图
HTML代码:
```html
<div class="slider">
<div class="slider-wrapper">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
</div>
```
CSS代码:
```css
.slider {
width: 500px;
height: 300px;
overflow: hidden;
}
.slider-wrapper {
width: 500px;
height: 900px;
position: relative;
top: 0;
transition: top 1s ease-in-out;
}
.slider-wrapper img {
position: absolute;
top: 0;
left: 0;
width: 500px;
height: 300px;
}
```
JavaScript代码:
```javascript
$(document).ready(function() {
var sliderWrapper = $('.slider-wrapper');
var images = sliderWrapper.find('img');
var imageHeight = images.height();
var imageCount = images.length;
var totalHeight = imageHeight * imageCount;
var currentPosition = 0;
sliderWrapper.height(totalHeight);
function moveSlider() {
if (currentPosition == -totalHeight + imageHeight) {
currentPosition = 0;
} else {
currentPosition -= imageHeight;
}
sliderWrapper.css('top', currentPosition);
}
setInterval(moveSlider, 3000);
});
```
阅读全文