js寫的簡單輪播圖

這個輪播圖代碼是從網上找來的,專門找了個寫法簡單的,只是做爲一個小練習,大概原理以下:javascript

1.首先是圖片切換
2.自動播放
3.調用自動播放
4.移動到容器上邊中止播放,離開自動播放
5.移動到導航上中止播放,離開自動播放
6.移動到導航上,切換相對應的導航html

 

而後貼代碼:java

<!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;}
.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 type="text/javascript">
window.onload=function(){
var wrap=document.getElementById('wrap'),
pic=document.getElementById('pic').getElementsByTagName("li"),
list=document.getElementById('list').getElementsByTagName('li'),
index=0,
timer=null;this

// 定義並調用自動播放函數
timer = setInterval(autoPlay, 2000);

// 鼠標劃過整個容器時中止自動播放
wrap.onmouseover = function () {
clearInterval(timer);
}htm

// 鼠標離開整個容器時繼續播放至下一張
wrap.onmouseout = function () {
timer = setInterval(autoPlay, 2000);
}
// 遍歷全部數字導航實現劃過切換至對應的圖片
for (var i = 0; i < list.length; i++) {
list[i].onmouseover = function () {
clearInterval(timer);
index = this.innerText - 1;
changePic(index);
};
};

function autoPlay () {
if (++index >= pic.length) index = 0;
changePic(index);
}seo

// 定義圖片切換函數
function changePic (curIndex) {
for (var i = 0; i < pic.length; ++i) {
pic[i].style.display = "none";
list[i].className = "";
}
pic[curIndex].style.display = "block";
list[curIndex].className = "on";
}圖片

};
</script>
</head>
<body>
<div class="wrap" id='wrap'>
<ul id="pic">
<li><img src="http://img.mukewang.com/54111cd9000174cd04900170.jpg" alt=""></li>
<li><img src="http://img.mukewang.com/54111dac000118af04900170.jpg" alt=""></li>
<li><img src="http://img.mukewang.com/54111d9c0001998204900170.jpg" alt=""></li>
<li><img src="http://img.mukewang.com/54111d8a0001f41704900170.jpg" alt=""></li>
<li><img src="http://img.mukewang.com/54111d7d00018ba604900170.jpg" alt=""></li>
</ul>
<ol id="list">
<li class="on">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
</div>
</body>
</html>ip

 

只是我的的一個小練習,但願諸位大神看了,不要黑我...get

相關文章
相關標籤/搜索