//轮播
$(function () {
// var a = document.body.clientWidth;
var a = 540;
var x = 0;
var timera = null;
var firstimga = $('.imga').children().clone(); //克隆
$('.imga').append(firstimga);
$('.imga').css('width',5400+"px");
$('.swiper-button-next').click(function () {
x++;
if (x == 6) {
x = 1; //这里不是i=0
$('.imga').css({left: 0}); //保证无缝轮播,设置left值
}
$('.imga').stop().animate({left: -x * a - 2});
// alert(2)
});
// 上一个按钮
$('.swiper-button-prev').click(function () {
clearInterval(timera);
x--;
if (x == -1) {
x = 6 - 2;
$('.imga').css({left: -(6 - 1) * a});
}
$('.imga').stop().animate({left: -x * a - 2});
})
// 下一个按钮
//定时器自动播放
timera = setInterval(function () {
x++;
if (x == 6) {
x = 0;
$('.imga').css({left: 0});
}
$('.imga').stop().animate({left: -x * a}, 800);
}, 2000)
//鼠标移入,暂停自动播放,移出,开始自动播放
$('.bannera').hover(function () {
clearInterval(timera);
}, function () {
timera = setInterval(function () {
x++;
if (x == 6) {
x = 1;
$('.imga').css({left: 0});
}
$('.imga').stop().animate({left: -x * a}, 800);
}, 2000)
})
});
</script>
bootstrap实现图片轮播:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<style>
/* Make the image fully responsive */
.carousel-inner img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- 指示符 -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- 轮播图片 -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="http://static.runoob.com/images/mix/img_fjords_wide.jpg">
</div>
<div class="carousel-item">
<img src="http://static.runoob.com/images/mix/img_nature_wide.jpg">
</div>
<div class="carousel-item">
<img src="http://static.runoob.com/images/mix/img_mountains_wide.jpg">
</div>
</div>
<!-- 左右切换按钮 -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
</body>
</html>