PDA

View Full Version : Help with anchors into current view



sergiozambrano
06-22-2009, 02:02 PM
I'm working on a script for highlighting the links to anchors in the visible part of screen and it only works for the first 3 anchors.

I've already changed the html to make sure it's fine, and no matter how I add, delete, or move around the links it always works with the first 3.

Anchors are named 'p01', 'p02' and so on.
Links id are same than anchors plus "_link" (e.g. 'p01_link')


function isinView(who) {
var top = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
var vpH = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
var coverage = parseInt(vpH + top);
if ( who.offsetTop > top && who.offsetTop < vpH ) {
isornot=1;
} else {
isornot=0;
}
return isornot;
}

window.onscroll = function () {
for (var i=0;i<anchors_count = document.anchors.length;i++) {
if(isinView(document.anchors[i])) {
var linkedlink = document.getElementById(document.anchors[i].name + '_link');
linkedlink.style.display = 'none';
} else {
document.anchors[i].style.color = '#999999';
}
}
}

Any clue where the error is? (I'd say it's something with the 'linkedlink' var, which constructs the name of the related link to its anchor, but I don't know how to do it in another way.

randomuser773
06-22-2009, 03:30 PM
if ( who.offsetTop > top && who.offsetTop < vpH )
Perhaps that test would do better as:

if ( who.offsetTop > top && who.offsetTop < top + vpH )

sergiozambrano
06-23-2009, 12:03 AM
Yes, super true. I was so tired.
I traced the result when building the script and it looked like vpH gave me the position of the bottom of the visible area.

Thanks for your time.