十分钟教你从零开始,动手写一个可拖动的时间进度条

先来个效果图

Image 1.png

1.创建一个时间条

    <span id="start" class="btn">开始</span>
    <span id="pause" class="btn">暂停</span>
    <span id="restart" class="btn">停止</span>
    <div class="progress-bar-out"></div>

2.初始化进度条

<script src="./jquery.min.js"></script>
<script>
    // 创建进度条
    function init(offsetMax, stepMax) { // offsetMax:进度条最大长度,stepMax:最大移动步数
        $('.progress-bar-out').html();
        $('.progress-bar-out').css('width', offsetMax + 'px');
        var offset = 0; // 进度条实际偏移量   
        var step = 0; // 实际移动步数
        var stepLength = offsetMax / stepMax; // 每一步移动多少距离
        var isAction = false; //定时器是是否正在进行
        var time; // 定时器
        var progressBarIn = document.createElement('div'); // 创建内进度条
        progressBarIn.className = "progress-bar-in";
        var progressBarBall = document.createElement('div'); // 创建小圆球
        progressBarBall.className = 'progress-bar-ball';
        progressBarIn.appendChild(progressBarBall);
        $('.progress-bar-out').append(progressBarIn);
        // 重置清空
        function clearAll() {
            offset = 0;
            step = 0;
        };
        // 点击开始
        function start() {
            clearInterval(time);
            isAction = true;
            time = setInterval(function () {
                if (offset >= offsetMax) {
                    offset = offsetMax;
                    step = stepMax;
                    clearInterval(time);
                    isAction = false;
                    return false;
                }
                step++;
                offset += stepLength;
                console.log('定时器的当前步数' + step);
                $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
            }, 100)
        };
        // 点击暂停
        function pause() {
            isAction = false;
            clearInterval(time);
            $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
        };
        // 点击重置
        function restart() {
            isAction = false;
            clearAll();
            clearInterval(time);
            $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
        };
        $('#start').off().on('click', function () {
            start();
        });
        $('#pause').off().on('click', function () {
            pause();
        });
        $('#restart').off().on('click', function () {
            restart();
        });
        var down = false; // 鼠标是否按下
        var lastX = 0; //距离修正
        var NewX = 0; // 小圆球拖动之后的偏移量
        // 小球监听鼠标按下
        $('.progress-bar-out .progress-bar-ball').off().on('mousedown', function (event) {
            lastX = event.pageX - $('.progress-bar-out .progress-bar-ball').offset().left;
            event.preventDefault();
            down = true;
        });
        // 文档监听鼠标抬起
        $(document).off().on('mouseup', function (event) {
            down = false;
            if (event.target.className == 'progress-bar-ball') {
                console.log('鼠标抬起步数' + step)
            }
        });
        $('.progress-bar-out').off().on('mousemove', function (event) {
            if (down) {
                // 限制鼠标移动最右不能超过进度条最右边(进度条偏移量+进度条宽度+距离修正)
                if (event.pageX >= $('.progress-bar-out').offset().left + $('.progress-bar-out').width() +
                    lastX) {
                    event.pageX = $('.progress-bar-out').offset().left + $('.progress-bar-out').width() + lastX
                }
                // 限制鼠标移动最左不能低于进度条最左边(进度条偏移量+距离修正)
                if (event.pageX <= $('.progress-bar-out').offset().left + lastX) {
                    event.pageX = $('.progress-bar-out').offset().left + lastX;
                }
                // 最后移动的距离(鼠标位置-进度条偏移量-距离修正)
                NewX = event.pageX - $('.progress-bar-out').offset().left - lastX;
                console.log('拖动之后的移动距离' + NewX);
                // 上取整获取当前步数
                step = Math.ceil(NewX / stepLength);
                // 根据当前步数,重置进度条偏移距离
                offset = step * stepLength;
                console.log('鼠标移动' + offset);
                console.log('鼠标移动步数' + step);
                $('.progress-bar-out .progress-bar-in').css("width", offset + 'px');
            }

        });
    }
    init(400, 400);
</script>

