WEB前端开发原生js简单轮播图

公众号开发前端与后台接口
做前端还是后台开发
后台开发和前端开发

html 代码

<!DOCTYPE html>
<head>
    <meta charset=”UTF-8″>
    <title></title>
    <style type=”text/css”>
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        body {
            padding: 20px;
        }
        #container {
            width: 445px;
            /*设置尺寸*/
            height: 272px;
            border: 1px solid #333;
            overflow: hidden;
            position: relative;
        }
        #list {
            width: 3115px;
            height: 272px;
            position: absolute;
            z-index: 1;
        }
        #list img {
            width: 445px;
            height: 272px;
            float: left;
        }
        #buttons {
            position: absolute;
            height: 10px;
            width: 100px;
            z-index: 2;
            bottom: 20px;
            left: 170px;
        }
        #buttons span {
            cursor: pointer;
            float: left;
            border: 1px solid #fff;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: #333;
            margin-right: 5px;
        }
        #buttons .on {
            background: orangered;
        }
        .arrow {
            cursor: pointer;
            display: none;
            line-height: 39px;
            text-align: center;
            font-size: 36px;
            font-weight: bold;
            width: 40px;
            height: 40px;
            position: absolute;
            z-index: 2;
            top: 100px;
            background-color: RGBA(0, 0, 0, .3);
            color: #fff;
        }
        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }
        #container:hover .arrow {
            display: block;
        }
        #prev {
            left: 20px;
        }
        #next {
            right: 20px;
        }
    </style>
    <script type=”text/javascript”>
        /* 知识点: */
        /* this用法 */
        /* DOM事件 */
        /* 定时器 */
        window.onload = function () {
            var container = document.getElementById(‘container’);
            var list = document.getElementById(‘list’);
            var buttons = document.getElementById(‘buttons’).getElementsByTagName(‘span’);
            var prev = document.getElementById(‘prev’);
            var next = document.getElementById(‘next’);
            var index = 1;
            var timer;
            function animate(offset) {
                //获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
                //且style.left获取的是字符串,需要用parseInt()取整转化为数字。
                var newLeft = parseInt(list.style.left) + offset;
                list.style.left = newLeft + ‘px’;
                //无限滚动判断
                if (newLeft > -445) {
                    list.style.left = -2225 + ‘px’;
                }
                if (newLeft < -2225) {
                    list.style.left = -445 + ‘px’;
                }
            }
            function play() {
                //重复执行的定时器
                timer = setInterval(function () {
                    next.onclick();
                }, 2000)
            }
            function stop() {
                clearInterval(timer);
            }
            function buttonsShow() {
                //将之前的小圆点的样式清除
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == “on”) {
                        buttons[i].className = “”;
                    }
                }
                //数组从0开始,故index需要-1
                buttons[index – 1].className = “on”;
            }
            prev.onclick = function () {
                index -= 1;
                if (index < 1) {
                    index = 5
                }
                buttonsShow();
                animate(445);
            };
            next.onclick = function () {
                //由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
                index += 1;
                if (index > 5) {
                    index = 1
                }
                animate(-445);
                buttonsShow();
            };
            for (var i = 0; i < buttons.length; i++) {
                buttons[i].onclick = function () {
                    //优化,当前图片点击当前的小圆点不执行以下代码。
                    if (this.className == “on”) {
                        return;
                    }
                    /* 这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,去谷歌this的用法 */
                    /* 由于这里的index是自定义属性,需要用到getAttribute()这个DOM2级方法,去获取自定义index的属性*/
                    var clickIndex = parseInt(this.getAttribute(‘index’));
                    var offset = 445 * (clickIndex – index); //这个index是当前图片停留时的index
                    animate(offset);
                    index = clickIndex; //存放鼠标点击后的位置,用于小圆点的正常显示
                    buttonsShow();
                }
            }
            container.onmouseover = stop;
            container.onmouseout = play;
            play();
        }
    </script>
</head>
<body>
    <div id=”container”>
        <div id=”list” style=”left: -445px;”>
            <img src=”https://www.rokub.com/wp-content/uploads/2018/05/a01ed079-51e5-11e8-9d3d-484d7ebf651c-720×340.jpg” alt=”1″ />
            <img src=”https://www.rokub.com/wp-content/uploads/2018/04/b6230aa6-2ba1-11e8-b1e6-8c859074c15d-520×245.jpg” alt=”1″ />
            <img src=”http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg” alt=”2″ />
            <img src=”https://www.rokub.com/wp-content/uploads/2018/04/b6230aa6-2ba1-11e8-b1e6-8c859074c15d-520×245.jpg” alt=”3″ />
            <img src=”https://www.rokub.com/wp-content/uploads/2018/05/a0eb5162-51e5-11e8-9169-484d7ebf651c-520×245.jpg” alt=”4″ />
            <img src=”https://www.rokub.com/wp-content/uploads/2018/05/a0eefb74-51e5-11e8-bcf4-484d7ebf651c-520×245.jpg” alt=”5″ />
            <img src=”https://www.rokub.com/wp-content/uploads/2018/05/a0eb5162-51e5-11e8-9169-484d7ebf651c-520×245.jpg” alt=”5″ />
        </div>
        <div id=”buttons”>
            <span index=”1″ class=”on”></span>
            <span index=”2″></span>
            <span index=”3″></span>
            <span index=”4″></span>
            <span index=”5″></span>
        </div>
        <a href=”javascript:;” id=”prev” class=”arrow”>
            <</a> <a href=”javascript:;” id=”next” class=”arrow”>>
        </a>
    </div>
</body>
</html>
前端开发与后台开发协助
前端开发被公司叫去做后台
前端开发与后台开发
» 本文来自:前端开发者 » 《WEB前端开发原生js简单轮播图》
» 本文链接地址:https://www.rokub.com/6249.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!