Javascript eine Drag-Klasse
Autor:Eve Cole
Aktualisierungszeit:2009-06-11 16:31:49
<HTML>
<KOPF>
<SCRIPT LANGUAGE="JavaScript">
<!--
var Drag = {
sx : 0,
sy: 0,
Beispiel: 0,
ey: 0,
lx : 0,
ly: 0,
offsetX : 0,
offsetY : 0,
cdom: null,
DragAble: falsch,
DragFun: null,
tempDrag: function(){
Drag.drag.call(Drag);
},
Cache: {},
putCache: function(key,v){
this.cache[key] = v;
},
getCache: Funktion(Schlüssel){
return this.cache[key];
},
dragStart : function(dfun,fn){
versuchen{
if(!this.dragAble){
this.cdom = event.srcElement?event.srcElement:event.target;
this.cdom.setCapture(true);
this.sx = event.screenX;
this.sy = event.screenY;
this.ex = event.screenX;
this.ey = event.screenY;
this.lx = event.screenX;
this.ly = event.screenY;
this.dragAble = true;
this.dragFun = dfun?dfun:null;
if(fn){fn();}
this.cdom.attachEvent("onmousemove",Drag.tempDrag);
}
}catch(e){
this.dragEnd();
}
},
ziehen: function(){
versuchen{
if(this.dragAble){
this.ex = event.screenX;
this.ey = event.screenY;
this.offsetX = this.ex - this.lx;
this.offsetY = this.ey - this.ly;
if(this.dragFun){this.dragFun();}
this.lx = this.ex;
this.ly = this.ey;
}
}
fangen(e){
this.dragEnd(null);
}
},
dragEnd : function(fn){
if(this.dragAble){
this.cdom.detachEvent("onmousemove",Drag.tempDrag);
this.cdom.releaseCapture();
this.ex = event.screenX;
this.ey = event.screenY;
this.dragAble = false;
if(fn){fn();}
}
this.releaseDrag();
},
releaseDrag : function(){
this.ex = 0;
this.ey = 0;
this.sx = 0;
this.sy = 0;
this.cdom = null;
this.dragAble = false;
this.dragFun = null;
this.cache = {};
}
};
Funktion start(){
var d = Drag.cdom.cloneNode(true);
var pos = getBodyPos(Drag.cdom);
with(d.style){
position="absolut";
left=pos.left;
top=pos.top;
filter='Alpha(Opacity="30")' ;
}
Drag.putCache("alo",d);
document.body.appendChild(d);
}
Funktion ing(){
var offx = Drag.offsetX;
var offy = Drag.offsetY;
var to = Drag.getCache("alo");
var l = to.offsetLeft;
var t = to.offsetTop;
with(to.style){
links=l+offx;
top=t+offy;
}
}
Funktion end(){
var cdom = Drag.cdom;
var alo = Drag.getCache("alo");
mit(cdom.style)
{
left=alo.offsetLeft;
top=alo.offsetTop;
position="absolut";
}
wenn(auch)
document.body.removeChild(alo);
}
Funktion getBodyPos(obj){
versuchen{
var l = obj.offsetLeft;
var t = obj.offsetTop;
while(obj.parentElement != document.body){
obj = obj.parentElement;
var tl = obj.offsetLeft;
var tt = obj.offsetTop;
l += tl;
t += tt;
}
return {left:l,top:t};
}catch(e){
alarm(e.message);
}
}
//-->
</SCRIPT>
</HEAD>
<KÖRPER>
<div style="border:solid 1 #7FFFD4;width:300;height:150;background:#3399FF;cursor:move;" onmousedown="Drag.dragStart(ing,start)" onmouseup="Drag.dragEnd(end)">
Drag-Objekt testen
</div>
</BODY>
</HTML>