首页 > 代码库 > jquery 实现的一款超简单的图片切换功能

jquery 实现的一款超简单的图片切换功能

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="http://www.mamicode.com/assets/js/jquery.js"></script>
</head>
<style>

</style>
<script type="text/javascript">
var t;
var speed = 2;//图片切换速度
var nowlan=0;//图片开始时间
function changepic() {
var imglen = $("div[name=‘pic‘]").find("div").length;//获取DIV 下的滚动条数
$("div[name=‘pic‘]").find("div").hide();//全部隐藏掉DIV

$("div[name=‘pic‘]").find("div").eq(nowlan).show();//只显示制定下表的DIV
nowlan = nowlan+1 ==imglen ?0:nowlan + 1;//计算下标
t = setTimeout("changepic()",1000);//重复执行
}

$(document).ready(function () {
changepic();
//鼠标在图片上悬停让切换暂停
$("div[name=‘pic‘]").hover(function () { clearInterval(t); });
//鼠标离开图片切换继续
$("div[name=‘pic‘]").mouseleave(function () { changepic(); });
});
</script>
<body>
<div name="pic" style="float:left; overflow:hidden;width:300px;height:240px;" >
<div style="border: 1px solid red;"><img width="300" height="240" src="http://www.mamicode.com/assets/img/1_thumb_G_1240902890710.jpg" alt="1"/></div>
<div style="border: 1px solid green;"><img width="300" height="240" src="http://www.mamicode.com/assets/img/3_thumb_G_1241422082679.jpg" alt="2"/></div>
<div style="border: 1px solid blue;"><img width="300" height="240" src="http://www.mamicode.com/assets/img/8_thumb_G_1241425513488.jpg" alt="3"/></div>
<div style="border: 1px solid blue;"><img width="300" height="240" src="http://www.mamicode.com/assets/img/8_thumb_G_1241425513488.jpg" alt="3"/></div>
</div>

</body>