用jquery实现了
<script> var minDiv = $('.minDiv'); minDiv.on('mousedown', function () { $('html').on('mousemove', div_move); }); $('html').on('mouseup', function () { $('html').off('mousemove', div_move); }); function div_move(e) { var f_off = minDiv.offsetParent(); var f_off_o = minDiv.offsetParent().offset(); if (e.pageX < f_off_o.left) { minDiv.offset({ left: f_off_o.left }); } else if (e.pageX > f_off_o.left + f_off.width()) { minDiv.offset({ left: f_off_o + f_off.width() }); } else { minDiv.offset({ left: e.pageX }); } } </script>