简体   繁体   中英

Javascript - Getting document width

I'd like to get the document's width with javascript (jQuery available), which I could achieve simply with document.width . The problem comes up when the contents are positioned with a negative value, because document.width will retrieve the width less that value (you can see this behavior in http://jsfiddle.net/McgzK/1/ ). So, is there a way I can get the document's width straight forward, or should I do something like document.width - parseInt($(container).css('left')) ?

There is no straight forward way to get that width. This width does not make any sense. document.width is the value of documents width. If you put something outside document layout its not included in documents width as its not in the document . So for your Total width you have to add that portion.

Use the scrollWidth property of your element and add absolute left value of css position.

See http://jsfiddle.net/McgzK/11/

Note:

  1. if you want the width of content use scrollWidth property.
  2. If you want to get the width of the element use offsetWidth .

尝试在CSS中将主体宽度设置为100%,然后使用jQuery获取主体的宽度,而不是文档的宽度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM