Other_7.一個比較簡單的HTML+JS圖片輪播效果

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
#tab { overflow:hidden; width:400px; height:250px; position:relative; float:left;}
#tab>img:not(:first-child){ display:none; }
 
</style>
<script>
    window.onload = function(){
     
        var images = document.getElementsByTagName('img');
        var pos = 0;
        var len = images.length;
         
        setInterval(function(){
          //通過每隔2秒就執行一次判斷,++pos是否等於len,如果是,就返回0,否則就返回pos;然後將圖片下標爲pos的圖片設置爲顯示,前一個圖片爲隱藏
            images[pos].style.display = 'none';
            pos = ++pos == len ? 0 : pos;
            images[pos].style.display = 'inline';
         
        },2000);//隔多久循環一次,這裏是2秒
         
    };
</script>
 
</head>
 
<body>
<div id="tab">
    <img src="img/01.jpg" width="400" height="250" alt="01.jpg"/>
    <img src="img/02.jpg" width="400" height="250" alt="02.jpg"/>
    <img src="img/03.jpg" width="400" height="250" alt="03.jpg"/>
</div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章