• JS轮播图实现


    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.0">
        <title>Documenttitle>
    
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }
    
            ul,
            ol {
                list-style: none;
            }
    
            a {
                text-decoration: none;
                color: white;
            }
    
            .box {
                position: relative;
                top: 100px;
    
                width: 600px;
                height: 400px;
                margin: 0 auto;
                background-color: pink;
    
                overflow: hidden;
            }
    
            .box img {
                height: 400px;
                width: 600px;
            }
    
            .box ul {
                display: flex;
                position: absolute;
                top: 0px;
                left: 0px;
            }
    
            .arrow-l,
            .arrow-r {
                display: none;
                position: absolute;
                top: 50%;
                margin-top: -20px;
                width: 24px;
                height: 40px;
                background: rgba(0, 0, 0, .3);
                text-align: center;
                line-height: 40px;
                color: #fff;
                font-family: 'icomoon';
                font-size: 18px;
                z-index: 2;
            }
    
            .arrow-r {
                right: 0;
            }
    
    
            .circle {
                display: flex;
                position: absolute;
                bottom: 10px;
                left: 50px;
            }
    
            .current {
                background-color: #fff;
            }
    
            .circle li {
                /* float: left; */
                width: 20px;
                height: 20px;
                border: 2px solid rgba(255, 255, 255, 0.5);
                margin: 0 3px;
                border-radius: 50%;
                /*鼠标经过显示小手*/
                cursor: pointer;
            }
    
            .current {
                background-color: #fff;
            }
        style>
    
    
        <script>
    
            window.addEventListener('load', function () {
                // 封装动画函数
                function animate(obj, target, callback) {
                    // 先清除以前的定时器,只保留当前的一个定时器执行
                    clearInterval(obj.timer);
                    obj.timer = setInterval(function () {
                        // 步长值写到定时器的里面
                        // 把我们步长值改为整数 不要出现小数的问题
                        // var step = Math.ceil((target - obj.offsetLeft) / 10);
                        var step = (target - obj.offsetLeft) / 10;
                        step = step > 0 ? Math.ceil(step) : Math.floor(step);
                        if (obj.offsetLeft == target) {
                            // 停止动画 本质是停止定时器
                            clearInterval(obj.timer);
                            // 回调函数写到定时器结束里面
                            // if (callback) {
                            //     // 调用函数
                            //     callback();
                            // }
                            callback && callback();
                        }
                        // 把每次加1 这个步长值改为一个慢慢变小的值  步长公式:(目标值 - 现在的位置) / 10
                        obj.style.left = obj.offsetLeft + step + 'px';
                    }, 15);
                }
    
                //轮播图
                var arrow_l = document.querySelector('.arrow-l');
                var arrow_r = document.querySelector('.arrow-r');
                var box = document.querySelector('.box');
    
                box.addEventListener('mouseenter', function () {
                    arrow_l.style.display = 'block';
                    arrow_r.style.display = 'block';
                    clearInterval(timer);
                    timer = null; // 清除定时器变量
                })
    
                box.addEventListener('mouseleave', function () {
                    arrow_l.style.display = 'none';
                    arrow_r.style.display = 'none';
                    timer = setInterval(function () {
                        //手动调用点击事件
                        arrow_r.click();
                    }, 2000);
                })
    
                // 动态生成小圆圈 有几张图片 就生成几张小圆圈
                var ul = document.querySelector('.ul');
                var ol = document.querySelector('.circle')
    
                for (var i = 0; i < ul.children.length; i++) {
                    // 创建li 插入ol
                    var li = document.createElement('li');
                    ol.appendChild(li);
                    //自定义属性 记录索引号
                    li.setAttribute('data-index', i);
    
                    //li设置类
                    li.addEventListener('click', function () {
                        for (var i = 0; i < ol.children.length; i++) {
                            ol.children[i].className = '';
                        }
                        this.className = 'current';
    
                        // 点击小圆圈滚动图片
                        // 得到宽度
                        var width = box.offsetWidth;
                        var index = this.getAttribute('data-index');
                        num = index;
                        circle = index;
                        // 步长
                        var step = index * width;
                        // 调用函数
                        animate(ul, - step);
    
                    })
                }
                // 设置第一张图片为current
                ol.children[0].className = 'current';
    
                // 克隆第一张图片li放在ul最后面
                var first = ul.children[0].cloneNode(true);
                ul.appendChild(first);
    
    
                // 节流阀 flag
                var flag = true;
    
                // 点击按钮 滑动图片 无缝滚动
                var num = 0;
                // 控制小圆圈播放
                var circle = 0;
                arrow_r.addEventListener('click', function () {
                    if (flag) {
                        // 关闭节流阀
                        flag = false;
    
                        if (num == ul.children.length - 1) {
                            ul.style.left = 0;
                            num = 0;
                        }
                        num++;
                        animate(ul, -num * (box.offsetWidth), function () {
                            flag = true;
                        });
    
                        // 点击一次 小圆圈播放也加1
                        circle++;
                        if (circle == ul.children.length - 1) {
                            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 (flag) {
                        if (num == 0) {
                            num = ol.children.length;
                        }
                        num--;
                        animate(ul, -num * (box.offsetWidth), function () {
                            flag = true;
                        });
    
                        // 点击一次 小圆圈播放-1
                        circle--;
                        if (circle < 0) {
                            circle = ol.children.length - 1;
                        }
                        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);
    
            })
    
        script>
    
    head>
    
    <body>
    
        <div class="box">
            
            <a href="javascript:;" class="arrow-l">a>
            
            <a href="javascript:;" class="arrow-r">a>
    
            <ul class="ul">
                <li>
                    <a href="#"><img src="upload/focus.jpg" alt="">a>
                li>
                <li>
                    <a href="#"><img src="upload/focus1.jpg" alt="">a>
                li>
                <li>
                    <a href="#"><img src="upload/focus2.jpg" alt="">a>
                li>
                <li>
                    <a href="#"><img src="upload/focus3.jpg" alt="">a>
                li>
            ul>
    
            
            <ol class="circle">
                
            ol>
    
        div>
    
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
  • 相关阅读:
    【左神算法笔记】Class1:异或交换,时间复杂度计算
    Node.js 事件循环和事件派发器
    Android自动化测试32--Appium Desktop
    Windows操作系统基础-第05课-DHCP介绍与安装
    神经网络的图像识别技术,人工神经网络图像识别
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    Python-数据爬取(爬虫)
    python的破解重构加密
    如何检测租用的香港服务器是不是CN2线路呢?
    HBase原理深入
  • 原文地址:https://blog.csdn.net/qq_43472877/article/details/126143320