在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享Javascript 一个拖动类,希望可以对大家能有小小的帮助。
【 tulaoshi.com - Web开发 】
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();
  },
来源:http://www.tulaoshi.com/n/20160219/1610572.html
看过《Javascript 一个拖动类》的人还看了以下文章 更多>>