<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> <br> <br> <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:moveposition;頂:100px;left:100px;border:solid 1px #ccc;} <BR>h2{color:#fff;background: none repeat scroll 0 0 rgba(16, 90, 31, 0.7);coloright:#FFFFFF; :40px;line-height:40px;margin:0;} <BR></style> <br><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery%20.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 || .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> </div>