Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help with anchors into current view

    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')

    Code:
    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.

  • #2
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Code:
    if ( who.offsetTop > top && who.offsetTop < vpH )
    Perhaps that test would do better as:
    Code:
    if ( who.offsetTop > top && who.offsetTop < top + vpH )

  • Users who have thanked randomuser773 for this post:

    sergiozambrano (06-23-2009)

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Solved

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •