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 6 of 6

Thread: scrollTo method

  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    scrollTo method

    I have a couple of questions regarding the scrollTo method.

    1.) By default, the scrollTo method positions the browser so that the specified coordinates are in the upper left corner. Is there a way to make it so it places the coordinates in the top center?

    2.) Also by default, the scrollTo method instantaneously moves to the specified coordinates. What's the best way to slow this down for a smooth-scrolling effect?

    Any help is much appreciated!

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    Port Huron, MI, U.S.A.
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1.)scrollTo(Xcoord+(document.body.clientWidth/2),Ycoord);
    2.)use timers and an incrementing variable
    Oh, was I supposed to put something here? ........

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, very nice. Thank you!

  • #4
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just wrote this function. It will center the browser window on the input coordinate. I've tested it in Mozilla Firefox, Explorer, and Safari (on both PC and Mac where applicable).

    Code:
    function centerScroll(xx,yy){
    
    var xpos
    var ypos
    
    xpos = xx - document.body.clientWidth/2
    ypos = yy - document.body.clientHeight/2
    
    scrollTo(xpos, ypos)
    }

    I am still having one problem, however, in Safari. When I try to access this function using onLoad, it doesn't work at all. Here's my body tag:

    Code:
    <body onLoad="javascript:centerScroll(200,200)">

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Location
    Port Huron, MI, U.S.A.
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if it will work, but remove it and add to your script

    window.onload=new Function("centerScroll(200,200");
    //had to use new func so arguments would be okay
    Oh, was I supposed to put something here? ........

  • #6
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I've found a solution to my problem which also reveals something interesting about Safari.

    What was happening was that the code was working fine. The document was shifted to the point I specified. Immediately after this, and far too quickly to be seen, the page was redirected so that it was positioned to the upper left of the browser. This is some default feature in Safari, and for some reason, it first ran the javascript code THEN did the default positioning.

    The solution was to use a setTimeout. I could do this and set the time for as little as 1 millisecond, just long enough to initiate the javascript AFTER the instantaneous Safari positioning. setTimeout is a dirty solution, but it seems to work in all cases and on all browsers. Test it out here (and let me know if there are problems!):

    http://www.appfilmfest.org/draft1


  •  

    Posting Permissions

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