Javascript window.scrollMaxY的替代方案?
我正在尝试使用window.pageYOffset&window.scrollMaxY计算当前页面进度。这种方法在FF3.5下有效,但在webkit窗口下。scrollMaxY是未定义的。我没有使用Javascript window.scrollMaxY的替代方案?,javascript,android,webkit,Javascript,Android,Webkit,我正在尝试使用window.pageYOffset&window.scrollMaxY计算当前页面进度。这种方法在FF3.5下有效,但在webkit窗口下。scrollMaxY是未定义的。我没有使用document.body.scrollHeight,因此 document.body.scrollHeight = window.pageYOffset + screen height in pixels 在页面末尾(在Android上)。替代窗口。scrollMaxY: document.doc
document.body.scrollHeight
,因此
document.body.scrollHeight = window.pageYOffset + screen height in pixels
在页面末尾(在Android上)。替代
窗口。scrollMaxY
:
document.documentElement.scrollHeight - document.documentElement.clientHeight
给出与窗口相同的结果。scrollMaxY在DoctypeXHTML1.0过渡版下使用ie7、ie8、ff3.5、Safari 4、Opera 10、Google Chrome 3。两年后
function getScrollMaxY(){"use strict";
var innerh = window.innerHeight || ebody.clientHeight, yWithScroll = 0;
if (window.innerHeight && window.scrollMaxY){
// Firefox
yWithScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){
// all but Explorer Mac
yWithScroll = document.body.scrollHeight;
} else {
// works in Explorer 6 Strict, Mozilla (not FF) and Safari
yWithScroll = document.body.offsetHeight;
}
return yWithScroll-innerh;
}
谢谢你,好先生。我很想知道使用这种方法是否有缺点。我刚刚在chrome/firefox上测试了它,效果很好!我注意到,至少在chrome中,
window.scrollY
是一个高精度的浮点,而scrollHeight
和clientHeight
是整数,所以我必须Math.round()
x = document.body.clientHeight;
console.log(x ,"Cline HEight");
xx = window.innerHeight;
console.log(xx, "Inner Height");
xxx = document.body.scrollHeight
console.log(xxx, "scrollHeight");
xxxx = window.scrollMaxY;
console.log(xxxx, "scrollMaxY for IE");
xxxxx = document.body.offsetHeight;
console.log(xxxxx, "offsetHeight");
xxxxxx= document.body.scrollTop;
console.log(xxxxxx, "scrollTop");strong text