继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

jQuery实现轮播图!!

就酱ba
关注TA
已关注
手记 15
粉丝 13
获赞 318

<html>
<head>
<meta charset="utf-8"/>
<title>jQuery实现轮播图</title>
<style type="text/css">
body,div,ul,li{margin:0;padding:0;}
ul,li,ol{list-style:none;}
li{float:left;}
.scroll{position:relative;}
.scroll-ul{positon:absolute;left:0px;width:1100px;height:390px;overflow:hidden;}
.spot-ul{position:absolute;left:50%;bottom:10px;overflow:hidden;}
.spot-ul li{width:4px;height:4px;border-radius:50px;background:#eee;}
.spot-ul li.on{background:red;}

</style>
</head>
<body>
<div class="scroll">
<ul class="scroll-ul">
<li class="scroll-li"><img src="images/1.jpg" /></li>
<li class="scroll-li"><img src="images/2.jpg" /></li>
<li class="scroll-li"><img src="images/3.jpg" /></li>
<li class="scroll-li"><img src="images/4.jpg" /></li>
</ul>
//图片对应小圆点
<ol class="spot-ul">
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<script src="js/jquery-1.8.3.min.js"></script>
<script >
$(document).ready(function(){
var length,
currentIndex = 0,
interval,
hasStarted = false,//轮播图是否开始
t = 3000;//轮播时间
length = $('.scroll-li').length;
//将除了第一个其他li隐藏
$('.scroll-li:not(:first)').hide();
//给第一个圆点赋值激活
$('.spot-ul li:first').addClass('on');
// 小圆点hover效果
$('.spot-ul li').hover(function(e){
stop();
var preIndex = $(".spot-ul li").filter(".on").index(); //
currentIndex = $(this).index(); //
play(preIndex, currentIndex);
},function(){
start();
});
/从preIndex页翻到currentIndex页
preIndex翻页的起始页
currentIndex整数,翻到的那页
/
function play(preIndex, currentIndex) {
$('.scroll-li').eq(preIndex).fadeOut(500).parent().children().eq(currentIndex).fadeIn(500);
$('.spot-ul li').removeClass('on');
$('.spot-ul li').eq(currentIndex).addClass('on');
}
// 自动向后翻页
function next() {
var preIndex = currentIndex;
currentIndex = ++currentIndex % length;
play(preIndex, currentIndex);
}
// 开始轮播
function start() {
if(!hasStarted) {
hasStarted = true;
interval = setInterval(next, t);
}
}
//停止轮播
function stop() {
clearInterval(interval);
hasStarted = false;
}
//开始轮播
start();
});
</script>
</body>

打开App,阅读手记
33人推荐
发表评论
随时随地看视频慕课网APP

热门评论

当鼠标重复移入spot ul下的li 时 判断一下preindex和currenyindex是否一致再考虑执行play函数 连续切换spot-ul 下的li是动画跟不上,可以用is animated

请问这个代码。它轮播是不是只能播一下。就是说从1轮播到2然后就不会继续向下了。因为next()函数是不是只用了一次啊0,0看不太懂,哪位大神能指点一下下吗!谢谢!!

查看全部评论