原生JS轮播图

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
                list-style: none;
            }

            body {
                width: 640px;
                height: 340px;
                padding: 100px;
            }

            img {
                display: block;
            }

            #box {
                width: 620px;
                height: 340px;
                position: relative;
                overflow: hidden;
                box-shadow: 0 0 10px rgba(0, 0, 0, .8);
            }

            #imgs {
                width: 2480px;
                height: 340px;
                position: absolute;             
            }

            #imgs li {
                width: 620px;
                height: 340px;
                float: left;
            }

            #scroll {
                position: absolute;
                bottom: 10px;
                right: 40%;
            }

            #scroll li {
                float: left;
                display: block;
                width: 10px;
                height: 8px;
                border-radius: 8px;
                background: lightgray;
                margin-right: 10px;
                box-shadow: 0 0 5px rgba(0, 0, 0, .8);
            }

            #scroll li.active {
                width: 30px;
            }

            #btn {
                position: relative;
                top: 40%;
                width: 620px;
                height: 50px;               
            }

            #btn span {
                float: left;
                display: block;
                width: 27px;
                height: 50px;               
                cursor: pointer;
            }

            #left {
                z-index: 10;
                position: absolute;
                left: 0;
                background: url(img/bg_24.png) 54px 0;
            }

            #right {
                z-index: 10;
                position: absolute;
                right: 0;
                background: url(img/bg_24.png) 81px 0;
            }
        </style>
    </head>

    <body>
        <div id="box">
            <ul id="imgs">
                <li><img src="img/1.jpg" /></li>
                <li><img src="img/2.jpg" /></li>
                <li><img src="img/3.jpg" /></li>
                <li><img src="img/4.jpg" /></li>
            </ul>
            <ul id="scroll">
                <li class="active"></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <div id="btn">
                <span id="left"></span>
                <span id="right"></span>
            </div>
        </div>
    </body>
    <script>
        var oBox = document.getElementById('box');
        var oImgs = document.getElementById('imgs');
        oImgs.innerHTML += oImgs.innerHTML;
        var oLi = oImgs.getElementsByTagName('li');
        var oUl = document.getElementById('scroll');
        var aLi = oUl.getElementsByTagName('li');
        var oBtn = document.getElementById('btn');
        var oLeft = document.getElementById('left');
        var oRight = document.getElementById('right');
        var now = 0;
        var timer = null;
        var oW = oLi[0].offsetWidth;
        var speed = 20;
        oImgs.style.width = oLi[0].offsetWidth*oLi.length + 'px';


        //小圆点的状态
        function show() {
            for (i = 0; i < aLi.length; i++) {
                aLi[i].className = '';
            }
            aLi[now].className = 'active';
            oImgs.style.left = -oW * now + 'px';
            if (oImgs.style.left <= -oImgs.style.width / 2) {
                oImgs.style.left = 0;
            }
        }

        //点击向左移动
        oLeft.onclick = function() {
            next();
        }

            //向右移动
        oRight.onclick = function() {
            prev();
        }

        function next() {
            now++
            if (now > oLi.length/2 - 1) {
                now = 0;
            }
            show();
        }

        function prev() {
            now--
            if (now < 0) {
                now = oLi.length/2 - 1;
            }
            show();
        }
        //小圆点对应图片
        for (var i = 0; i < aLi.length; i++) {
            aLi[i].index = i;
            aLi[i].onclick = function() {
                now = this.index;
                show();
            }
        }
        //自动轮播
        function auto() {
            timer = setInterval(function() {
                next();
            }, 2000);
        }
        auto();
        oBox.onmouseover = function() {
            clearInterval(timer);
        }
        oBox.onmouseout = function() {
            auto();
        }
    </script>

</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值