精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Confused by document dimensions in JavaScript
userphoto

2013.08.19

關注
up vote 7 down vote accepted

I'll try to answer as simply as I can.

The Document and Viewport

In terms of geometry, there are two sets of dimensions to be aware of; the document dimensions, which reflect the entire size of the loaded page, including the content beyond the bottom of the window and the viewport dimensions, which reflect the size of the visible part of the document that is immediately displayed in the window.

When you scroll down, the viewport moves down over the document by a certain number of pixels. In other words, the viewport is the actual browser window "border" (the toolbars, menus, tabs, and so on).

The confusion comes from the fact that depending on the browser and mode, different properties are used to get the dimensions of a document and viewport, and they return different results depending on scrollbars. But we'll come back to this.

Dimensions Overview

There are a number of properties available to you from the get-go in javascript which give you different dimensions.

  1. Screen resolution:window.screen.width -Height

  2. Available screen space (same as monitor resolution) minus docks, toolbars and other UI elements:window.screen.availWidth -Height.

  3. Document dimensions:document.documentElement.offsetWidth -HeightNote: These numbers do not include the scrollbars.

  4. Viewport dimensions:window.innerWidth -Height

    • These numbers include the scrollbars.

    • This is not available in IE 8- and IE9, so if IE, test for the document.compatMode === "CSS1Compat" and if true, use document.documentElement.clientWidth -Height, and for quirks mode use document.body.clientWidth -Height.

A note about document dimensions

As per above, document.documentElement.offsetWidth/Height provides you with the actual size of the document. One caveat to this is that scrollbars work differently between browsers. For example, IE9 will always display a vertical scrollbar even if the document height is less than the viewport height. Safari/Chrome doesn't have scrollbars on OS X Lion. Chrome on PC will not display vertical scrollbars unless it needs to.

So you may bump into inconsistencies and the Scrollbar shifts content problem. Imagine you have an absolutely positioned and centred element. Because CSS calculates the "centre" relative to the document dimensions and not the viewport dimensions, when say, Google adds the scrollbars, your content may "jump" a bit to the left as the "document centre" changes. So you may need to write JS to compensate for this effect if it bothers you, or maybe someone here can write a quick JS function to calculate document dimensions with scrollbars included.

Scrollbar Position and Dimensions

While some methods in JavaScript work with document coordinates, others work with viewport coordinates, and often this is not what you want. For example, if you have an element's top edge at 20px in document coordinates, and you scroll the page down by 20px, the top edge of that element will be at 0px relative to the top viewport coordinate. So to convert between the two systems, you first need to know by how many pixels a user has scrolled the document, and then add that number to the viewport to compensate (look at example below).

I also found these helpful:

http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

http://www.quirksmode.org/mobile/viewports.html

And here's a quick cross-browser module I mucked up to help you:

var dimensions = (function(){    var dims = {};    // get screen width/height:    dims.screenWidth = function() { window.screen.width };    dims.screenHeight = function() { return window.screen.height };    // get screen width/height minus chrome:    dims.availWidth = function() { return window.screen.availWidth };    dims.availHeight = function() { return window.screen.availHeight };    // get document width/height (with-out scrollbars):    if (window.document.compatMode == "CSS1Compat"){ // if IE Standards Mode        dims.documentWidth = function() { return document.body.offsetWidth };        dims.documentHeight = function() { return document.body.offsetHeight };    }    else {        dims.documentWidth = function() { return document.documentElement.offsetWidth };        dims.documentHeight = function() { return document.documentElement.offsetHeight };    }    // get viewport width/height (with scrollbars):    if (window.innerWidth != null) {        dims.viewportWidth = function () { return window.innerWidth };        dims.viewportHeight = function () { return window.innerHeight };    }        // if IE in Standards Mode        else if (window.document.compatMode == "CSS1Compat"){            dims.viewportWidth = function () {                 return window.document.documentElement.clientWidth            };            dims.viewportHeight = function () {                 return window.document.documentElement.clientHeight            };        }    // get scrollbar offsets:    if (window.pageXOffset != null) {        dims.scrollXOffset = function() { return window.pageXOffset };        dims.scrollYOffset = function() { return window.pageYOffset };    }        // if IE in Standards Mode        else if (window.document.compatMode == "CSS1Compat"){            dims.scrollXOffset = function() { return document.documentElement.scrollLeft };            dims.scrollYOffset = function() { return document.documentElement.scrollTop };          }    return dims;}());

You can for example do console.log(dimensions.viewportWidth()) to get the viewport width.

Hope this helps you :)

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
DOM系列:視窗、設備、滾動條和文檔尺寸
Web移動端實現自適應縮放界面的方法匯總
判斷移動設備的橫屏和豎屏最佳方案
Js返回頂部的方法
獲取分辨率
js/jq滾動到一定位置后固定顯示 | 站長學習網
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 桃源县| 东阳市| 漳平市| 伊春市| 佳木斯市| 霍城县| 吉木萨尔县| 大埔县| 苍溪县| 平山县| 汤原县| 双流县| 河北省| 缙云县| 大英县| 罗定市| 满洲里市| 无极县| 道孚县| 宜兰县| 黑山县| 东源县| 黄浦区| 阳泉市| 黔西县| 陆丰市| 馆陶县| 新余市| 舒兰市| 永泰县| 蓬莱市| 灵台县| 那曲县| 宾川县| 延川县| 金溪县| 嘉黎县| 河西区| 云阳县| 浦城县| 阜阳市|