drag.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. (function ($, window, document, undefined) {
  2. var Drag = function (ele, opt) {
  3. this.$ele = ele, this.x = 0, this.y = 0, this.defaults = {
  4. parent: 'parent',
  5. randomPosition: true,
  6. direction: 'all',
  7. handler: false,
  8. grid:null,
  9. dragStart: function (x, y) {},
  10. dragEnd: function (x, y) {},
  11. dragMove: function (x, y) {}
  12. }, this.options = $.extend({}, this.defaults, opt)
  13. }
  14. Drag.prototype = {
  15. run: function () {
  16. var $this = this;
  17. var element = this.$ele;
  18. var randomPosition = this.options.randomPosition;
  19. var direction = this.options.direction;
  20. var handler = this.options.handler;
  21. var parent = this.options.parent;
  22. var isDown = false;
  23. var grid = this.options.grid
  24. var fun = this.options;
  25. var X = 0,
  26. Y = 0,
  27. moveX, moveY;
  28. element.find('*').not('img').mousedown(function (e) {
  29. e.stopPropagation();
  30. });
  31. if (parent == 'parent') {
  32. parent = element.parent();
  33. } else {
  34. parent = element.parents(parent);
  35. }
  36. if (!handler) {
  37. handler = element;
  38. } else {
  39. handler = element.find(handler);
  40. }
  41. parent.css({
  42. position: 'relative'
  43. });
  44. element.css({
  45. position: 'absolute'
  46. });
  47. var boxWidth = 0,
  48. boxHeight = 0,
  49. sonWidth = 0,
  50. sonHeight = 0;
  51. initSize();
  52. if (randomPosition) {
  53. randomPlace();
  54. }
  55. $(window).resize(function () {
  56. initSize();
  57. if (randomPosition) {
  58. randomPlace();
  59. }
  60. });
  61. function initSize() {
  62. boxWidth = parent.outerWidth();
  63. boxHeight = parent.outerHeight();
  64. sonWidth = element.outerWidth();
  65. sonHeight = element.outerHeight();
  66. }
  67. function randomPlace() {
  68. if (randomPosition) {
  69. var randX = parseInt(Math.random() * (boxWidth - sonWidth));
  70. var randY = parseInt(Math.random() * (boxHeight - sonHeight));
  71. if (direction.toLowerCase() == 'x') {
  72. element.css({
  73. left: randX
  74. });
  75. } else if (direction.toLowerCase() == 'y') {
  76. element.css({
  77. top: randY
  78. });
  79. } else {
  80. element.css({
  81. left: randX,
  82. top: randY
  83. });
  84. }
  85. }
  86. }
  87. handler.css({
  88. cursor: 'move'
  89. }).mousedown(function (e) {
  90. isDown = true;
  91. X = e.pageX;
  92. Y = e.pageY;
  93. $this.x = element.position().left;
  94. $this.y = element.position().top;
  95. element.addClass('on');
  96. fun.dragStart(parseInt(element.css('left')), parseInt(element.css('top')));
  97. return false;
  98. });
  99. $(document).mouseup(function (e) {
  100. fun.dragEnd(parseInt(element.css('left')), parseInt(element.css('top')));
  101. element.removeClass('on');
  102. isDown = false;
  103. });
  104. $(document).mousemove(function (e) {
  105. moveX = $this.x + e.pageX - X;
  106. moveY = $this.y + e.pageY - Y;
  107. if(grid!==null){
  108. var gx = grid[0];
  109. var gy = grid[1];
  110. moveX= Math.floor((moveX + gx/ 2) / gx) * gx;
  111. moveY= Math.floor((moveY+ gy / 2) / gy) * gy;
  112. }
  113. function thisXMove() {
  114. if (isDown == true) {
  115. element.css({
  116. left: moveX
  117. });
  118. } else {
  119. return;
  120. }
  121. if (moveX < 0) {
  122. element.css({
  123. left: 0
  124. });
  125. }
  126. if (moveX > (boxWidth - sonWidth)) {
  127. element.css({
  128. left: boxWidth - sonWidth
  129. });
  130. }
  131. return moveX;
  132. }
  133. function thisYMove() {
  134. if (isDown == true) {
  135. element.css({
  136. top: moveY
  137. });
  138. } else {
  139. return;
  140. }
  141. if (moveY < 0) {
  142. element.css({
  143. top: 0
  144. });
  145. }
  146. if (moveY > (boxHeight - sonHeight)) {
  147. element.css({
  148. top: boxHeight - sonHeight
  149. });
  150. }
  151. return moveY;
  152. }
  153. function thisAllMove() {
  154. if (isDown == true) {
  155. element.css({
  156. left: moveX,
  157. top: moveY
  158. });
  159. } else {
  160. return;
  161. }
  162. if (moveX < 0) {
  163. element.css({
  164. left: 0
  165. });
  166. }
  167. if (moveX > (boxWidth - sonWidth)) {
  168. element.css({
  169. left: boxWidth - sonWidth
  170. });
  171. }
  172. if (moveY < 0) {
  173. element.css({
  174. top: 0
  175. });
  176. }
  177. if (moveY > (boxHeight - sonHeight)) {
  178. element.css({
  179. top: boxHeight - sonHeight
  180. });
  181. }
  182. }
  183. if (isDown) {
  184. fun.dragMove(parseInt(element.css('left')), parseInt(element.css('top')));
  185. } else {
  186. return false;
  187. }
  188. if (direction.toLowerCase() == "x") {
  189. thisXMove();
  190. } else if (direction.toLowerCase() == "y") {
  191. thisYMove();
  192. } else {
  193. thisAllMove();
  194. }
  195. });
  196. }
  197. }
  198. $.fn.myDrag = function (options) {
  199. var drag = new Drag(this, options);
  200. drag.run();
  201. return this;
  202. }
  203. })(jQuery, window, document);