<div class="codetitle"> <span><a style="CURSOR: pointer" data="78043" class="copybut" id="copybut78043" onclick="doCopy('code78043')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code78043"> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <br><title>简单拖曵原理实例</title> <br><style type="text/css"> <BR>#drag{width:400px;height:300px;background:url(http://upload.yxgz.cn/uploadfile/2009/0513/20090513052611873.jpg);cursor:move;position:absolute;top:100px;left:100px;border:solid 1px #ccc;} <BR>h2{color:#fff;background: none repeat scroll 0 0 rgba(16, 90, 31, 0.7);color:#FFFFFF;height:40px;line-height:40px;margin:0;} <BR></style> <br><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <br><script type="text/javascript"> <BR>$(function(){ <BR>/*--------------拖曳效果---------------- <BR>*原理:标记拖曳状态dragging ,坐标位置iX, iY <BR>* mousedown:fn(){dragging = true, 记录起始坐标位置,设置鼠标捕获} <BR>* mouseover:fn(){判断如果dragging = true, 则当前坐标位置 - 记录起始坐标位置,绝对定位的元素获得差值} <BR>* mouseup:fn(){dragging = false, 释放鼠标捕获,防止冒泡} <BR>*/ <BR>var dragging = false; <BR>var iX, iY; <BR>$("#drag").mousedown(function(e) { <BR>dragging = true; <BR>iX = e.clientX - this.offsetLeft; <BR>iY = e.clientY - this.offsetTop; <BR>this.setCapture && this.setCapture(); <BR>return false; <BR>}); <BR>document.onmousemove = function(e) { <BR>if (dragging) { <BR>var e = e || window.event; <BR>var oX = e.clientX - iX; <BR>var oY = e.clientY - iY; <BR>$("#drag").css({"left":oX + "px", "top":oY + "px"}); <BR>return false; <BR>} <BR>}; <BR>$(document).mouseup(function(e) { <BR>dragging = false; <BR>$("#drag")[0].releaseCapture(); <BR>e.cancelBubble = true; <BR>}) <br><br>}) <br><br></script> <br> <br><br> <br><div id="drag"> <br><h2>来拖动我啊</h2> <br> </div> <br> <br> </div>