首页 > 代码库 > jQuery图片轮播
jQuery图片轮播
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>lb</title>
<script src="http://www.mamicode.com/js/jquery-1.8.3.min.js"></script>
<style>
body{width: 992px;margin:0 auto;background-color:lavender;}
.lb{width: 500px;height: 200px;overflow: hidden;z-index: 1;}
ol{position:relative;width: 120px;height: 20px;top:-30px;left:400px;margin: 0;padding: 0;z-index:3;}
ol li{float:left;margin:10px;list-style-image: url("imgs/a.jpg");}
ol li.seleted{list-style-image: url("imgs/b.jpg");}
.tm{width: 500px;height: 30px;background-color:#000; position:absolute;top:170px;z-index:2;filter: alpha(opacity=50);-moz-opacity: 0.50;opacity: 0.50;}
.tm .txt{font-family: 微软雅黑;font-size: 14px;color: antiquewhite;margin-top: 7px;float: left;}
</style>
</head>
<body>
<div class="lb">
<img src="http://www.mamicode.com/imgs/icon_swhj1.jpg" width="500px" height="200px">
<img src="http://www.mamicode.com/imgs/icon_swhj3.jpg" width="500px" height="200px">
<img src="http://www.mamicode.com/imgs/icon_swhj4.jpg" width="500px" height="200px">
<img src="http://www.mamicode.com/imgs/icon_swhj5.jpg" width="500px" height="200px">
</div>
<ol class="bn">
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
<div class="tm">
<span class="txt">你好吗1</span>
<span class="txt">你好吗2</span>
<span class="txt">你好吗3</span>
<span class="txt">你好吗4</span>
</div>
</body>
<script>
$(function(){
var index=0;
var imgs=$(".lb img");//获取图片
var txt=$(".tm span");//文字
var sel=$(".bn li");//小圆点
var len=imgs.length;
var timer = null;
//延时切换图片
function startTime(){
timer = setInterval(function(){
tplb(index);
index++;
if(index==len)
{
index=0;
}
},3000);
}
//停止延时切换
function stopTime(){
if(timer){
clearInterval(timer);
}
}
function tplb(n){
txt.eq(n).fadeIn(10).siblings("span").fadeOut(10);
imgs.eq(n).fadeIn(20).siblings("img").fadeOut(20);
sel.eq(n).css("list-style-image","url(‘imgs/b.jpg‘)").siblings("li").css("list-style-image","url(‘imgs/a.jpg‘)");
}
txt.eq(0).fadeIn(10).siblings("span").fadeOut(10);//初始化
imgs.eq(0).fadeIn(10).siblings("img").fadeOut(10);
sel.eq(0).css("list-style-image","url(‘imgs/b.jpg‘)").siblings("li").css("list-style-image","url(‘imgs/a.jpg‘)");
//鼠标一上小圆点切换
sel.mouseover(function(){
index=$(this).index("li");
tplb(index);
});
startTime();//初始化
//当鼠标移动到图片上是,停止切换
imgs.mouseover(function(){
stopTime();
});
//当鼠标离开时,开启切换
imgs.mouseleave(function(){
startTime();
});
});
</script>
</html>
jQuery图片轮播