jq 可拖动进度条

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;" />
    <title>Document</title>
    <style>
        .progress {
            position: relative;
            width: 196px;
            margin: 100px auto;
            z-index: 4;

        }

        .progress_bg {
            height: 8px;
            background: rgba(242, 242, 242, 1);
            border-radius: 4px;
        }

        .progress_bar {
            background: red;
            width: 0;
            height: 10px;
            border-radius: 5px;
        }

        .progress_btn {
            width: 20px;
            height: 20px;
            border-radius: 5px;
            position: absolute;
            background: #fff;
            left: 0px;
            margin-left: -10px;
            top: -5px;
            cursor: pointer;
            border: 1px #ddd solid;
            box-sizing: border-box;
            z-index: 1;
        }

    </style>
</head>

<body>
    <div class="progress">
        <div class="progress_bg">
            <div class="progress_bar"></div>
        </div>
        <div class="progress_btn"></div>
        <div class="text">0%</div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        $(function () {
            var tag = false, ox = 0, left = 0, bgleft = 0;
            //pc端
            // $('.progress_btn').mousedown(function (e) {
            //     ox = e.pageX - left;
            //     tag = true;
            // });

            // $(document).mouseup(function () {
            //     tag = false;
            // });

            // $('.progress').mousemove(function (e) {//鼠标移动
            //     if (tag) {
            //         left = e.pageX - ox;
            //         if (left <= 0) {
            //             left = 0;
            //         } else if (left > 196) {
            //             left = 196;
            //         }
            //         $('.progress_btn').css('left', left);
            //         $('.progress_bar').width(left);
            //         $('.text').html(parseInt((left / 196) * 100) + '%');
            //     }
            // });

            // $('.progress_bg').click(function (e) {//鼠标点击
            //     if (!tag) {
            //         bgleft = $('.progress_bg').offset().left;
            //         left = e.pageX - bgleft;
            //         if (left <= 0) {
            //             left = 0;
            //         } else if (left > 196) {
            //             left = 196;
            //         }
            //         $('.progress_btn').css('left', left);
            //         $('.progress_bar').animate({ width: left }, 196);
            //         $('.text').html(parseInt((left / 196) * 100) + '%');
            //     }
            // });
            //移动端
            $('.progress_btn').on('touchstart', function (e) {
                ox = e.originalEvent.targetTouches[0].pageX - left;
                tag = true;
            });
            $(document).on('touchend', function () {
                tag = false;
            });
            $('.progress').on('touchmove', function (e) {
                if (tag) {
                    left = e.originalEvent.targetTouches[0].pageX - ox;
                    if (left <= 0) {
                        left = 0;
                    } else if (left > 196) {
                        left = 196;
                    }
                    $('.progress_btn').css('left', left);
                    $('.progress_bar').width(left);
                    $('.text').html(parseInt((left / 196) * 100) + '%');
                }
            });
            $('.progress_bg').on('touchend', function (e) {
                if (!tag) {
                    bgleft = $('.progress_bg').offset().left;
                    left = e.originalEvent.changedTouches[0].pageX - bgleft;
                    if (left <= 0) {
                        left = 0;
                    } else if (left > 196) {
                        left = 196;
                    }
                    $('.progress_btn').css('left', left);
                    $('.progress_bar').animate({ width: left }, 196);
                    $('.text').html(parseInt((left / 196) * 100) + '%');
                }
            });
        });
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值