Results 1 to 2 of 2

Thread: Help with anchors into current view.

  1. #1
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    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. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by sergiozambrano View Post

    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;
    }
    Reading the value of vpH on its own doesn't allow for the scrolled displacement of the page, so it will work only when the page isn't scrolled.

Bookmarks

Posting Permissions

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