mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
Offset: report offset for 0 sized elements
Fixes gh-3267 Closes gh-3367
This commit is contained in:
parent
4e50967725
commit
1777899a74
@ -93,6 +93,7 @@ jQuery.fn.extend( {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return zeros for disconnected and hidden (display: none) elements (gh-2310)
|
||||||
// Support: IE <=11 only
|
// Support: IE <=11 only
|
||||||
// Running getBoundingClientRect on a
|
// Running getBoundingClientRect on a
|
||||||
// disconnected node in IE throws an error
|
// disconnected node in IE throws an error
|
||||||
@ -102,20 +103,14 @@ jQuery.fn.extend( {
|
|||||||
|
|
||||||
rect = elem.getBoundingClientRect();
|
rect = elem.getBoundingClientRect();
|
||||||
|
|
||||||
// Make sure element is not hidden (display: none)
|
doc = elem.ownerDocument;
|
||||||
if ( rect.width || rect.height ) {
|
win = getWindow( doc );
|
||||||
doc = elem.ownerDocument;
|
docElem = doc.documentElement;
|
||||||
win = getWindow( doc );
|
|
||||||
docElem = doc.documentElement;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
top: rect.top + win.pageYOffset - docElem.clientTop,
|
top: rect.top + win.pageYOffset - docElem.clientTop,
|
||||||
left: rect.left + win.pageXOffset - docElem.clientLeft
|
left: rect.left + win.pageXOffset - docElem.clientLeft
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// Return zeros for disconnected and hidden elements (gh-2310)
|
|
||||||
return rect;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
position: function() {
|
position: function() {
|
||||||
|
@ -42,7 +42,7 @@ QUnit.test( "empty set", function( assert ) {
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( "disconnected element", function( assert ) {
|
QUnit.test( "disconnected element", function( assert ) {
|
||||||
assert.expect( 2 );
|
assert.expect( 3 );
|
||||||
|
|
||||||
var result = jQuery( document.createElement( "div" ) ).offset();
|
var result = jQuery( document.createElement( "div" ) ).offset();
|
||||||
|
|
||||||
@ -51,10 +51,11 @@ QUnit.test( "disconnected element", function( assert ) {
|
|||||||
// valid input, but will return zeros for back-compat
|
// valid input, but will return zeros for back-compat
|
||||||
assert.equal( result.top, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
|
assert.equal( result.top, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
|
||||||
assert.equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
|
assert.equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
|
||||||
|
assert.equal( Object.keys( result ).length, 2, "Retrieving offset on disconnected elements returns offset object (gh-3167)" );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( "hidden (display: none) element", function( assert ) {
|
QUnit.test( "hidden (display: none) element", function( assert ) {
|
||||||
assert.expect( 2 );
|
assert.expect( 3 );
|
||||||
|
|
||||||
var node = jQuery( "<div style='display: none' />" ).appendTo( "#qunit-fixture" ),
|
var node = jQuery( "<div style='display: none' />" ).appendTo( "#qunit-fixture" ),
|
||||||
result = node.offset();
|
result = node.offset();
|
||||||
@ -66,6 +67,33 @@ QUnit.test( "hidden (display: none) element", function( assert ) {
|
|||||||
// valid input, but will return zeros for back-compat
|
// valid input, but will return zeros for back-compat
|
||||||
assert.equal( result.top, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
|
assert.equal( result.top, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
|
||||||
assert.equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
|
assert.equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
|
||||||
|
assert.equal( Object.keys( result ).length, 2, "Retrieving offset on hidden elements returns offset object (gh-3167)" );
|
||||||
|
} );
|
||||||
|
|
||||||
|
QUnit.test( "0 sized element", function( assert ) {
|
||||||
|
assert.expect( 3 );
|
||||||
|
|
||||||
|
var node = jQuery( "<div style='margin: 5px; width: 0; height: 0' />" ).appendTo( "#qunit-fixture" ),
|
||||||
|
result = node.offset();
|
||||||
|
|
||||||
|
node.remove();
|
||||||
|
|
||||||
|
assert.notEqual( result.top, 0, "Retrieving offset on 0 sized elements (gh-3167)" );
|
||||||
|
assert.notEqual( result.left, 0, "Retrieving offset on 0 sized elements (gh-3167)" );
|
||||||
|
assert.equal( Object.keys( result ).length, 2, "Retrieving offset on 0 sized elements returns offset object (gh-3167)" );
|
||||||
|
} );
|
||||||
|
|
||||||
|
QUnit.test( "hidden (visibility: hidden) element", function( assert ) {
|
||||||
|
assert.expect( 3 );
|
||||||
|
|
||||||
|
var node = jQuery( "<div style='margin: 5px; visibility: hidden' />" ).appendTo( "#qunit-fixture" ),
|
||||||
|
result = node.offset();
|
||||||
|
|
||||||
|
node.remove();
|
||||||
|
|
||||||
|
assert.notEqual( result.top, 0, "Retrieving offset on visibility:hidden elements (gh-3167)" );
|
||||||
|
assert.notEqual( result.left, 0, "Retrieving offset on visibility:hidden elements (gh-3167)" );
|
||||||
|
assert.equal( Object.keys( result ).length, 2, "Retrieving offset on visibility:hidden elements returns offset object (gh-3167)" );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
testIframe( "absolute", "offset/absolute.html", function( assert, $, iframe ) {
|
testIframe( "absolute", "offset/absolute.html", function( assert, $, iframe ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user