开封利通水务前端

jquery.drag.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. $.fn.extend({
  2. //---元素拖动插件
  3. dragging:function(data){
  4. var $this = $(this);
  5. var xPage;
  6. var yPage;
  7. var X;//
  8. var Y;//
  9. var xRand = 0;//
  10. var yRand = 0;//
  11. var father = $this.parent().parent();
  12. var defaults = {
  13. move : 'both',
  14. randomPosition : true ,
  15. hander:1
  16. }
  17. var opt = $.extend({},defaults,data);
  18. var movePosition = opt.move;
  19. var random = opt.randomPosition;
  20. var hander = opt.hander;
  21. if(hander === 1){
  22. hander = $this;
  23. }else{
  24. hander = $this.find(opt.hander);
  25. }
  26. //---初始化
  27. // father.css({"position":"relative","overflow":"hidden"});
  28. father.css({"position":"relative"});
  29. $this.css({"position":"absolute"});
  30. hander.css({"cursor":"move"});
  31. var faWidth = father.width();
  32. var faHeight = father.height();
  33. var thisWidth = $this.width()+parseInt($this.css('padding-left'))+parseInt($this.css('padding-right'));
  34. var thisHeight = $this.height()+parseInt($this.css('padding-top'))+parseInt($this.css('padding-bottom'));
  35. var mDown = false;//
  36. var positionX;
  37. var positionY;
  38. var moveX ;
  39. var moveY ;
  40. if(random){
  41. $thisRandom();
  42. }
  43. function $thisRandom(){ //随机函数
  44. $this.each(function(index){
  45. var randY = parseInt(Math.random()*(faHeight-thisHeight));///
  46. var randX = parseInt(Math.random()*(faWidth-thisWidth));///
  47. if(movePosition.toLowerCase() == 'x'){
  48. $(this).css({
  49. left:randX
  50. });
  51. }else if(movePosition.toLowerCase() == 'y'){
  52. $(this).css({
  53. top:randY
  54. });
  55. }else if(movePosition.toLowerCase() == 'both'){
  56. $(this).css({
  57. top:randY,
  58. left:randX
  59. });
  60. }
  61. });
  62. }
  63. hander.mousedown(function(e){
  64. //father.children().css({"zIndex":"0"});
  65. //$this.css({"zIndex":"6666"});
  66. mDown = true;
  67. X = e.pageX;
  68. Y = e.pageY;
  69. positionX = $this.position().left;
  70. positionY = $this.position().top;
  71. return false;
  72. });
  73. $(document).mouseup(function(e){
  74. mDown = false;
  75. });
  76. $(document).mousemove(function(e){
  77. xPage = e.pageX;//--
  78. moveX = positionX+xPage-X;
  79. yPage = e.pageY;//--
  80. moveY = positionY+yPage-Y;
  81. function thisXMove(){ //x轴移动
  82. if(mDown == true){
  83. $this.css({"left":moveX});
  84. }else{
  85. return;
  86. }
  87. if(moveX < 0){
  88. $this.css({"left":"0"});
  89. }
  90. if(moveX > (faWidth-thisWidth)){
  91. $this.css({"left":faWidth-thisWidth});
  92. }
  93. return moveX;
  94. }
  95. function thisYMove(){ //y轴移动
  96. if(mDown == true){
  97. $this.css({"top":moveY});
  98. }else{
  99. return;
  100. }
  101. if(moveY < 0){
  102. $this.css({"top":"0"});
  103. }
  104. if(moveY > (faHeight-thisHeight)){
  105. $this.css({"top":faHeight-thisHeight});
  106. }
  107. return moveY;
  108. }
  109. function thisAllMove(){ //全部移动
  110. if(mDown == true){
  111. $this.css({"left":moveX,"top":moveY});
  112. }else{
  113. return;
  114. }
  115. if(moveX < 0){
  116. $this.css({"left":"0"});
  117. }
  118. if(moveY < 0){
  119. $this.css({"top":"0"});
  120. }
  121. }
  122. if(movePosition.toLowerCase() == "x"){
  123. thisXMove();
  124. }else if(movePosition.toLowerCase() == "y"){
  125. thisYMove();
  126. }else if(movePosition.toLowerCase() == 'both'){
  127. thisAllMove();
  128. }
  129. });
  130. }
  131. });