Skip to content

Commit 4fa0036

Browse files
committed
Added in support for .offset(Function).
1 parent ebaf374 commit 4fa0036

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/offset.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ if ( "getBoundingClientRect" in document.documentElement ) {
77
}
88

99
if ( options ) {
10-
return this.each(function() {
11-
jQuery.offset.setOffset( this, options );
10+
return this.each(function( i ) {
11+
jQuery.offset.setOffset( this, i, options );
1212
});
1313
}
1414

@@ -33,8 +33,8 @@ if ( "getBoundingClientRect" in document.documentElement ) {
3333
}
3434

3535
if ( options ) {
36-
return this.each(function() {
37-
jQuery.offset.setOffset( this, options );
36+
return this.each(function( i ) {
37+
jQuery.offset.setOffset( this, i, options );
3838
});
3939
}
4040

@@ -137,19 +137,24 @@ jQuery.offset = {
137137
return { top: top, left: left };
138138
},
139139

140-
setOffset: function( elem, options ) {
140+
setOffset: function( elem, i, options ) {
141141
// set position first, in-case top/left are set even on static elem
142142
if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) {
143143
elem.style.position = "relative";
144144
}
145145
var curElem = jQuery( elem ),
146146
curOffset = curElem.offset(),
147147
curTop = parseInt( jQuery.curCSS( elem, "top", true ), 10 ) || 0,
148-
curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10) || 0,
149-
props = {
150-
top: (options.top - curOffset.top) + curTop,
151-
left: (options.left - curOffset.left) + curLeft
152-
};
148+
curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0;
149+
150+
if ( jQuery.isFunction( options ) ) {
151+
options = options.call( elem, i, curOffset );
152+
}
153+
154+
var props = {
155+
top: (options.top - curOffset.top) + curTop,
156+
left: (options.left - curOffset.left) + curLeft
157+
};
153158

154159
if ( "using" in options ) {
155160
options.using.call( elem, props );

test/unit/offset.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module("offset");
22

33
testoffset("absolute", function( jQuery ) {
4+
expect(144);
5+
46
// get offset tests
57
var tests = [
68
{ id: '#absolute-1', top: 1, left: 1 },
@@ -51,6 +53,16 @@ testoffset("absolute", function( jQuery ) {
5153
equals( jQuery( this.id ).offset().top, this.top, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
5254
equals( jQuery( this.id ).offset().left, this.left, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
5355

56+
var top = this.top, left = this.left;
57+
58+
jQuery( this.id ).offset(function(i, val){
59+
equals( val.top, top, "Verify incoming top position." );
60+
equals( val.left, left, "Verify incoming top position." );
61+
return { top: top + 1, left: left + 1 };
62+
});
63+
equals( jQuery( this.id ).offset().top, this.top + 1, "jQuery('" + this.id + "').offset({ top: " + this.top + " })" );
64+
equals( jQuery( this.id ).offset().left, this.left + 1, "jQuery('" + this.id + "').offset({ left: " + this.left + " })" );
65+
5466
jQuery( this.id ).offset({ top: this.top, left: this.left, using: function( props ) {
5567
jQuery( this ).css({
5668
top: props.top + 1,
@@ -63,6 +75,8 @@ testoffset("absolute", function( jQuery ) {
6375
});
6476

6577
testoffset("relative", function( jQuery ) {
78+
expect(60);
79+
6680
// IE is collapsing the top margin of 1px
6781
var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
6882

@@ -122,6 +136,8 @@ testoffset("relative", function( jQuery ) {
122136
});
123137

124138
testoffset("static", function( jQuery ) {
139+
expect(80);
140+
125141
// IE is collapsing the top margin of 1px
126142
var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
127143

@@ -187,6 +203,8 @@ testoffset("static", function( jQuery ) {
187203
});
188204

189205
testoffset("fixed", function( jQuery ) {
206+
expect(28);
207+
190208
jQuery.offset.initialize();
191209

192210
var tests = [
@@ -238,31 +256,18 @@ testoffset("fixed", function( jQuery ) {
238256
});
239257

240258
testoffset("table", function( jQuery ) {
241-
var ie = jQuery.browser.msie;
259+
expect(4);
242260

243261
equals( jQuery('#table-1').offset().top, 6, "jQuery('#table-1').offset().top" );
244262
equals( jQuery('#table-1').offset().left, 6, "jQuery('#table-1').offset().left" );
245263

246264
equals( jQuery('#th-1').offset().top, 10, "jQuery('#th-1').offset().top" );
247265
equals( jQuery('#th-1').offset().left, 10, "jQuery('#th-1').offset().left" );
248-
249-
// equals( jQuery('#th-2').offset().top, 10, "jQuery('#th-2').offset().top" );
250-
// equals( jQuery('#th-2').offset().left, 116, "jQuery('#th-2').offset().left" );
251-
//
252-
// equals( jQuery('#th-3').offset().top, 10, "jQuery('#th-3').offset().top" );
253-
// equals( jQuery('#th-3').offset().left, 222, "jQuery('#th-3').offset().left" );
254-
255-
// equals( jQuery('#td-1').offset().top, ie ? 116 : 112, "jQuery('#td-1').offset().top" );
256-
// equals( jQuery('#td-1').offset().left, 10, "jQuery('#td-1').offset().left" );
257-
//
258-
// equals( jQuery('#td-2').offset().top, ie ? 116 : 112, "jQuery('#td-2').offset().top" );
259-
// equals( jQuery('#td-2').offset().left, 116, "jQuery('#td-2').offset().left" );
260-
//
261-
// equals( jQuery('#td-3').offset().top, ie ? 116 : 112, "jQuery('#td-3').offset().top" );
262-
// equals( jQuery('#td-3').offset().left, 222, "jQuery('#td-3').offset().left" );
263266
});
264267

265268
testoffset("scroll", function( jQuery, win ) {
269+
expect(12);
270+
266271
var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8;
267272

268273
// IE is collapsing the top margin of 1px
@@ -294,6 +299,8 @@ testoffset("scroll", function( jQuery, win ) {
294299
});
295300

296301
testoffset("body", function( jQuery ) {
302+
expect(2);
303+
297304
equals( jQuery('body').offset().top, 1, "jQuery('#body').offset().top" );
298305
equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" );
299306
});

0 commit comments

Comments
 (0)