prototype: Add the document.viewport object for querying dimensions and scroll offsets of the browser viewport.

This commit is contained in:
Sam Stephenson 2007-08-08 20:42:46 +00:00
parent c488384f90
commit ae2ab03294
3 changed files with 58 additions and 0 deletions

View File

@ -1,5 +1,12 @@
*SVN*
* Add the document.viewport object for querying dimensions and scroll offsets of the browser viewport. [Andrew Dupont, Thomas Fuchs, sam]
Example:
document.viewport.getDimensions() // { width: 1149, height: 923 }
document.viewport.getWidth() // 1149
document.viewport.getHeight() // 923
document.viewport.getScrollOffsets() // { left: 0, top: 1592 }
* Add support for brackets in quoted attribute value selectors. Closes #9157. [Ken Snyder]
* Add some missing semicolons to the source tree. Closes #9140. [jdalton]

View File

@ -1168,3 +1168,29 @@ Element.addMethods = function(methods) {
if (Element.extend.refresh) Element.extend.refresh();
Element.cache = { };
};
document.viewport = {
getDimensions: function() {
var dimensions = {};
$w('width height').each( function(d) {
var D = d.capitalize();
dimensions[d] = self['inner' + D] ||
(document.documentElement['client' + D] || document.body['client' + D]);
});
return dimensions;
},
getWidth: function() {
return this.getDimensions().width;
},
getHeight: function() {
return this.getDimensions().height;
},
getScrollOffsets: function() {
return Element._returnOffset(
window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
}
};

View File

@ -1491,6 +1491,31 @@
elt.absolutize();
assertUndefined(elt._originalLeft, 'absolutize() did not detect absolute positioning');
});
}},
testViewportDimensions: function() {with(this) {
window.resizeTo(800, 600);
var before = document.viewport.getDimensions();
window.resizeBy(50, 50);
var after = document.viewport.getDimensions();
assertEqual(before.width + 50, after.width, "NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THESE TESTS TO PASS");
assertEqual(before.height + 50, after.height, "NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THESE TESTS TO PASS");
}},
testViewportScrollOffsets: function() {with(this) {
window.scrollTo(0, 0);
assertEqual(0, document.viewport.getScrollOffsets().top);
window.scrollTo(0, 35);
assertEqual(35, document.viewport.getScrollOffsets().top);
window.resizeTo(200, 650);
window.scrollTo(25, 35);
assertEqual(25, document.viewport.getScrollOffsets().left);
window.resizeTo(850, 650);
}}
}, 'testlog');