SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help finding scroll bar offset

    i started out using
    Code JavaScript:
    window.pageYOffset
    to find out how many pixels vertically have been scrolled from the top.. but, of course, this doesn't work on all browsers. Does anybody know a way to do this cross browser or other functions for other browsers?

    thanks in advance

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    function getScrollingPosition (){
    	var position = [0, 0];
    	if (typeof window.pageYOffset != 'undefined'){
    		position = [window.pageXOffset,window.pageYOffset];
    	}
    	if (typeof document.documentElement.scrollTop != 'undefined'
    		&& document.documentElement.scrollTop > 0){
    		position = [document.documentElement.scrollLeft,document.documentElement.scrollTop];
    	} else if(typeof document.body.scrollTop != 'undefined'){
    		position = [document.body.scrollLeft,document.body.scrollTop];
    	}
    	return position;
    }

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing wrong with ize's script, but it could be a little more compact,
    since all you want is the scroll top value:

    Code:
    function getScrollTop(){
      if(typeof pageYOffset=='number')return pageYOffset;
    
      var D= document.documentElement;
      D= (D && D.clientHeight)? D: document.body;
      return D.scrollTop;
    }

  4. #4
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys!, i'm going to try those out


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
  •