2009-07-07 34 views
9

Tôi đang cố gắng sử dụng window.pageYOffset & window.scrollMaxY để tính toán tiến độ trang hiện tại. Cách tiếp cận này hoạt động theo FF3.5 nhưng theo webkit window.scrollMaxY là không xác định.Lựa chọn thay thế cho window.scrollMaxY?

Trả lời

2

Tôi đã có đi với document.body.scrollHeight để

document.body.scrollHeight = window.pageYOffset + screen height in pixels 

vào cuối trang (trên Android).

14

Thay thế cho window.scrollMaxY:

document.documentElement.scrollHeight - document.documentElement.clientHeight 

cho cùng một kết quả như window.scrollMaxY với IE7, IE8, FF3.5, Safari 4, Opera 10, Google Chrome 3 dưới DOCTYPE XHTML 1.0 Transitional.

+0

Cảm ơn bạn sir tốt. Tôi rất muốn biết nếu có bất kỳ nhược điểm nào khi sử dụng phương pháp này. Tôi chỉ thử nghiệm nó trong chrome/firefox và nó hoạt động! – swajak

+0

Cố gắng thực hiện nó chống lại 'window.scrollY' Tôi nhận thấy rằng trong chrome ít nhất,' window.scrollY' là một float chính xác cao, trong khi 'scrollHeight' và' clientHeight' là số nguyên, vì vậy tôi đã phải 'Math.round() 'nó – Kaiido

3

hai năm sau ...

function getScrollMaxY(){ 

var innerh; 

if (window.innerHeight){ 
    innerh = window.innerHeight; 
}else{ 
    innerh = document.body.clientHeight; 
} 

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; 
} 
0
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