diff --git a/CHANGELOG b/CHANGELOG index 26baf73..b5faa02 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/src/dom.js b/src/dom.js index 563b7da..0ab6b7a 100644 --- a/src/dom.js +++ b/src/dom.js @@ -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); + } +}; \ No newline at end of file diff --git a/test/unit/dom.html b/test/unit/dom.html index e501a16..719695d 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -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');