SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Using scrollTop

  1. #1
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using scrollTop

    I have this function:
    HTML Code:
    function getScrollTop() {
    	if (navigator.appName == "Microsoft Internet Explorer") {
    		return document.body.scrollTop;
    	} else {
    		return window.pageYOffset;
    	}
    }
    I picked it up somewhere. Now I'm using it like this:
    HTML Code:
    function recordScroll() {
    	currentScroll = getScrollTop();
    	alert(currentScroll);
    }
    With recordScroll being the window.onscroll event. But no matter where I am, currentScroll always gets set to 0.

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById(Obj).scrollTop what you need I think
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using an XHTML doctype, the scrollTop property now belongs to documentElement, and not body.

    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

    That should be the source of your headache.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    If you are using an XHTML doctype, the scrollTop property now belongs to documentElement, and not body.

    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

    That should be the source of your headache.
    Ahh of course, now it works! Thank you. Do you have any idea where I might find scrollTop in other browsers besides IE? I was under the impression it should be in window.pageYOffset, but has that also changed now that I'm using XHTML?

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just test it. Gecko's DOM reference does indicate that window.pageYOffset exists, although I think Gecko has supported scrollTop since 1.0 or earlier.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •