Skip to content
Browse files

allow offsets() accept object with `top` and `left`

  • Loading branch information...
1 parent 361ecbc commit f31a9135c262e270b0bc249f742a21e706cca16e @rvagg rvagg committed
Showing with 21 additions and 3 deletions.
  1. +5 −1 bonzo.js
  2. +6 −1 src/bonzo.js
  3. +10 −1 tests/layout-test.js
View
6 bonzo.js
@@ -781,7 +781,11 @@
* @return {Bonzo|number}
*/
, offset: function (opt_x, opt_y) {
- if (typeof opt_x == 'number' || typeof opt_y == 'number') {
+ if (typeof opt_x == 'object' && (typeof opt_x.top == 'number' || typeof opt_x.left == 'number')) {
+ return this.each(function (el) {
+ xy(el, opt_x.top, opt_y.left)
+ })
+ } else if (typeof opt_x == 'number' || typeof opt_y == 'number') {
return this.each(function (el) {
xy(el, opt_x, opt_y)
})
View
7 src/bonzo.js
@@ -776,7 +776,12 @@
* @return {Bonzo|number}
*/
, offset: function (opt_x, opt_y) {
- if (typeof opt_x == 'number' || typeof opt_y == 'number') {
+ console.log('offset',arguments)
+ if (opt_x && typeof opt_x == 'object' && (typeof opt_x.top == 'number' || typeof opt_x.left == 'number')) {
+ return this.each(function (el) {
+ xy(el, opt_x.left, opt_x.top)
+ })
+ } else if (typeof opt_x == 'number' || typeof opt_y == 'number') {
return this.each(function (el) {
xy(el, opt_x, opt_y)
})
View
11 tests/layout-test.js
@@ -1,7 +1,7 @@
/*global sink:true start:true Q:true dom:true $:true bowser:true ender:true*/
sink('Layout', function (test, ok) {
- test('offsets', 6, function () {
+ test('offsets', 10, function () {
var $el = $(dom.create('<div/>')).css({
position: 'absolute',
left: '50px',
@@ -18,6 +18,15 @@ sink('Layout', function (test, ok) {
$el.offset(0, 0)
ok($el.offset().top === 0, 'setting "0" doesnt become falsy')
ok($el.offset().left === 0, 'setting "0" doesnt become falsy')
+
+ $el.offset({ left: 100 })
+ $el.offset({ top: -100 })
+ ok($el.offset().left == 100, 'after offset({ left: 100 })')
+ ok($el.offset().top == -100, 'after offset({ top: -100 })')
+
+ $el.offset({ left: -200, top: 200 })
+ ok($el.offset().left == -200, 'after offset({ left: -200, top: 200 })')
+ ok($el.offset().top == 200, 'after offset({ left: -200, top: 200 })')
})
test('offset + scroll', 2, function () {

0 comments on commit f31a913

Please sign in to comment.
Something went wrong with that request. Please try again.