html+css+javascript实现渐隐轮播

实现效果:

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

编写思想:

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

自动轮播采用了setInterval();

具体步骤参详代码:

代码:

css:

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:

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:

javascript 复制代码
<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>

运行结果:

相关推荐
高级程序源2 小时前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计
javascript·vue.js·spring boot·后端·python·django·课程设计
lizz22763 小时前
Babel插件手册
javascript
志尊宝5 小时前
Vue3 零基础每日笔记(040):Vite 创建 Vue3 + TS 项目——企业标配从这一篇开始
笔记·vue·html·前端开发·软件开发
前端·柱子5 小时前
q-floodfill白边锯齿?一招搞定抗锯齿边缘问题
前端·html·canva可画
VXbishe6 小时前
【课程设计】基于SpringBoot的乡村政务服务系统的设计与实现-计算机毕设77011
javascript·vue.js·spring boot·python·php·课程设计·政务
志尊宝6 小时前
Vue3 零基础每日笔记(037):动手封装 useMouse 与 useWindowSize——事件监听的标准模板
前端·javascript·vue.js·笔记·html5
shmily麻瓜小菜鸡6 小时前
TDD(测试驱动开发)详解
开发语言·javascript·typescript·node.js·ecmascript
wcy03108 小时前
POST为什么会发送两次请求
javascript·ajax
志尊宝8 小时前
Vue3 零基础每日笔记(047):动态路由与路由参数——:id 传参、query 传参、props 解耦
前端·javascript·vue.js·笔记·html5
qq_589666059 小时前
移动前端开发之viewport的深入理解
html