Other_7.一个比较简单的HTML+JS图片轮播效果

来源:互联网 发布:大芒果数据库修改 编辑:程序博客网 时间:2024/06/09 16:57
<!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>
0 0