简体   繁体   English

获取页面内容的高度

[英]Get height of page content

I am trying to get the height of the body of a webpage. 我正在尝试获取网页正文的高度。 I want it to be the value of when the page is loaded on an HD screen at 100% zoom. 我希望它是将页面以100%缩放比例加载到高清屏幕上时的值。

The point of this is to then change the viewport in the Meta data and be able to force a mobile browser to load whole page at once, and block the responsiveness of the design. 这样做的目的是然后更改元数据中的视口,并能够强制移动浏览器立即加载整个页面,并阻止设计的响应速度。

function calculateHeightOfBody() {
var body = document.body,
html = document.documentElement;

var height = Math.max( body.scrollHeight, body.offsetHeight, 
                   html.clientHeight, html.scrollHeight, html.offsetHeight );

viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', /*'width='+width+',*/ 'height='+height);

alert('height is ' + height);

I have this function it works but gives me 2 different values on my whether i open the page on a computer or on a phone. 我可以使用此功能,但是无论我是在计算机上还是在手机上打开该页面,都可以给我2个不同的值。

On a computer with and HD screen i get 944 and on a phone i get 2044. 944 is the right value. 在带高清屏幕的计算机上,我得到944,而在电话上,我得到2044。944是正确的值。

Thanks 谢谢

The problem is that you have a high pixel density phone that renders the web page at an increased pixel ratio. 问题是您有一个高像素密度的电话,该电话以增加的像素比渲染网页。 Divide height by window.devicePixelRatio to account for this. 将高度除以window.devicePixelRatio即可解决此问题。

Note: I don't think your method of getting the height is particularly ideal. 注意:我认为您获取高度的方法不是特别理想。 Instead of just picking the biggest of like 5 values, you should just use one value. 与其仅选择5个值中的最大值,不如仅使用一个值。 Perhaps window.innerHeight would do the trick? 也许window.innerHeight可以解决问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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