javascript 关于事件问题的请教

<html><head><title></title></head><body><scriptlanguage="javascript">var$=function(id... <html>
<head>
<title>
</title>
</head>
<body>
<script language="javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
function removeHandle(ob,type,hand){
ob.removeEventListener(type,hand,false);
}

var bindEvent = function(obj,hand){
return function(ev){
hand.apply(obj,arguments);
}
}
function Divm(idname){
if(typeof idname == "string"){
this.init(idname)
}

}
Divm.prototype={
init:function(idname){
this.divm = $(idname);
this._x = this._y=0;
this._move = bindEvent(this,this.move)
this._stop = removeHandle(this.divm,this.stop)
if(this.divm.addEventListener){
this.divm.addEventListener("mousedown",bindEvent(this,this.start),false);
}
},
start:function(ev){
this._x = ev.clientX - this.divm.offsetLeft;
this._y = ev.clientY - this.divm.offsetTop;
if(this.divm.addEventListener){
this.divm.addEventListener("mousemove",bindEvent(this,this.move),false)
}

},
move:function(ev){
this.divm.style.left = ev.clientX - this._x + "px";
this.divm.style.top = ev.clientY - this._y + "px";
if(this.divm.addEventListener){
this.divm.addEventListener("mouseup",bindEvent(this,this.stop),false)
}
},
stop:function(){
if(this.divm.addEventListener){
removeHandle(this.divm,"mousemove",this.move);
removeHandle(this.divm,"mouseup",this.stop)
// document.removeEventListener(this.divm,"mousemove",this.move)
// document.removeEventListener(this.divm,"mouseup",this.stop)
}

}

}
</script>
<div id="divtest" style ="position:absolute; visibility:visible; left: 0px; top: 0px;">
<table width="200" height="130" border="1" bordercolor="#00FF00">
<tr>
<td height="17" bgcolor="#EC7237"> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</div>
<script>
var dddd = new Divm("divtest");
</script>
</body>
</html>

代码里有个可拖动DIV ,当我松开鼠标的时候,我想注销鼠标拖动事件,代码有问题,高手们帮我看看,应该怎么改一下?
只支持火狐的
11!
展开
 我来答
淡梦桐4x
推荐于2016-02-09
知道答主
回答量:12
采纳率:0%
帮助的人:0
展开全部
你的方法不对,

var bindEvent = function(obj,hand){
return function(ev){
hand.apply(obj,arguments);
}
}
每次产生的都是一个新的函数,而你又没有对这个函数进行其他的引用,所以之前绑定的函数无法解除绑定。(即使函数功能完全一样)。

拖动效果的mousemove mouseup 要绑定在document上:

<html>
<head>
<title></title>
</head>
<body>
<script language="javascript">
var Drag = {

obj : null,

init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
{
o.onmousedown = Drag.start;

o.hmode = bSwapHorzRef ? false : true ;
o.vmode = bSwapVertRef ? false : true ;

o.root = oRoot && oRoot != null ? oRoot : o ;

if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left = "0px";
if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = "0px";
if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

o.minX = typeof minX != 'undefined' ? minX : null;
o.minY = typeof minY != 'undefined' ? minY : null;
o.maxX = typeof maxX != 'undefined' ? maxX : null;
o.maxY = typeof maxY != 'undefined' ? maxY : null;

},

start : function(e)
{
var o = Drag.obj = this;
e = Drag.fixE(e);
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );

o.lastMouseX = e.clientX;
o.lastMouseY = e.clientY;

if (o.hmode) {
if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
} else {
if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
}

if (o.vmode) {
if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
} else {
if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
}

document.onmousemove = Drag.drag;
document.onmouseup = Drag.end;

return false;
},

drag : function(e)
{
e = Drag.fixE(e);
var o = Drag.obj;
var ey = e.clientY;
var ex = e.clientX;
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
var nx, ny;
if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
Drag.obj.lastMouseX = ex;
Drag.obj.lastMouseY = ey;
return false;
},

end : function()
{
document.onmousemove = null;
document.onmouseup = null;
Drag.obj = null;
},

fixE : function(e)
{
if (typeof e == 'undefined') e = window.event;
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
return e;
}
};

</script>
<div id="divtest" style ="position:absolute; visibility:visible; left: 0px; top: 0px;">
<table width="200" height="130" border="1" bordercolor="#00FF00">
<tr>
<td height="17" bgcolor="#EC7237"></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</div>
<script>
Drag.init(document.getElementById('divtest'));

</script>
</body>
</html>
百度网友dc81944bb
2009-10-31 · TA获得超过572个赞
知道小有建树答主
回答量:793
采纳率:0%
帮助的人:723万
展开全部
ob.removeEventListener(type,hand,false);
你这里就有问题..开始报错了.但你的代码太长了.没太看明白...
等有空给你看看.先留个位
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式