SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    scroll to anchor

    Hi,

    I came across this article from Sitepoint http://www.sitepoint.com/print/scrol...hly-javascript

    It works perfectly fine when i click on an anchor link. But now the question is, i need to use javascript to call the scroll function to an anchor link instead of having user to click on an anchor link. Is it possible?

    Any ideas are greatly appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simplest way is to call document.anchors[i].focus() for the i index of the anchors array

    Otherwise you need to figure out the y position of the element
    and call window.scrollTo(0,y).

    Here is one way:

    Code:
    function elWhere(who){	
    	var L=0, T=0;
    	var pa=who;
    	while(pa.parentNode){
    		L+= ( pa.offsetLeft)? pa.offsetLeft: 0;
    		T+= (pa.offsetTop)?  pa.offsetTop: 0;	
    		pa= pa.parentNode;		
    	}
    	var A=[L,T];
    	return A;
    }

  3. #3
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might argue that it is simpler just to make location.href = URL#anchor_name


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
  •