首页 > 代码库 > javascript图片轮播
javascript图片轮播
图片轮播源代码
复制可用
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;
padding:0;
list-style:none;}
.wrap{height:170px;
width:490px;
margin:60px auto;
overflow: hidden;
position: relative;
margin:100px auto;}
.wrap ul{position:absolute;}
.wrap ul li{height:170px;}
.wrap ol{position:absolute;
right:5px;
bottom:10px;}
img{width: 500px; height: 300px}
.wrap ol li{height:20px; width: 20px;
background:#ccc;
border:solid 1px #666;
margin-left:5px;
color:#000;
float:left;
line-height:center;
text-align:center;
cursor:pointer;}
.wrap ol .on{background:#E97305;
color:#fff;}
</style>
<script>
window.onload=function () {
var pic=document.getElementById(‘pic‘) .getElementsByTagName(‘li‘), //获取图片li元素
list=document.getElementById(‘list‘).getElementsByTagName(‘li‘), //获取图片序号li元素
index=0; //赋值从第一张图片开始播放
//以下为自动播放代码
var time=setInterval(autoplay,2000);
function autoplay() {
index++;
if(index>=pic.length){
index=0;
}
checkPic(index);
}
function checkPic(Picindex) {
for(var i=0; i<pic.length; i++){
pic[i].style.display="none";
list[i].className=""
}
pic[Picindex].style.display="block";
list[Picindex].className="on";
index=Picindex;
}
//以下为触摸显示代码
//触摸时停止播放,移开时继续播放
for(a=0; a<list.length; a++) {
list[a].id =a; //遍历id
list[a].onmousemove = function () {
for(b=0; b<list.length; b++)
{
list[b].className=""; //隐藏序号的样式
pic[b].style.display="none" //隐藏图片
}
list[this.id].className="on"; //显示序号的样式
pic[this.id].style.display = "block" //显示图片
clearInterval(time); //停止循环播放
index=[this.id]; //赋值指向时的图片id
}
list[a].onmouseout=function(){
time=setInterval(autoplay,2000); //继续播放
}
pic[a].onmousemove=function(){
clearInterval(time);
}
pic[a].onmouseout=function(){
time=setInterval(autoplay,2000);
}
}
}
</script>
</head>
<body>
<div class="wrap" id=‘wrap‘>
<ul id="pic">
<li><img src="http://www.mamicode.com/images/img1.jpg" ></li>
<li><img src="http://www.mamicode.com/images/img2.jpg" ></li>
<li><img src="http://www.mamicode.com/images/img3.jpg" ></li>
<li><img src="http://www.mamicode.com/images/img4.jpg" ></li>
<li><img src="http://www.mamicode.com/images/img5.jpg" ></li>
</ul>
<ol id="list">
<li class="on" onm ouseout="zx()">1</li>
<li onm ouseout="zx()">2</li>
<li onm ouseout="zx()">3</li>
<li onm ouseout="zx()">4</li>
<li onm ouseout="zx()">5</li>
</ol>
</div>
</body>
</html>
javascript图片轮播