addEvent:function(oTarget,name,funName){
var args = Array.prototype.slice.call(arguments).slice(3);
var regHandler=funName
if(args.length>0){
regHandler=function(e){
return funName.apply(this,[e||window.event].concat(args));
}//这里的regHandler产生了个闭包,一直在堆栈中存在,该怎么解决呢?
}
if(oTarget.addEventListener){
oTarget.addEventListener(name,regHandler,false);
}
else{
oTarget.attachEvent("on"+name,regHandler);
}
},
//this.addEvent(document,"mousemove",this.bindEvent(this,this.RightDownMouseMove));
//this.addEvent(document,"mouseup",this.bindEvent(this,this.RightDownMouseUp));
//这两个换成下面注释中的就可以正常运行,如果不换成注释的mouseup总是释放不了。是什么原因呢?
var Resize=Class.create();
Resize.prototype={
init:function(content,cut,cutImg){
this.content=content;
this.cut=cut;
this.cutImg=$(cutImg);
},
Set:function(funCase){
var fun;
switch(funCase){
case "RightDown":
fun=this.RightDownMouseDown;
this._obj=$(funCase);
break;
default:break;
}
this.addEvent(this._obj,"mousedown",this.bindEvent(this,fun))
},
RightDownMouseDown:function(){
var e=arguments[0]
this.para={};
this.para.mouseX=e.clientX;
this.para.mouseY=e.clientY;
this.para.cutWidth=this.cut.offsetWidth;
this.para.cutHeight=this.cut.offsetHeight;
this.addEvent(document,"mousemove",this.bindEvent(this,this.RightDownMouseMove));
this.addEvent(document,"mouseup",this.bindEvent(this,this.RightDownMouseUp));
// var handler = this;
// document.onmousemove=function(e){
// e=e||window.event;
// handler.RightDownMouseMove(e);
// }
// document.onmouseup=function(){
// document.onmousemove=null;
// document.onmouseup=null;
// }
},
RightDownMouseMove:function(){
var e=arguments[0]
e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
this.cut.style.width=e.clientX-this.para.mouseX+this.para.cutWidth+"px";
this.cut.style.height=e.clientY-this.para.mouseY+this.para.cutHeight+"px";
var rectTop=(parseInt(this.cut.style.top)+1)+"px";//加1边框
var rectRight=(parseInt(this.cut.style.left)+parseInt(this.cut.style.width)+1)+"px";
var rectBottom=(parseInt(this.cut.style.top)+parseInt(this.cut.style.height)+1)+"px";
var rectLeft=(parseInt(this.cut.style.left))+"px";
this.cutImg.style.clip="rect("+rectTop+" "+rectRight+" "+rectBottom+" "+rectLeft+")";
},
RightDownMouseUp:function(){
this.deleteEvent(document,"mousemove",this.RightDownMouseMove);
this.deleteEvent(document,"mouseup",this.RightDownMouseUp);
},
bindEvent:function(obj,fun){
return function(e){
return fun.apply(obj,[e||window.event]);
}
},
addEvent:function(oTarget,name,funName){
if(oTarget.addEventListener) {
oTarget.addEventListener(name,funName,false);
}
else if(oTarget.attachEvent) {
oTarget.attachEvent("on" +name,funName);
}
else{
oTarget["on" + name] = funName;
}
},
deleteEvent:function(oTarget,name,funName){
if(oTarget.removeEventListener) {
oTarget.removeEventListener(name,funName,false);
}
else if(oTarget.detachEvent) {
oTarget.detachEvent("on" + name,funName);
}
else{
oTarget["on" + name] = null;
}
}
}