WEB开发网
开发学院网页设计JavaScript Javascript 一个拖动类 阅读

Javascript 一个拖动类

 2009-05-06 20:12:05 来源:WEB开发网   
核心提示:<HTML> <HEAD> <SCRipT LANGUAGE="javaScript"> <!-- var Drag = { sx : 0, sy : 0, ex : 0, ey : 0, lx : 0, ly : 0, offsetX :
<HTML>
 <HEAD>
  <SCRipT LANGUAGE="javaScript">
  <!--
 var Drag = {
  sx : 0,
  sy : 0,
  ex : 0,
  ey : 0,
  lx : 0,
  ly : 0,
  offsetX : 0,
  offsetY : 0,
  cdom : null,
  dragAble : false,
  dragFun : null,
  tempDrag : function(){
  Drag.drag.call(Drag);
  },
  cache : {},
  putCache : function(key,v){
  this.cache[key] = v;
  },
  getCache : function(key){
  return this.cache[key];
  },
  dragStart : function(dfun,fn){
  try{
   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();
  }
  },
  drag : function(){
  try{
 
   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;
   }
  }
  catch(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 = {};
  }
 };
 
 function start(){
  var d = Drag.cdom.cloneNode(true);
  var pos = getBodyPos(Drag.cdom);
  with(d.style){
  position="absolute";
  left=pos.left;
  top=pos.top;
  filter='Alpha(Opacity="30")' ;
  }
  Drag.putCache("alo",d);
  document.body.appendChild(d);
 
 }
 function 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){
  left=l+offx;
  top=t+offy;
  }
 }
 function end(){
  var cdom = Drag.cdom;
  var alo = Drag.getCache("alo");
  with(cdom.style)
  {
  left=alo.offsetLeft;
  top=alo.offsetTop;
  position="absolute";
  }
  if(alo)
  document.body.removeChild(alo);
 }
 function getBodyPos(obj){
  try{
  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){
  alert(e.message);
  }
 }
  //-->
  </SCRIPT>
 </HEAD>
 <BODY>
  <div style="border:solid 1 #7FFFD4;width:300;height:150;background:#3399FF;cursor:move;" >
  Test Drag Object
  </div>
 </BODY>
</HTML>

Tags:Javascript 一个 拖动

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接