3.进度条样式

        .progress-bar-out {
            display: inline-block;
            height: 10px;
            background: rgba(3,17,49,0.8);
            border-radius: 5px;
            border: 1px solid #104e80;
        
        }

        .progress-bar-in {
            display: inline-block;
            width: 0px;
            height: 9px;
            border-top-left-radius: 3px;
            border-bottom-left-radius: 3px;
            position: relative;
            background: linear-gradient(to right, #0bb2ff, #98e5f6);
            transition: all 0.03s;
            float: left;
            left: -1px;
        }

        .progress-bar-ball {
            display: inline-block;
            width: 14px;
            height: 14px;
            border-radius: 50%;
            position: absolute;
            right: -14px;
            top: -2px;
            background: #fde020;
            box-shadow: 0 0 15px #fde020;
            cursor: pointer;
        }
        .btn {
            display: inline-block;
            padding:5px 15px;
            float:left;
            border:1px solid #0bb2ff;
            border-radius:5px;
            margin-right:15px;
        }

4.完整代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .progress-bar-out {
            display: inline-block;
            height: 10px;
            background: rgba(3,17,49,0.8);
            border-radius: 5px;
            border: 1px solid #104e80;
        
        }

        .progress-bar-in {
            display: inline-block;
            width: 0px;
            height: 9px;
            border-top-left-radius: 3px;
            border-bottom-left-radius: 3px;
            position: relative;
            background: linear-gradient(to right, #0bb2ff, #98e5f6);
            transition: all 0.03s;
            float: left;
            left: -1px;
        }

        .progress-bar-ball {
            display: inline-block;
            width: 14px;
            height: 14px;
            border-radius: 50%;
            position: absolute;
            right: -14px;
            top: -2px;
            background: #fde020;
            box-shadow: 0 0 15px #fde020;
            cursor: pointer;
        }
        .btn {
            display: inline-block;
            padding:5px 15px;
            float:left;
            border:1px solid #0bb2ff;
            border-radius:5px;
            margin-right:15px;
        }
    </style>
</head>

<body>
    <span id="start" class="btn">开始</span>
    <span id="pause" class="btn">暂停</span>
    <span id="restart" class="btn">停止</span>
    <div class="progress-bar-out">
    </div>
</body>
<script src="./jquery.min.js"></script>
<script>
    // 创建进度条
    function init(offsetMax, stepMax) { // offsetMax:进度条最大长度,stepMax:最大移动步数
        $('.progress-bar-out').html();
        $('.progress-bar-out').css('width', offsetMax + 'px');
        var offset = 0; // 进度条实际偏移量   
        var step = 0; // 实际移动步数
        var stepLength = offsetMax / stepMax; // 每一步移动多少距离
        var isAction = false; //定时器是是否正在进行
        var time; // 定时器
        var progressBarIn = document.createElement('div'); // 创建内进度条
        progressBarIn.className = "progress-bar-in";
        var progressBarBall = document.createElement('div'); // 创建小圆球
        progressBarBall.className = 'progress-bar-ball';
        progressBarIn.appendChild(progressBarBall);
        $('.progress-bar-out').append(progressBarIn);
        // 重置清空
        function clearAll() {
            offset = 0;
            step = 0;
        };
        // 点击开始
        function start() {
            clearInterval(time);
            isAction = true;
            time = setInterval(function () {
                if (offset >= offsetMax) {
                    offset = offsetMax;
                    step = stepMax;
                    clearInterval(time);
                    isAction = false;
                    return false;
                }
                step++;
                offset += stepLength;
                console.log('定时器的当前步数' + step);
                $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
            }, 100)
        };
        // 点击暂停
        function pause() {
            isAction = false;
            clearInterval(time);
            $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
        };
        // 点击重置
        function restart() {
            isAction = false;
            clearAll();
            clearInterval(time);
            $('.progress-bar-out .progress-bar-in').css('width', offset + 'px');
        };
        $('#start').off().on('click', function () {
            start();
        });
        $('#pause').off().on('click', function () {
            pause();
        });
        $('#restart').off().on('click', function () {
            restart();
        });
        var down = false; // 鼠标是否按下
        var lastX = 0; //距离修正
        var NewX = 0; // 小圆球拖动之后的偏移量
        // 小球监听鼠标按下
        $('.progress-bar-out .progress-bar-ball').off().on('mousedown', function (event) {
            lastX = event.pageX - $('.progress-bar-out .progress-bar-ball').offset().left;
            event.preventDefault();
            down = true;
        });
        // 文档监听鼠标抬起
        $(document).off().on('mouseup', function (event) {
            down = false;
            if (event.target.className == 'progress-bar-ball') {
                console.log('鼠标抬起步数' + step)
            }
        });
        $('.progress-bar-out').off().on('mousemove', function (event) {
            if (down) {
                // 限制鼠标移动最右不能超过进度条最右边(进度条偏移量+进度条宽度+距离修正)
                if (event.pageX >= $('.progress-bar-out').offset().left + $('.progress-bar-out').width() +
                    lastX) {
                    event.pageX = $('.progress-bar-out').offset().left + $('.progress-bar-out').width() + lastX
                }
                // 限制鼠标移动最左不能低于进度条最左边(进度条偏移量+距离修正)
                if (event.pageX <= $('.progress-bar-out').offset().left + lastX) {
                    event.pageX = $('.progress-bar-out').offset().left + lastX;
                }
                // 最后移动的距离(鼠标位置-进度条偏移量-距离修正)
                NewX = event.pageX - $('.progress-bar-out').offset().left - lastX;
                console.log('拖动之后的移动距离' + NewX);
                // 上取整获取当前步数
                step = Math.ceil(NewX / stepLength);
                // 根据当前步数,重置进度条偏移距离
                offset = step * stepLength;
                console.log('鼠标移动' + offset);
                console.log('鼠标移动步数' + step);
                $('.progress-bar-out .progress-bar-in').css("width", offset + 'px');
            }

        });
    }
    init(400, 400);
</script>

</html>

评论

暂无评论