<script> function move_div(obj){
obj.mousedown(function (e) { var oe=e||window.event; var $this = this; var startX = oe.clientX - $this.offsetLeft; var startY = oe.clientY - $this.offsetTop; document.onmousemove = function (e) { var oe = e || window.event; $this.style.left = oe.clientX - startX + "px"; $this.style.top = oe.clientY - startY + "px"; }; document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; if ($this.releaseCapture) { $this.releaseCapture(); } }; if ($this.setCapture) { $this.setCapture(); } return false; }); } $(function (){ $('#box').hover( function(){ move_div($(this)) }, function () { }) })</script>
<div id="box" >
<h2 style="text-align: center"> 按住可以拖动</h2>
</div>
</body>
<style type="text/css">
#box {
position:absolute;
width:200px;
height:200px;
border: 1px solid #ddd;
top:100px;
background: #01AAED;
}
</style>
参考大神编写,传入id使其拖动