1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
var ltwhRelativeTo=function(elms, container) {
var i=0;
var res=[];
var node, w, h, l, t;
while (i<elms.length) {
var node=elms[i];
w=node.offsetWidth;
h=node.offsetHeight;
l=0;
t=0;
while (node!=container && node.tagName.toLowerCase()!="html") {
l+=node.offsetLeft;
t+=node.offsetTop;
if (node.offsetParent!==null) {
node=node.offsetParent;
} else {
break;
}
}
res[i]={left:l, top:t, width:w, height:h};
i++;
}
return res;
}
$('window').scroll(
function() {
var scrollTop=$("body").scrollTop();
var pagesAnchors=$(".page"),min=100000, currentPage=null;
var offsetsTop=ltwhRelativeTo(pagesAnchors, document.body);
for (var i=0; i<offsetsTop.length; i++) {
if (Math.abs(scrollTop-offsetsTop[i].top)<min) {
min=Math.abs(scrollTop-offsetsTop[i].top);
currentPage=pagesAnchors[i].name;
}
}
//currentPage donne maintenant en principe la valeur de la page courante... Il n'y a plus qu'à en faire quelquechose
}
); |
Partager