Forum Moderators: open

Message Too Old, No Replies

vertical scroll question

         

scorpion

5:11 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



I know this sounds strange but is it possible to programatically load a page in such a way that it loads with the page scrolled vertically just slightly? That is, is it possible to tell the browser in javascript to start say at the middle of a page that has a long vertical scroll...

DrDoc

5:18 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have the exact code, but yes, it is possible. There are several different JavaScript functions that would do it. Search for scrollTo/scrollBy/scroll.

Not sure if you can assign a percentage though. That would be really neat ;)

Jocelyn

7:09 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



Just do:
document.location = "page.html#anchor";

And be sure that the anchor is defined somewhere in page.html:
<a name="anchor"></a>

Jocelyn

DrDoc

8:13 pm on Mar 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But that only scrolls horizontally, it won't scroll the page vertically, will it?
And, even if it does, it won't center the page...

Jocelyn

9:15 pm on Mar 25, 2003 (gmt 0)

10+ Year Member



it will scroll what it needs so that the anchor is displayed on the screen.
In most cases it means a vertical scroll. This is what happens too when you click a regular link :
<a href="mypage.html#anchor">Click here</a>

If you put your anchor very much on the right side, it will scroll horizontally as well. You can center the page the way you want, if you set the anchor at the correct position.

Jocelyn

tedster

1:02 am on Mar 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



scrollTo/scrollBy/scroll. Not sure if you can assign a percentage...

No, these JavaScript methods only take pixels for their arguments. scroll() is deprecated in favor of the two newer methods:

window.scrollTo(x,y)
x and y are the exact coordinates of the top right corner of the window after the scroll is executed.

window.scrollBy(dx,dy)
dx and dy are the number of pixels to scroll in each dimension

DrDoc

5:19 am on Mar 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The number of pixels could easily be calculated, though. I assume the window has a fixed width, and you can check for actual window width... It's just a matter of math :)