dwz.drag.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @author Roger Wu
  3. */
  4. (function($){
  5. $.fn.jDrag = function(options){
  6. if (typeof options == 'string') {
  7. if (options == 'destroy')
  8. return this.each(function(){
  9. $(this).unbind('mousedown', $.rwdrag.start);
  10. $.data(this, 'pp-rwdrag', null);
  11. });
  12. }
  13. return this.each(function(){
  14. var el = $(this);
  15. $.data($.rwdrag, 'pp-rwdrag', {
  16. options: $.extend({
  17. el: el,
  18. obj: el
  19. }, options)
  20. });
  21. if (options.event)
  22. $.rwdrag.start(options.event);
  23. else {
  24. var select = options.selector;
  25. $(select, obj).bind('mousedown', $.rwdrag.start);
  26. }
  27. });
  28. };
  29. $.rwdrag = {
  30. start: function(e){
  31. document.onselectstart=function(e){return false};//禁止选择
  32. var data = $.data(this, 'pp-rwdrag');
  33. var el = data.options.el[0];
  34. $.data(el, 'pp-rwdrag', {
  35. options: data.options
  36. });
  37. if (!$.rwdrag.current) {
  38. $.rwdrag.current = {
  39. el: el,
  40. oleft: parseInt(el.style.left) || 0,
  41. otop: parseInt(el.style.top) || 0,
  42. ox: e.pageX || e.screenX,
  43. oy: e.pageY || e.screenY
  44. };
  45. $(document).bind("mouseup", $.rwdrag.stop).bind("mousemove", $.rwdrag.drag);
  46. }
  47. },
  48. drag: function(e){
  49. if (!e) var e = window.event;
  50. var current = $.rwdrag.current;
  51. var data = $.data(current.el, 'pp-rwdrag');
  52. var left = (current.oleft + (e.pageX || e.clientX) - current.ox);
  53. var top = (current.otop + (e.pageY || e.clientY) - current.oy);
  54. if (top < 1) top = 0;
  55. if (data.options.move == 'horizontal') {
  56. if ((data.options.minW && left >= $(data.options.obj).cssv("left") + data.options.minW) && (data.options.maxW && left <= $(data.options.obj).cssv("left") + data.options.maxW))
  57. current.el.style.left = left + 'px';
  58. else if (data.options.scop) {
  59. if (data.options.relObj) {
  60. if ((left - parseInt(data.options.relObj.style.left)) > data.options.cellMinW) {
  61. current.el.style.left = left + 'px';
  62. }
  63. } else
  64. current.el.style.left = left + 'px';
  65. }
  66. } else if (data.options.move == 'vertical') {
  67. current.el.style.top = top + 'px';
  68. } else {
  69. var selector = data.options.selector ? $(data.options.selector, data.options.obj) : $(data.options.obj);
  70. if (left >= -selector.outerWidth() * 2 / 3 && top >= 0 && (left + selector.outerWidth() / 3 < $(window).width()) && (top + selector.outerHeight() < $(window).height())) {
  71. current.el.style.left = left + 'px';
  72. current.el.style.top = top + 'px';
  73. }
  74. }
  75. if (data.options.drag) {
  76. data.options.drag.apply(current.el, [current.el, e]);
  77. }
  78. return $.rwdrag.preventEvent(e);
  79. },
  80. stop: function(e){
  81. var current = $.rwdrag.current;
  82. var data = $.data(current.el, 'pp-rwdrag');
  83. $(document).unbind('mousemove', $.rwdrag.drag).unbind('mouseup', $.rwdrag.stop);
  84. if (data.options.stop) {
  85. data.options.stop.apply(current.el, [current.el, e]);
  86. }
  87. $.rwdrag.current = null;
  88. document.onselectstart=function(e){return true};//启用选择
  89. return $.rwdrag.preventEvent(e);
  90. },
  91. preventEvent:function(e){
  92. if (e.stopPropagation) e.stopPropagation();
  93. if (e.preventDefault) e.preventDefault();
  94. return false;
  95. }
  96. };
  97. })(jQuery);