<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>焦點輪播圖效果</title><style type="text/css"> *{ margin: 0; padding: 0; list-style-type: ...
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>焦點輪播圖效果</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style-type: none;
}
.wrap{
width:490px;
height: 170px;
margin: 100px auto;
overflow: hidden;
position: relative;
}
.wrap ul{
position: absolute;
}
.wrap li{
height: 170px;
}
.list{
position: absolute;
right: 10px;
bottom: 10px;
}
.list li{
float: left;
width: 20px;
height: 20px;
background-color: #ccc;
border: 1px solid #666;
margin-left: 5px;
color: #000;
text-align: center;
line-height: 20px;
cursor: pointer;
}
.list li.on{
background-color: #E57000;
color: #fff;
}
</style>
</head>
<body>
<div class="wrap" id="wrap">
<ul class="pic" id="pic">
<li><img src="http://img.mukewang.com/54111cd9000174cd04900170.jpg" alt="圖1" /></li>
<li><img src="http://img.mukewang.com/54111dac000118af04900170.jpg" alt="圖2" /></li>
<li><img src="http://img.mukewang.com/54111d9c0001998204900170.jpg" alt="圖3" /></li>
<li><img src="http://img.mukewang.com/54111d8a0001f41704900170.jpg" alt="圖4" /></li>
<li><img src="http://img.mukewang.com/54111d7d00018ba604900170.jpg" alt="圖5" /></li>
</ul>
<ol class="list" id="list">
<li class="on">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
</div>
<script type="text/javascript">
window.onload=function () {
//獲取元素,申明變數
var wrap=document.getElementById('wrap'),
pic=document.getElementById('pic'),
list=document.getElementById('list').getElementsByTagName('li');
var index=0,
that=this,
timer=null;
//綁定
function autoPlay(){
index++;
if (index>=list.length) {
index=0;
}
qiehuan(index);
}
timer=setInterval(autoPlay,2000);
//滑鼠滑過整個容器時停止切換(定時器)
wrap.onmouseover=function(){
clearInterval(timer)
}
//滑鼠離開時,繼續切換
wrap.onmouseout=function(){
timer=setInterval(autoPlay,2000);
}
//遍歷所有數字導航,實現滑過切換至對應得圖片
for (var j = 0; j < list.length; j++) {
list[j].id=j;
list[j].onmouseover=function(){
qiehuan(this.id);
clearInterval(timer);
}
}
function qiehuan(num){
for (var i = 0; i < list.length; i++) {
list[i].className="";
}
list[num].className="on";
pic.style.top="-"+num*170+"px";
num=index;
}
}
</script>
</body>
</html>