function Drag_Class(id) ...{ this.id = id; } Drag_Class.prototype.startDrag =function(ev) ...{ var obj = $(this.id).parentNode; obj.style.zIndex +=100; var ev=ev||window.event; this.mouse_x = ev.clientX||ev.pageX; this.mouse_y = ev.clientY||ev.pageY; this.obj_x = parseInt(obj.style.left.replace("px","")); this.obj_y = parseInt(obj.style.top.replace("px","")); this.moveable =true; } Drag_Class.prototype.Drag =function(ev) ...{ var obj = $(this.id).parentNode; var ev=ev||window.event; var ev_x = ev.clientX||ev.pageX; var ev_y = ev.clientY||ev.pageY; if (this.moveable) ...{ obj.style.left =this.obj_x + ev_x -this.mouse_x +"px"; obj.style.top =this.obj_y + ev_y -this.mouse_y +"px"; } } Drag_Class.prototype.stopDrag =function() ...{ if (this.moveable) ...{ var obj = $(this.id).parentNode; this.moveable =false; } }
Base.js文件:
function $(str) ...{ return document.getElementById(str); } function addEvent(elm, evType, fn, useCapture)...{ if (elm.addEventListener)...{ elm.addEventListener(evType, fn, useCapture); returntrue; }elseif (elm.attachEvent)...{ var r = elm.attachEvent("on"+evType, fn); return r; }else...{ alert("Handler could not be removed"); } }