drag.js 3.0 KB

12345678910111213141516171819202122
  1. ;(function($,window,document,undefined){var Drag=function(ele,opt){this.$ele=ele,this.x=0,this.y=0,this.defaults={parent:'parent',randomPosition:true,direction:'all',handler:false,dragStart:function(x,y){},dragEnd:function(x,y){},dragMove:function(x,y){}},this.options=$.extend({},this.defaults,opt)}
  2. Drag.prototype={run:function(){var $this=this;var element=this.$ele;var randomPosition=this.options.randomPosition;var direction=this.options.direction;var handler=this.options.handler;var parent=this.options.parent;var isDown=false;var fun=this.options;var X=0,Y=0,moveX,moveY;element.find('*').not('img').mousedown(function(e){e.stopPropagation();});if(parent=='parent'){parent=element.parent();}else{parent=element.parents(parent);}
  3. if(!handler){handler=element;}else{handler=element.find(handler);}
  4. parent.css({position:'relative'});element.css({position:'absolute'});var boxWidth=0,boxHeight=0,sonWidth=0,sonHeight=0;initSize();if(randomPosition){randomPlace();}
  5. $(window).resize(function(){initSize();if(randomPosition){randomPlace();}});function initSize(){boxWidth=parent.outerWidth();boxHeight=parent.outerHeight();sonWidth=element.outerWidth();sonHeight=element.outerHeight();}
  6. function randomPlace(){if(randomPosition){var randX=parseInt(Math.random()*(boxWidth-sonWidth));var randY=parseInt(Math.random()*(boxHeight-sonHeight));if(direction.toLowerCase()=='x'){element.css({left:randX});}else if(direction.toLowerCase()=='y'){element.css({top:randY});}else{element.css({left:randX,top:randY});}}}
  7. handler.css({cursor:'move'}).mousedown(function(e){isDown=true;X=e.pageX;Y=e.pageY;$this.x=element.position().left;$this.y=element.position().top;element.addClass('on');fun.dragStart(parseInt(element.css('left')),parseInt(element.css('top')));return false;});$(document).mouseup(function(e){fun.dragEnd(parseInt(element.css('left')),parseInt(element.css('top')));element.removeClass('on');isDown=false;});$(document).mousemove(function(e){moveX=$this.x+e.pageX-X;moveY=$this.y+e.pageY-Y;function thisXMove(){if(isDown==true){element.css({left:moveX});}else{return;}
  8. if(moveX<0){element.css({left:0});}
  9. if(moveX>(boxWidth-sonWidth)){element.css({left:boxWidth-sonWidth});}
  10. return moveX;}
  11. function thisYMove(){if(isDown==true){element.css({top:moveY});}else{return;}
  12. if(moveY<0){element.css({top:0});}
  13. if(moveY>(boxHeight-sonHeight)){element.css({top:boxHeight-sonHeight});}
  14. return moveY;}
  15. function thisAllMove(){if(isDown==true){element.css({left:moveX,top:moveY});}else{return;}
  16. if(moveX<0){element.css({left:0});}
  17. if(moveX>(boxWidth-sonWidth)){element.css({left:boxWidth-sonWidth});}
  18. if(moveY<0){element.css({top:0});}
  19. if(moveY>(boxHeight-sonHeight)){element.css({top:boxHeight-sonHeight});}}
  20. if(isDown){fun.dragMove(parseInt(element.css('left')),parseInt(element.css('top')));}else{return false;}
  21. if(direction.toLowerCase()=="x"){thisXMove();}else if(direction.toLowerCase()=="y"){thisYMove();}else{thisAllMove();}});}}
  22. $.fn.myDrag=function(options){var drag=new Drag(this,options);drag.run();return this;}})(jQuery,window,document);