Description
CDN Search
75CDN jsDelivr
<!DOCTYPE html>
<html>

<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta charset="utf-8" />
	<meta name="referrer" content="never">
	<title>拖动或点击保持进度占比一致</title>

	<link href="https://lib.baomitu.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://lib.baomitu.com/jquery/1.12.4/jquery.js"></script>
</head>

<body>

	<div class="container">

		<h2>拖动或点击保持进度占比一致</h2>

		<div class="row">
			<div class="col-xs-8">
				<div class="sh">
					<div class="shtag"></div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="sh">
					<div class="shtag"></div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-4">
				<div class="sh">
					<div class="shtag"></div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-6">
				<div class="sh">
					<div class="shtag"></div>
				</div>
			</div>
		</div>
	</div>
</body>

</html>
function getMousePos(event) {
    var e = event || window.event;
    var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    var x = e.pageX || e.clientX + scrollX;
    var y = e.pageY || e.clientY + scrollY;
    return { 'x': x, 'y': y };
}

function tip(ratio) {
    ratio = parseFloat(ratio.replace('%', '')).toFixed(2);
    return '<div class="shtip">' + ratio + '%</div>';
}

$('div.sh').click(function (e) {
    var pos = getMousePos(e);
    var offset = $(this).offset();
    var allw = $(this).width();
    var beforew = pos.x - offset.left;
    var ratio = beforew / allw;
    ratio < 0 && (ratio = 0);
    ratio > 1 && (ratio = 1);
    ratio = ratio * 100 + "%";
    $('div.sh').children().animate({ left: ratio }, 400).html(tip(ratio));
});

$('div.shtag').mousedown(function (e) {
    var that = this;
    var pos1 = getMousePos(e);
    var sx = parseInt($(that).css('left'));
    var allw = $(that).parent().width();
    document.onmousemove = function (e) {
        var pos2 = getMousePos(e);
        var mx = pos2.x - pos1.x;

        var cx = sx + mx;
        var ratio = cx / allw;
        ratio < 0 && (ratio = 0);
        ratio > 1 && (ratio = 1);
        ratio = ratio * 100 + "%";
        $('div.sh').children().css('left', ratio).html(tip(ratio));

        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
            this.releaseCapture && this.releaseCapture()
        }
        this.setCapture && this.setCapture();
        return false;
    }
}).on('click dblclick', function (e) {
    if (e && e.stopPropagation) {
        e.stopPropagation()
    } else {
        window.event.cancelBubble = true
    }
});
.row {
        margin-top: 80px;
    }

    .sh {
        position: relative;
        height: 8px;
        cursor: pointer;
        background-color: #ddd;
        border: 1px solid transparent;
    }

    .shtag {
        position: absolute;
        left: 0;
        top: -7px;
        width: 20px;
        height: 20px;
        color: white;
        cursor: move;
        border-radius: 20px;
        background-color: blue;
        border: 1px solid transparent;
    }

    .shtip {
        position: absolute;
        color: white;
        bottom: 25px;
        padding: 5px 10px;
        border-radius: 3px;
        background-color: #000000;
    }