Permalink
Browse files

exchange scroll with scrollTop and scrollLeft for backwards compatibi…

…lity with jquery + compat with bean (which has scroll() as event handler)
  • Loading branch information...
1 parent 376e428 commit bba165a8b2607184061230d13f4cd68da95e7598 @fat fat committed Apr 14, 2011
Showing with 46 additions and 36 deletions.
  1. +20 −12 bonzo.js
  2. +1 −1 bonzo.min.js
  3. +20 −12 src/bonzo.js
  4. +5 −11 tests/tests.html
View
@@ -228,22 +228,30 @@
});
},
- scroll: function (x, y) {
- var el = this.first();
- if (x == null || y == null) {
- return isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop };
- }
- if (isBody(el)) {
- window.scrollTo(x, y);
- } else {
- x != null && (el.scrollLeft = x);
- y != null && (el.scrollTop = y);
- }
- return this;
+ scrollTop: function (y) {
+ return scroll.call(this, null, y, 'y');
+ },
+
+ scrollLeft: function (x) {
+ return scroll.call(this, x, null, 'x');
}
};
+ function scroll(x, y, type) {
+ var el = this.first();
+ if (x == null && y == null) {
+ return (isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop })[type];
+ }
+ if (isBody(el)) {
+ window.scrollTo(x, y);
+ } else {
+ x != null && (el.scrollLeft = x);
+ y != null && (el.scrollTop = y);
+ }
+ return this;
+ }
+
function isBody(element) {
return element === window || (/^(?:body|html)$/i).test(element.tagName);
}
View
Oops, something went wrong.
View
@@ -222,22 +222,30 @@
});
},
- scroll: function (x, y) {
- var el = this.first();
- if (x == null || y == null) {
- return isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop };
- }
- if (isBody(el)) {
- window.scrollTo(x, y);
- } else {
- x != null && (el.scrollLeft = x);
- y != null && (el.scrollTop = y);
- }
- return this;
+ scrollTop: function (y) {
+ return scroll.call(this, null, y, 'y');
+ },
+
+ scrollLeft: function (x) {
+ return scroll.call(this, x, null, 'x');
}
};
+ function scroll(x, y, type) {
+ var el = this.first();
+ if (x == null && y == null) {
+ return (isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop })[type];
+ }
+ if (isBody(el)) {
+ window.scrollTo(x, y);
+ } else {
+ x != null && (el.scrollLeft = x);
+ y != null && (el.scrollTop = y);
+ }
+ return this;
+ }
+
function isBody(element) {
return element === window || (/^(?:body|html)$/i).test(element.tagName);
}
View
@@ -194,17 +194,11 @@
ok($('#first-last div').last().id == 'last', 'found last() element');
});
- test('scroll', 1, function () {
- $(window).scroll(0, 100);
- ok($(window).scroll().y == 100, 'set and retrieved correct scroll on window');
- });
-
- test('scroll', 3, function () {
- $(window).scroll(0, 100);
- ok($(window).scroll().y == 100, 'condition1');
- $('#overflowed').scroll(150, 150);
- ok($('#overflowed').scroll().x == 150, 'condition2');
- ok($('#overflowed').scroll().y == 150, 'condition3');
+ test('scrollTop && scrollLeft', 2, function () {
+ $(window).scrollTop(100);
+ ok($(window).scrollTop() == 100, 'condition1');
+ $('#overflowed').scrollLeft(150);
+ ok($('#overflowed').scrollLeft() == 150, 'condition2');
});
});

0 comments on commit bba165a

Please sign in to comment.