• 手写一个移动端进度条


    HTML:

    <input type="range" />
    
    • 1

    CSS:

    /*横条样式*/
    input[type='range'] {
        -webkit-appearance: none; /* 清除系统默认样式 */
        background: linear-gradient(to right, #f00, #f00) no-repeat, #000; /*设置背景渐变色*/
        background-size: 50% 100%; /* 设置左右宽度比例 */
        width: 100%;
        height: 20px;
        border-radius: 10px;
        transition: height 0.3s, border-radius 0.3s;
    }
    input[type='range'].active {
        height: 30px;
        border-radius: 15px;
    }
    input[type='range'].dragging {
        height: 40px;
        border-radius: 20px;
    }
    
    /*拖动块的样式*/
    input[type='range']::-webkit-slider-thumb {
        -webkit-appearance: none; /* 清除系统默认样式 */
        height: 26px;
        width: 26px;
        background: #fff;
        border-radius: 50%;
        border: solid 1px #ddd;
        transition: height 0.3s, width 0.3s;
    }
    input[type='range'].active::-webkit-slider-thumb {
        height: 38px;
        width: 38px;
        border: solid 1px #f00;
    }
    input[type='range'].dragging::-webkit-slider-thumb {
        height: 50px;
        width: 50px;
        border: solid 1px #f00;
    }
    
    • 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

    JS:

    const input = document.querySelector('input');
    const activeTimer = (() => {
        let timer = null;
        return {
            clear: () => {
                if (timer) {
                    clearTimeout(timer);
                    timer = null;
                }
            },
            set: () => {
                timer = setTimeout(() => {
                    input.classList.remove('active');
                }, 2000);
            },
        };
    })();
    
    input.addEventListener('input', event => {
        const target = event.target;
        const value = target.value;
        console.log('target value', value);
        target.style.backgroundSize = `${value}% 100%`;
    });
    
    input.addEventListener('touchstart', event => {
        const target = event.target;
        target.classList.add('active');
    
        activeTimer.clear();
        activeTimer.set();
    });
    
    input.addEventListener('touchmove', event => {
        activeTimer.clear();
    
        const target = event.target;
        target.classList.remove('active');
        target.classList.add('dragging');
    });
    
    input.addEventListener('touchend', event => {
        const target = event.target;
        target.classList.remove('dragging');
        target.classList.add('active');
    
        activeTimer.clear();
        activeTimer.set();
    });
    
    • 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

  • 相关阅读:
    网络安全(黑客)自学
    【深入浅出 Yarn 架构与实现】5-3 Yarn 调度器资源抢占模型
    Dell R720\R720xd\R730\R730xd等iDRAC风扇调速
    C++ Memory Order 理解
    Android连载43-Netd相关学习笔记
    手机端布局页面写法
    i.MX6ULL驱动开发 | 31 - Linux内核网络设备驱动框架
    【MySql】mysql之连接查询和存储过程
    Ajax中XMLHttpRequest
    利用Python创作热力图
  • 原文地址:https://blog.csdn.net/Superman_H/article/details/133365066