本文为大家详细介绍下jQuery拖曵的简单实例
具体的实现思路及代码如下
感兴趣的朋友可以参考下哈
希望对大家有所帮助
复制代码 代码如下:
<!DOCTYPE html>
<html>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>简单拖曵原理实例</title>
<style type="text/css">
#drag{width:px;height:px;background:url:move;position:absolute;top:px;left:px;border:solid px #ccc;}
h{color:#fff;background: none repeat scroll rgba( );color:#FFFFFF;height:px;lineheight:px;margin:;}
</style>
<script src="
<script type="text/javascript">
$(function(){
/*拖曳效果
*原理标记拖曳状态dragging 坐标位置iX iY
* mousedown:fn(){dragging = true 记录起始坐标位置设置鼠标捕获}
* mouseover:fn(){判断如果dragging = true 则当前坐标位置 记录起始坐标位置绝对定位的元素获得差值}
* mouseup:fn(){dragging = false 释放鼠标捕获防止冒泡}
*/
var dragging = false;
var iX iY;
$("#drag")mousedown(function(e) {
dragging = true;
iX = eclientX thisoffsetLeft;
iY = eclientY thisoffsetTop;
thissetCapture && thissetCapture();
return false;
});
documentonmousemove = function(e) {
if (dragging) {
var e = e || windowevent;
var oX = eclientX iX;
var oY = eclientY iY;
$("#drag")css({"left":oX + "px" "top":oY + "px"});
return false;
}
};
$(document)mouseup(function(e) {
dragging = false;
$("#drag")[]releaseCapture();
ecancelBubble = true;
})
})
</script>
</head>
<body>
<div id="drag">
<h>来拖动我啊</h>
</div>
</body>
</html>