大家帮忙看一下jquery拖拽的问题 本帖最后由 list8477 于 2010-09-07 17:00:30 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $(this).parent().mousemove(function(event){//拖父元素看看 if(_move){ var x = event.clientX -_x; var y = event.clientY -_y; $('#'+id).css({top:y,left:x}); window.status = 'x:'+x+"\ty:"+y; } }).mouseup(function(){ _move=false; }); 那为什么偏把该行this替换为document呢.无论document还是父元素,属于父元素内部的两个或更多的子元素肯定是要一起动了,必须分开针对每个子元素本身才对. 看下我很久以前写的,,是何等效率...CPU占用率..<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD> <BODY style="height:800px;"> <div id="tee" style="width:200px; height:200px; background:#f00; position:absolute; cursor:move"></div><script type="text/javascript"> var tee=document.getElementById('tee'); tee.onmousedown=function(e){ e=e || event; start(e,this); e.cancelBubble=true; return false; } function start(e,o) { var i, x, y; i=0; x=e.screenX-o.offsetLeft; y=e.screenY-o.offsetTop; if(window.addEventListener) { window.document.addEventListener('mousemove',moving,false) window.document.addEventListener('mouseup',end,false) }else{ window.document.attachEvent('onmousemove',moving) window.document.attachEvent('onmouseup',end) } function moving(e) { if(i++^6)//计数器, 如果i=6 则 i^6==0 return; o.style.left=(e.screenX-x)+'px'; o.style.top=(e.screenY-y)+'px'; i=0; } function end(e) { if(window.addEventListener) { window.document.removeEventListener('mousemove',moving,false); document.removeEventListener('mouseup',arguments.callee,false); }else{ window.document.detachEvent('onmousemove',moving); window.document.detachEvent('onmouseup',arguments.callee); } window.document.body.focus() // ff 3.0 } }// end start </script> </BODY></HTML> 其实拖动取偏移值会更快....这个是仓库里的html页了..忘了把内部函数弄出来了... var popupMove=function(obj){ var mobj=obj.parent(); obj.mousedown(function(e){ if(this.setCapture){ this.setCapture(); }else if(window.captureEvents){ window.captureEvents(Event.mousemove|Event.mouseup); } posX = e.clientX - mobj.offset().left; posY = e.clientY - mobj.offset().top; $(this).mousemove(function(e){ mobj.css({ "left":(e.clientX - posX) + "px", "top":(e.clientY - posY) + "px" }); }) }); obj.mouseup(function(){ if(this.releaseCapture){ this.releaseCapture(); }else if(window.captureEvents){ window.captureEvents(Event.mousemove|Event.mouseup); } $(this).unbind("mousemove"); })} (function($){ $.fn.drag=function(){ var _move = false; var _x,_y; $(this).mousedown(function(event){ _move = true; _x = event.clientX; _y = event.clientY; }); $(this).mousemove(function(event){/*如果该行this替换为document后,拖拽效果很好,但是出现的问题是:当我同时为两个对象添加拖拽动作后,问题就出现了,不论拖动哪一个两个都动。反过来拖拽起来很卡,但是不会两个对象同时都动。帮忙看看到底该怎么处理?[/color]*/ if(_move){ var x = event.clientX -_x; var y = event.clientY -_y; $(this).css({top:y+$(this).position().top,left:x+$(this).position().left}); _x = event.clientX; _y = event.clientY; window.status = 'x:'+x+"\ty:"+y; } }).mouseup(function(){ _move=false; });; } })(jQuery);在你的drag.js基础上改的,你试试。 【求助】JS操作输入框内数值递增递减,在IE浏览器中单击递增或递减按钮偶尔输入框内的值不变。 关于 js 拼接 ,请大侠帮忙 关于输出到另外一个网页的字体很小! 使用XMLHttpRequest调用 java的webservice(使用XFire发布的) 这个js文件该如何解密,向js高手请教 怎样用代码实现保存页面中的<IMG ...>图片到文件 表格添加删除行的操作 请求~~~哪位程序员帮我解决这个小问题? js变量的问题 在標識中插入js語句 闭包里怎么调用对象成员呢??? 急... css问题求解
if(_move){
var x = event.clientX -_x;
var y = event.clientY -_y;
$('#'+id).css({top:y,left:x});
window.status = 'x:'+x+"\ty:"+y;
}
}).mouseup(function(){
_move=false;
});
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD> <BODY style="height:800px;">
<div id="tee" style="width:200px; height:200px; background:#f00; position:absolute; cursor:move"></div>
<script type="text/javascript">
var tee=document.getElementById('tee');
tee.onmousedown=function(e){
e=e || event;
start(e,this);
e.cancelBubble=true;
return false;
}
function start(e,o)
{
var i,
x,
y;
i=0;
x=e.screenX-o.offsetLeft;
y=e.screenY-o.offsetTop;
if(window.addEventListener)
{
window.document.addEventListener('mousemove',moving,false)
window.document.addEventListener('mouseup',end,false)
}else{
window.document.attachEvent('onmousemove',moving)
window.document.attachEvent('onmouseup',end) }
function moving(e)
{
if(i++^6)//计数器, 如果i=6 则 i^6==0
return;
o.style.left=(e.screenX-x)+'px';
o.style.top=(e.screenY-y)+'px';
i=0;
} function end(e)
{
if(window.addEventListener)
{
window.document.removeEventListener('mousemove',moving,false);
document.removeEventListener('mouseup',arguments.callee,false);
}else{
window.document.detachEvent('onmousemove',moving);
window.document.detachEvent('onmouseup',arguments.callee); }
window.document.body.focus() // ff 3.0
}
}// end start
</script>
</BODY>
</HTML>
var popupMove=function(obj){
var mobj=obj.parent();
obj.mousedown(function(e){
if(this.setCapture){
this.setCapture();
}else if(window.captureEvents){
window.captureEvents(Event.mousemove|Event.mouseup);
}
posX = e.clientX - mobj.offset().left;
posY = e.clientY - mobj.offset().top;
$(this).mousemove(function(e){
mobj.css({
"left":(e.clientX - posX) + "px",
"top":(e.clientY - posY) + "px"
});
})
});
obj.mouseup(function(){
if(this.releaseCapture){
this.releaseCapture();
}else if(window.captureEvents){
window.captureEvents(Event.mousemove|Event.mouseup);
}
$(this).unbind("mousemove");
})
}
$.fn.drag=function(){
var _move = false;
var _x,_y;
$(this).mousedown(function(event){
_move = true;
_x = event.clientX;
_y = event.clientY;
});
$(this).mousemove(function(event){/*如果该行this替换为document后,
拖拽效果很好,但是出现的问题是:当我同时为两个对象添加拖拽动作后,问
题就出现了,不论拖动哪一个两个都动。反过来拖拽起来很卡,但是不会两个
对象同时都动。帮忙看看到底该怎么处理?[/color]*/
if(_move){
var x = event.clientX -_x;
var y = event.clientY -_y;
$(this).css({top:y+$(this).position().top,left:x+$(this).position().left});
_x = event.clientX;
_y = event.clientY;
window.status = 'x:'+x+"\ty:"+y;
}
}).mouseup(function(){
_move=false;
});;
}
})(jQuery);
在你的drag.js基础上改的,你试试。