mobiscroll.zepto.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. if (!window.jQuery) {
  2. var jQuery = Zepto;
  3. (function ($) {
  4. ['width', 'height'].forEach(function (dimension) {
  5. $.fn[dimension] = function (value) {
  6. var offset,
  7. body = document.body,
  8. html = document.documentElement,
  9. Dimension = dimension.replace(/./, function (m) { return m[0].toUpperCase(); });
  10. if (value === undefined) {
  11. return this[0] == window ?
  12. html['client' + Dimension] :
  13. this[0] == document ?
  14. Math.max(body['scroll' + Dimension], body['offset' + Dimension], html['client' + Dimension], html['scroll' + Dimension], html['offset' + Dimension]) :
  15. (offset = this.offset()) && offset[dimension];
  16. } else {
  17. return this.each(function (idx) {
  18. $(this).css(dimension, value);
  19. });
  20. }
  21. };
  22. });
  23. ['width', 'height'].forEach(function (dimension) {
  24. var offset, Dimension = dimension.replace(/./, function (m) { return m[0].toUpperCase(); });
  25. $.fn['outer' + Dimension] = function (margin) {
  26. var elem = this;
  27. if (elem) {
  28. var size = elem[0]['offset' + Dimension],
  29. sides = {'width': ['left', 'right'], 'height': ['top', 'bottom']};
  30. sides[dimension].forEach(function (side) {
  31. if (margin) {
  32. size += parseInt(elem.css('margin-' + side), 10);
  33. }
  34. });
  35. return size;
  36. } else {
  37. return null;
  38. }
  39. };
  40. });
  41. ['width', 'height'].forEach(function (dimension) {
  42. var offset, Dimension = dimension.replace(/./, function (m) { return m[0].toUpperCase(); });
  43. $.fn['inner' + Dimension] = function () {
  44. var elem = this;
  45. if (elem[0]['inner' + Dimension]) {
  46. return elem[0]['inner' + Dimension];
  47. } else {
  48. var size = elem[0]['offset' + Dimension],
  49. sides = {'width': ['left', 'right'], 'height': ['top', 'bottom']};
  50. sides[dimension].forEach(function (side) {
  51. size -= parseInt(elem.css('border-' + side + '-width'), 10);
  52. });
  53. return size;
  54. }
  55. };
  56. });
  57. ["Left", "Top"].forEach(function (name, i) {
  58. var method = "scroll" + name;
  59. function isWindow(obj) {
  60. return obj && typeof obj === "object" && "setInterval" in obj;
  61. }
  62. function getWindow(elem) {
  63. return isWindow(elem) ? elem : elem.nodeType === 9 ? elem.defaultView || elem.parentWindow : false;
  64. }
  65. $.fn[method] = function (val) {
  66. var elem, win;
  67. if (val === undefined) {
  68. elem = this[0];
  69. if (!elem) {
  70. return null;
  71. }
  72. win = getWindow(elem);
  73. // Return the scroll offset
  74. return win ? ("pageXOffset" in win) ? win[i ? "pageYOffset" : "pageXOffset"] :
  75. win.document.documentElement[method] ||
  76. win.document.body[method] :
  77. elem[method];
  78. }
  79. // Set the scroll offset
  80. this.each(function () {
  81. win = getWindow(this);
  82. if (win) {
  83. var xCoord = !i ? val : $(win).scrollLeft(),
  84. yCoord = i ? val : $(win).scrollTop();
  85. win.scrollTo(xCoord, yCoord);
  86. } else {
  87. this[method] = val;
  88. }
  89. });
  90. };
  91. });
  92. $.fn.prevUntil = function (selector) {
  93. var n = this,
  94. array = [];
  95. while (n.length && !$(n).filter(selector).length) {
  96. array.push(n[0]);
  97. n = n.prev();
  98. }
  99. return $(array);
  100. };
  101. $.fn.nextUntil = function (selector) {
  102. var n = this,
  103. array = [];
  104. while (n.length && !n.filter(selector).length) {
  105. array.push(n[0]);
  106. n = n.next();
  107. }
  108. return $(array);
  109. };
  110. // Fix zepto.js extend to work with undefined parameter
  111. $._extend = $.extend;
  112. $.extend = function () {
  113. arguments[0] = arguments[0] || {};
  114. return $._extend.apply(this, arguments);
  115. };
  116. })(jQuery);
  117. }