jquery技术实现纵向轮播图自动
时间: 2023-12-25 19:28:38 浏览: 136
以下是使用jQuery实现纵向轮播图自动的代码:
HTML代码:
```html
<div class="slider">
<ul>
<li><img src="image1.jpg" alt=""></li>
<li><img src="image2.jpg" alt=""></li>
<li><img src="image3.jpg" alt=""></li>
<li><img src="image4.jpg" alt=""></li>
</ul>
</div>
```
CSS代码:
```css
.slider {
height: 300px;
overflow: hidden;
}
.slider ul {
list-style: none;
margin: 0;
padding: 0;
height: 1200px; /* 图片高度的总和 */
-webkit-animation: slide 20s linear infinite;
animation: slide 20s linear infinite;
}
.slider li {
height: 300px;
}
.slider li img {
width: 100%;
height: 100%;
}
@-webkit-keyframes slide {
0% {
margin-top: 0;
}
100% {
margin-top: -900px; /* 图片高度的总和减去容器高度 */
}
}
@keyframes slide {
0% {
margin-top: 0;
}
100% {
margin-top: -900px; /* 图片高度的总和减去容器高度 */
}
}
```
JavaScript代码:
```javascript
$(function() {
var slider = $('.slider ul');
var sliderChild = slider.find('li');
var height = sliderChild.height();
var length = sliderChild.length;
var totalHeight = height * length;
var curIndex = 0;
setInterval(changeIndex, 3000);
function changeIndex() {
var prevIndex = curIndex;
curIndex++;
if (curIndex >= length) {
curIndex = 0;
}
slider.animate({marginTop: -curIndex*height}, 1000);
}
});
```
阅读全文