html+css+javascript实现渐隐轮播

   实现效果:

    图片自动轮播,点击左右按钮会操作图片向前或向后,图片与小圆点相互呼应,具有交互效果。

   编写思想:   

   实现交互时使用了排他思想,同选项卡的功能;

  自动轮播采用了setInterval()

   具体步骤参详代码:

代码:

css:


    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
            box-sizing: border-box;
        }

        #focus {
            width: 600px;
            height: 400px;
            background-color: orange;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
        }

        #focus .pics {
            width: 100%;
            height: 100%;
        }

        #focus .pics li {
            width: 600px;
            height: 100%;
            text-align: center;
            font-size: 30px;
            line-height: 400px;
            opacity: 0;
            position: absolute;
            top:0;
            left:0;
            transition: all 0.3 ease;
        }
        #focus .pics li.on{
            opacity: 1;
        }

        #focus .circle {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);

        }

        #focus .circle li {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            float: left;
            margin-left: 5px;
            background-color: #fff;
            text-align: center;
            line-height: 16px;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
        }

        #focus .circle li.active {
            background-color: orange;
        }

        #focus .prev, .next {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            display: block;
            width: 28px;
            height: 56px;
            background-color: rgba(255, 255, 255, 0.7);
            border-radius: 0 56px 56px 0;
            line-height:56px;
            font-size:20px;
            color:#fff;
        }

        #focus .next {
            right: 0;
            border-radius: 56px 0 0 56px;
            padding-left:8px;
        }
    </style>

html:

<div id="focus">
        <ul class="pics">
            <li style="background-color: rgb(179, 115, 115);" class="on">0</li>
            <li style="background-color: rgb(122, 122, 206);">1</li>
            <li style="background-color: rgb(92, 162, 92);">2</li>
        </ul>
        <ol class="circle">
        </ol>
        <span class="prev">&lt;</span>
        <span class="next">&gt;</span>
    </div>

js:

<script>
        //1.查节点
        var focus = document.querySelector('#focus');
        var pics = document.querySelector('.pics');
        var lis = document.querySelectorAll('.pics li');
        var circle = document.querySelector('.circle');
        var prev = document.querySelector('.prev');
        var next = document.querySelector('.next');
        //获取li宽度
        var liW = lis[0].offsetWidth;
        var time = null;
        //动态生成小圆点
        for (var i = 0; i < lis.length; i++) {
            var li = document.createElement('li');
            li.setAttribute('idx', i);
            circle.appendChild(li);
            //排他思想 选项卡
            li.onclick = function () {
                var active = document.querySelector('.circle li.active');
                active.classList.remove('active');
                this.classList.add('active');
                var idx = this.getAttribute('idx');
                //解决index与idx与cir之间的关系
                index = idx;
                cir = idx;
                document.querySelector('.pics li.on').classList.remove('on');
                lis[idx].classList.add('on');
            }
        }
        circle.children[0].classList.add('active');

        //2.添加功能
        //鼠标经过
        focus.onmouseenter = function () {
            prev.style.display = "block";
            next.style.display = "block";
            //鼠标经过时停止自动轮播
            clearInterval(time);
        }
        //鼠标离开
        focus.onmouseleave = function () {
            prev.style.display = "none";
            next.style.display = "none";
            //自动轮播
            time = setInterval(function () {
                next.onclick();
            }, 2500)
        }
        var index = 0;
        var cir = 0;
        //单击next箭头
        next.onclick = function () {
                if (index == lis.length - 1) {
                    //return;//图片到最后一张不再继续往下
                    index = -1;     
                }
                index++;

                document.querySelector('.pics li.on').classList.remove('on');
                lis[index].classList.add('on');

                //右按钮控制小圆点
                cir++;
                if (cir == circle.children.length) {
                    cir = 0;
                }
                run(cir);
            }
        //单击prev箭头
        prev.onclick = function () {
                if (index == 0) {
                    //return;//图片到最后一张不再继续往下
                    index = lis.length;
                    
                }
                index--;
                document.querySelector('.pics li.on').classList.remove('on');
                lis[index].classList.add('on');
               
                //左按钮控制小圆点
                cir--;
                if (cir == -1) {
                    cir = circle.children.length - 1;
                }
                run(cir);
            }
        //自动轮播
        time = setInterval(function () {
            next.onclick();
        }, 2500)

        // 左右箭头控制小圆点
        function run(cir) {
            // cir当前小圆点的索引号
            var active = document.querySelector('.circle li.active');
            active.classList.remove('active');
            // circle.children[cir]对应索引号的孩子
            circle.children[cir].classList.add('active');
        }
    </script>

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值