<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
#box {
width: 400px;
height: 500px;
border: 1px solid red;
position: relative;
margin: 50px auto;
}
#block {
width: 100px;
height: 30px;
position: absolute;
left: 0;
top: 400px;
background-color: red;
}
</style>
<body>
<div id="box">
<div id="block"></div>
</div>
</body>
<script>
var oBlock = document.querySelector('#block');
var oBox = document.querySelector('#box');
var maxLeft = oBox.clientWidth - oBlock.offsetWidth;
oBlock.onmousedown = function (e) {
var ev = event || e;
var startX = ev.x;
document.onmousemove = function (e) {
var ev = event || e;
var endX = ev.x;
var left = endX - startX;
if (left <= 0) {
left = 0;
}
if (left >= maxLeft) {
left = maxLeft;
}
oBlock.style.left = left + 'px';
}
document.onmouseup = function (e) {
document.onmousemove = null;
if (oBlock.offsetLeft === maxLeft) {
location.href = 'http://www.baidu.com';
} else {
oBlock.style.left = 0 + 'px';
}
}
}
</script>