原生JavaScript轮播图

效果图:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAemhpaHVpNDMx,size_20,color_FFFFFF,t_70,g_se,x_16

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta http-equiv="X-UA-Compatible" content="IE=Edge">

  <meta name="viewport" content="width=device-width, initial-scale=1">

 

  <title>HTML</title>

  

  <link rel="stylesheet" href="style.css">

  <script src="main.js"></script>

  

</head>

 

<body>

    <div class="focus">

        <a href="javascript:;" class="arrow_l"><</a>

        <a href="javascript:;" class="arrow_r">></a>

        <!-- 核心滚动区域 -->

        <ul>

            <li>

                <a href="javascript:;"><img src="/images/jpg1.jpg" alt=""></a>

            </li>

            <li>

                <a href="javascript:;"><img src="/images/jpg2.jpg" alt=""></a>

            </li>

            <li>

                <a href="javascript:;"><img src="/images/jpg3.jpg" alt=""></a>

            </li>

            <li>

                <a href="javascript:;"><img src="/images/jpg4.jpg" alt=""></a>

            </li>

        </ul>

        <!-- 小圆圈 -->

        <ol class="circle">

            

        </ol>

    </div>

</body>

 

</html>

* {

margin: 0;

padding: 0;

}

.focus {

    width: 200px;

    height: 120px;

    overflow: hidden;

    margin: 50px auto 0;

    position: relative;

}

.focus ul {

    display: flex;

    flex-wrap: wrap;

    width: 1000px;

    height: 120px;

    position: absolute;

}

.focus ul li {

    list-style: none;

    width: 200px;

    height: 100%;

}

.focus ul li img {

    width: 100%;

    height: 100%;

}

.focus .arrow_l,

.arrow_r {

    top: 50%;

    margin-top: -15px;

    width: 20px;

    height: 20px;

    display: block;

    text-decoration: none;

    display: none;

    position: absolute;

    z-index: 2;

    color: #fff;

    text-align: center;

    line-height: 20px;

    background: rgba(255, 255, 255, .3);

}

.focus .arrow_r {

    right: 0px;

}

.focus .circle {

    display: flex;

    flex-wrap: wrap;

    bottom: 3px;

    left: 40px;

    position: absolute;

    z-index: 3;

}

.focus .circle li {

    width: 15px;

    height: 15px;

    margin:0 3px;

    list-style: none;

    border-radius: 50%;

    border: 1px solid #999;

}

.current {

    background: #fff;

}
window.addEventListener('load', function() {

    //动画函数

    

    function animate(obj, target) {

        clearInterval(obj.timer);

        obj.timer = setInterval(function() {

            var step = (target - obj.offsetLeft) / 10;

            step = step > 0 ? Math.ceil(step) : Math.floor(step);

            if (obj.offsetLeft == target) {

                clearInterval(obj.timer);

            } else {

            obj.style.left = obj.offsetLeft + step + 'px';

            }

        }, 30)

    }

    

    var focus = document.querySelector('.focus');

    

    var arrow_l = document.querySelector('.arrow_l');

    var arrow_r = document.querySelector('.arrow_r');

    

    focus.addEventListener('mouseenter', function() {

        arrow_l.style.display = 'block';

        arrow_r.style.display = 'block';

        //停止定时器

        clearInterval(timer);

        timer = null;//清除变量

        console.log('清除定时器');

    })

    focus.addEventListener('mouseleave', function() {

        arrow_l.style.display = 'none';

        arrow_r.style.display = 'none';

        //鼠标离开,开启定时器

        var timer = setInterval(function() {

            arrow_r.click()

        }, 2000);

        console.log('开启定时器');

    })

    

    

    //动态生成小圆圈

    

    var ul = document.querySelector('ul');

    var ol = document.querySelector('ol');

    //console.log(ul.children)

    for (var i = 0 ; i < ul.children.length; i++) {

        //创建小li

        var li = document.createElement('li');

        //记录小li索引号,用自定义属性

        

        li.setAttribute('index', i);

        

        ol.appendChild(li);

        

        li.addEventListener('click', function() {

            //排他

            for (var i = 0; i < ol.children.length; i++) {

                ol.children[i].className = '';

            }

            this.className = 'current';

            var index = this.getAttribute('index');

            num = index;

            circle = index;

            //var focusetWidth = ul.children.offsetWidth;

            //console.log(focusetWidth)

            animate(ul,-index * 200);

        })

    }

    //将ol中第一个小li的类名设置为current

    ol.children[0].className = 'current';

    

    //克隆第一张图片到ul的最后面(一定是深克隆)

    var first = ul.children[0].cloneNode(true);

    ul.appendChild(first);

    var circle = 0;

    var num = 0;

    arrow_r.addEventListener('click', function() {

        if (num == ul.children.length - 1) {

            ul.style.left = 0;

            num = 0;

        }

        num++;

        circle++;

        animate(ul, -num * 200);

        if (circle == ol.children.length) {

            circle = 0;

        }

        for (var i = 0;i < ol.children.length; i++) {

            ol.children[i].className = '';

        }

        ol.children[circle].className = 'current';

    })

    

    //左箭头切换

    arrow_l.addEventListener('click', function() {

        if (num == 0) {

            ul.style.left = -(ul.children.length - 1) * 200 + 'px';

            num = ul.children.length - 1;

        }

        num--;

        circle--;

        animate(ul, -num * 200);

        if (circle < 0) {

            circle = 3;

        }

        for (var i = 0; i < ol.children.length; i++) {

            ol.children[i].className = '';

        }

        ol.children[circle].className = 'current';

    })

    

    //自动播放图片

    var timer = setInterval(function () {

        arrow_r.click();

    }, 2000);

})

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值