js实现图片轮播

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</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 type = "text/javascript" >
        window.onload = function(){
            var images = document.getElementsByTagName('img');
            var pos = 0;
            var len = images.length;

            setInterval(function(){
                images[pos].style.display = 'none';
                pos = ++pos == len ? 0 : pos;
                images[pos].style.display = 'inline';
            },1000);
        };
    </script>
</head>
<body>
    <div id="tab">
        <img src = '1.jpg' width = "400px" height="250px">
        <img src = '2.jpg' width = "400px" height="250px">
        <img src = '3.jpg' width = "400px" height="250px">

    </div>
</body>
</html>
只有图片自动播放的功能

猜你喜欢

转载自blog.csdn.net/qq_38362772/article/details/81016062