Position: Extract getWithinInfo method, use that for tests that call getScrollInfo directly, pass within info to that, gets rid of a few more DOM accesses

This commit is contained in:
Jörn Zaefferer 2012-04-13 15:05:43 +02:00
parent c0a5e52f87
commit 7dcfae7da2
2 changed files with 25 additions and 23 deletions

View File

@ -277,7 +277,7 @@ test( "collision: fit, no offset", function() {
collisionTest({
collision: "fit"
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( within ).width }, "right bottom" );
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({
collision: "fit"
@ -292,7 +292,7 @@ test( "collision: fit, with offset", function() {
collisionTest({
collision: "fit",
at: "right+2 bottom+3"
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( within ).width }, "right bottom");
}, { top: addTop + of.position().top + of.height() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom");
collisionTest2({
collision: "fit",
@ -397,7 +397,7 @@ test( "collision: fit, with margin", function() {
collisionTest({
collision: "fit"
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( within ).width }, "right bottom" );
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({
collision: "fit"
@ -410,7 +410,7 @@ test( "collision: fit, with margin", function() {
collisionTest({
collision: "fit"
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( within ).width }, "right bottom" );
}, { top: addTop + of.position().top + of.height() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 10 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({
collision: "fit"
@ -423,7 +423,7 @@ test( "collision: fit, with margin", function() {
collisionTest({
collision: "fit"
}, { top: addTop + of.position().top + of.height() - 15 - $.position.getScrollInfo( within ).height, left: addLeft + of.position().left + of.width() - 15 - $.position.getScrollInfo( within ).width }, "right bottom" );
}, { top: addTop + of.position().top + of.height() - 15 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).height, left: addLeft + of.position().left + of.width() - 15 - $.position.getScrollInfo( $.position.getWithinInfo( within ) ).width }, "right bottom" );
collisionTest2({
collision: "fit"

View File

@ -38,16 +38,28 @@ $.position = {
return w1 - w2;
},
getScrollInfo: function(within) {
var notWindow = within[0] !== window,
overflowX = notWindow ? within.css( "overflow-x" ) : "",
overflowY = notWindow ? within.css( "overflow-y" ) : "",
getScrollInfo: function( within ) {
var overflowX = within.isWindow ? "" : within.element.css( "overflow-x" ),
overflowY = within.isWindow ? "" : within.element.css( "overflow-y" ),
scrollbarWidth = overflowX === "auto" || overflowX === "scroll" ? $.position.scrollbarWidth() : 0,
scrollbarHeight = overflowY === "auto" || overflowY === "scroll" ? $.position.scrollbarWidth() : 0;
return {
height: within.height() < within[0].scrollHeight ? scrollbarHeight : 0,
width: within.width() < within[0].scrollWidth ? scrollbarWidth : 0
height: within.height < within.element[0].scrollHeight ? scrollbarHeight : 0,
width: within.width < within.element[0].scrollWidth ? scrollbarWidth : 0
};
},
getWithinInfo: function( element ) {
var withinElement = $( element || window ),
isWindow = $.isWindow( withinElement[0] );
return {
element: withinElement,
isWindow: isWindow,
offset: withinElement.offset(),
scrollLeft: withinElement.scrollLeft(),
scrollTop: withinElement.scrollTop(),
width: isWindow ? withinElement.width() : withinElement.outerWidth(),
height: isWindow ? withinElement.height() : withinElement.outerHeight()
};
},
getOffsets: function( offsets, width, height ) {
@ -70,18 +82,8 @@ $.fn.position = function( options ) {
options = $.extend( {}, options );
var target = $( options.of ),
withinElement = $( options.within || window ),
isWindow = $.isWindow( withinElement[0] ),
within = {
element: withinElement,
isWindow: isWindow,
offset: withinElement.offset(),
scrollLeft: withinElement.scrollLeft(),
scrollTop: withinElement.scrollTop(),
width: isWindow ? withinElement.width() : withinElement.outerWidth(),
height: isWindow ? withinElement.height() : withinElement.outerHeight()
},
scrollInfo = $.position.getScrollInfo( withinElement ),
within = $.position.getWithinInfo( options.within ),
scrollInfo = $.position.getScrollInfo( within ),
targetElem = target[0],
collision = ( options.collision || "flip" ).split( " " ),
offsets = {},