兼容ie及以上 轮播图

关于轮播图

  1. 原生JS
  2. 兼容IE7+等各大浏览器
  3. 左右箭头切换
  4. 点击索引切换
  5. 只需修改图片路径
  6. 项目依赖 animate.js

在这里插入图片描述

html

  <div class="all" id="box">
        <div class="screen" id="screen">
            <ul>
                <li>
                    <img src="./lun1_03.png" width="500" height="300" />
                </li>
                <li>
                    <img src="./lun1_03.png" width="500" height="300" />
                </li>
                <li>
                    <img src="./lun1_03.png" width="500" height="300" />
                </li>
                <li>
                    <img src="./lun1_03.png" width="500" height="300" />
                </li>
                <li>
                    <img src="./lun1_03.png" width="500" height="300" />
                </li>
            </ul>
            // 小圆点
            <ol></ol>
        </div>
        //左右箭头
        <div id="arr">
            <span id="left">&lt;</span>
            <span id="right">&gt;</span>
        </div>
    </div>

样式

   * {
            padding: 0;
            margin: 0;
            list-style: none;
            border: 0;
        }

        .all {
            width: 500px;
            height: 300px;
            padding: 7px;
            border: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }

        .screen {
            width: 500px;
            height: 300px;
            overflow: hidden;
            position: relative;
        }

        .screen li {
            width: 500px;
            height: 300px;
            overflow: hidden;
            float: left;
        }

        .screen ul {
            position: absolute;
            left: 0px;
            top: 0px;
            width: 3000px;
        }

        .all ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
            line-height: 20px;
            text-align: center;
        }

        .all ol li {
            float: left;
            width: 20px;
            height: 20px;
            background: #fff;
            border: 1px solid #ccc;
            margin-left: 10px;
            cursor: pointer;
        }

        .all ol li.current {
            background: yellow;
        }

        #arr {
            display: none;
            z-index: 1000;
        }

        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑体';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }

        #arr #right {
            right: 5px;
            left: auto;
        }

js


```javascript
 var screen = document.getElementById('screen')
        var ul = screen.getElementsByTagName('ul')[0]
        var lis = ul.children
        var ol = screen.getElementsByTagName('ol')[0]
        var box = document.getElementById('box')
        var arr = document.getElementById('arr')
        var right = document.getElementById('right')
        var indexGlobal = 0 //用于存储当前展示的图片的下标





        // 1. 动态创建数字按钮 - >根据ul中,li的个数,动态的创建ol中的li
        for (var i = 0; i < lis.length; i++) {
            var li = document.createElement('li')
            if (i == 0) {
                li.className='current'
            }
            li.id = i
            li.onclick = fn
            li.innerText = i + 1
            ol.appendChild(li)
        }

        //给ol里的li注册点击事件
        function fn() {
            var id = this.id
            console.log(id);
            
            var target = id * screen.offsetWidth
            animate(ul, -target, 7)

            // for (var i = 0; i < ol.children.length; i++) {
            //     ol.children[i].classList.remove('current')
            // }
            // this.className='current'
            $(this).addClass('current').siblings().removeClass('current')
            indexGlobal=id
        }

        var last = lis[0].cloneNode(true)
        ul.appendChild(last)


        box.onmouseenter = function () {
            arr.style.display = 'block'
        }

        box.onmouseleave = function () {
            arr.style.display = 'none'
        }



        right.onclick = function() {
           if(indexGlobal !=ol.children.length-1) {
            indexGlobal++
          ol.children[indexGlobal].click()
           }else{
            indexGlobal++
            var target = indexGlobal*screen.offsetWidth
            animate(ul,-target,20,function() {
                ul.style.left = 0
            })
            indexGlobal=0

            // for(var i = 0 ; i <ol.children.length;i++) {
            //     ol.children[i].classList.remove('current')
            // }
            $('ol li').removeClass('current')
            ol.children[indexGlobal].className='current'
           }
        }


        left.onclick = function() {
            if(indexGlobal != 0 ){
                indexGlobal--
                ol.children[indexGlobal].click()
            }else{
                indexGlobal=ol.children.length
                var target = indexGlobal*screen.offsetWidth
                ul.style.left = -target+'px'

                indexGlobal--
                // ul.style.left = -target+'px'
                // animate(ul,)
                ol.children[indexGlobal].click()

            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值