J'essaie d'utiliser window.pageYOffset & window.scrollMaxY pour calculer la progression de la page en cours. Cette approche fonctionne sous FF3.5 mais sous webkit window.scrollMaxY est indéfini.Alternatives à window.scrollMaxY?
9
A
Répondre
2
J'ai loin avec document.body.scrollHeight
afin que
document.body.scrollHeight = window.pageYOffset + screen height in pixels
à la fin de la page (sur Android).
14
Alternative à window.scrollMaxY
:
document.documentElement.scrollHeight - document.documentElement.clientHeight
donne même résultat que window.scrollMaxY
avec IE7, IE8, FF3.5, Safari 4, Opera 10, Google Chrome 3 sous DOCTYPE XHTML 1.0 Transitional.
3
deux ans plus tard ...
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
Merci bon monsieur. J'aimerais savoir s'il y a des inconvénients à utiliser cette méthode. Je viens de le tester en chrome/firefox et ça marche! – swajak
En essayant de l'implémenter avec 'window.scrollY', j'ai remarqué que dans le chrome au moins,' window.scrollY' est un float de haute précision, alors que 'scrollHeight' et' clientHeight' sont des entiers, donc j'ai dû 'Math.round() ' – Kaiido