jQuery.Resize.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (function($){
  2. var doc = $(document),
  3. win = $(window),
  4. mouseDown = 'mousedown.resize',
  5. mouseMove = 'mousemove.resize',
  6. mouseUp = 'mouseup.resize',
  7. clsName = 'CursorResize',
  8. resize = function (e) {
  9. var o = this,
  10. a = o.wrapper.width('auto'),
  11. b = o.target,
  12. c = 0,
  13. d = 0,
  14. f = b.offset();
  15. if (a)
  16. {
  17. c = a.width() - b.width();
  18. d = a.height() - b.height();
  19. f = a.offset();
  20. }
  21. var m = $.extend({
  22. width : win.width() - (f.left - doc.scrollLeft()) - c,
  23. height : win.height() - (f.top - doc.scrollTop()) - d
  24. }, o.max),
  25. w = Math.min(Math.max(e.pageX - o.x + o.w, o.min.width), m.width),
  26. h = Math.min(Math.max(e.pageY - o.y + o.h, o.min.height),m.height);
  27. b.css({ width : w, height : h });
  28. return false;
  29. },
  30. end = function () {
  31. doc.unbind(mouseMove + ' ' + mouseUp);
  32. },
  33. start = function (e) {
  34. var E = this,
  35. T = E.target;
  36. E.x = e.pageX;
  37. E.y = e.pageY;
  38. E.w = T.outerWidth() - T.getPadding().w;
  39. E.h = T.outerHeight() - T.getPadding().h;
  40. if (E.min.width === 0)
  41. {
  42. var MW = T.data('_mw');
  43. if (MW)
  44. {
  45. E.min.width = MW;
  46. }
  47. else
  48. {
  49. E.min.width = T.outerWidth() - T.getPadding().w;
  50. T.data('_mw', E.min.width);
  51. }
  52. }
  53. if (this.min.height === 0)
  54. {
  55. var MH = T.data('_mh');
  56. if (MH)
  57. {
  58. E.min.height = MH;
  59. }
  60. else
  61. {
  62. E.min.height = T.outerHeight() - T.getPadding().h;
  63. T.data('_mh', E.min.height);
  64. }
  65. }
  66. doc.bind(mouseMove, $.proxy(resize, E)).bind(mouseUp, end);
  67. return false;
  68. };
  69. $.fn.resize = function(o){
  70. o = $.extend({ min : { width : 0, height: 0 }}, o);
  71. /*
  72. options = {
  73. handler : null,
  74. wrapper : null,
  75. min : { width : 0, height: 0},
  76. max : { width : 0, height: 0}
  77. };
  78. */
  79. return this.each(function(){
  80. var e = $(this),
  81. x = function () {
  82. this.target = e;
  83. this.handler = o.handler ? (typeof o.handler === 'string' ? $(o.handler, e[0]) : o.handler) : e;
  84. this.wrapper = o.wrapper;
  85. e.data('_h', this.handler);
  86. this.min = o.min;
  87. this.max = o.max;
  88. if (o.min) $.extend(this.min, o.min);
  89. return this;
  90. },
  91. X = new x();
  92. X.handler.addClass(clsName).unbind(mouseDown).bind(mouseDown, $.proxy(start, X));
  93. });
  94. };
  95. $.fn.unResize = function () {
  96. return this.each(function(){
  97. ($(this).data('_h') || $()).removeClass(clsName).unbind(mouseDown);
  98. });
  99. };
  100. $.fn.getPadding = function () {
  101. var s = this[0].style,
  102. o =
  103. {
  104. w : parseInt(s.paddingLeft) + parseInt(s.paddingRight) || 0,
  105. h : parseInt(s.paddingTop) + parseInt(s.paddingBottom) || 0
  106. };
  107. return o;
  108. };
  109. })($);