views:

635

answers:

5

What are the different ways you can control/read the Scrollbar in a web browser with JavaScript, apart from anchors? -- Unless you can dynamically create anchors and tell the browser to scroll down to that.

+1  A: 

Here is one example of how you can control X/Y scrolling without anchors. ( ScrollX/ScrollY)

The key part I suppose is the following

function saveScrollCoordinates() { 
  document.Form1.scrollx.value = (document.all)?document.body.scrollLeft:window.pageXOffset; 
  document.Form1.scrolly.value = (document.all)?document.body.scrollTop:window.pageYOffset; 
}
Serapth
+1  A: 

To scroll by an arbitrary amount:

//Parameters being the amount by which to scroll in pixels (horizontal and vertical)
window.scrollBy(50, 50);

To scroll to a certain point:

 window.scrollTo(0, 30);

To read current position:

 document.body.scrollLeft
 document.body.scrollTop
Dmitri Farkov
I think you mean document.documentElement (normally refers to the <HTML> tag) instead of document.body.
J-P
No, the support of documentelement in IE is almost non-existent.
Dmitri Farkov
Where did you hear that? Works perfectly in IE6/7...
J-P
http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.htmlBoth are wrong depending on the browser, or are right. It's a bit of guesswork.
Dmitri Farkov
+1  A: 

Using the scrollTop property (inherent of all elements) you can scroll to a specific pixel height. So, scrolling to the height of a specific anchor would involve querying for the offset of that anchor and setting scrollTop accordingly. Just for illustration's sake; this is how you would scroll to a specified element with jQuery:

var top = $('div#something').offset().top;
$(document).scrollTop(top);

NOTE: jQuery's implementation can be misleading; it accepts document but the top-most element with the scrollTop property is, in fact, document.documentElement (usually refers to <HTML>).

There is also a scrollLeft property for horizontal scrolling.

And of course, you can read these properties:

var currentScrollTop = document.documentElement.scrollTop;
J-P
+1  A: 

Prototype implements a scrollTo() function that makes it really easy to scroll to a particular element:

$("#elementID").scrollTo();

The implementation internally calls window.scrollTo to do the actual scrolling.

molf
A: 

var coord = {top:null,left:null,width:null,height:null}; if (typeof window.pageYOffset == 'number') { coord.top = window.pageYOffset; coord.left = window.pageXOffset;
} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { coord.top = document.body.scrollTop; coord.left = document.body.scrollLeft; } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { coord.top = document.documentElement.scrollTop; coord.left = document.documentElement.scrollLeft; }

    if (typeof window.innerWidth == 'number') {
        coord.width = window.innerWidth; coord.height = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        coord.width = document.documentElement.clientWidth; coord.height = document.documentElement.clienthHeight; 
    } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        coord.width = document.body.clientWidth;
        coord.height = document.body.clientHeight;
    }
johndoe