Skip to content

Commit

Permalink
allow offsets() accept object with top and left
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 30, 2012
1 parent 361ecbc commit f31a913
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion bonzo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
7 changes: 6 additions & 1 deletion src/bonzo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
11 changes: 10 additions & 1 deletion tests/layout-test.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 () {
Expand Down

0 comments on commit f31a913

Please sign in to comment.