代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
input {
width: 30px;
float: left;
}
#bigbox {
border: 1px solid whitesmoke;
height: 22px;
width: 350px;
float: left;
}
.box{
height: 18px;
width: 12px;
background:greenyellow;
position: relative;
border: 1px solid snow;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="bigbox">
<div class="box"></div>
</div>
<input type="text" value="0" class="xs"/>
<div class="cz">
</body>
<script src="dist/jquery.js"></script>
<script>
$(function () {
$(".box").mousedown(function (e) {
var w = e.pageX - $(".box").offset().left;
$(document).mousemove(function (e) {
var l = e.pageX - w;
if (l < 0) {
l = 0;
}
if (l > 350 - 12) {
l = 350 - 12;
}
if (l > $(window).width() - $(".box").width()) {
l = $(window).width() - $(".box").width()
}
$(".box").css({
left: l + "px",
})
$(".xs").val(l);
}).mouseup(function () {
$(document).off('mousemove')
})
})
});
</script>
</html>
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/031070ee145a13c6f72453d8bca8d24e.png)