Skip to main content
scrollTop is not a function, edit example to promote better coding practices
Source Link

You need to get the top offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container):

var topPosmyElement = document.getElementById('element_within_div');
var topPos = myElement.offsetTop;

The variable topPos is now set to the distance between the top of the scrolling div and the element you wish to have visible (in pixels).

Now we tell the div to scroll to that position using scrollTop()scrollTop:

document.getElementById('scrolling_div').scrollTop = topPos;

If you're using the prototype JS framework, you'd do the same thing like this:

var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];

Again, this will scroll the div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

You need to get the top offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container):

var topPos = document.getElementById('element_within_div').offsetTop;

The variable topPos is now set to the distance between the top of the scrolling div and the element you wish to have visible (in pixels).

Now we tell the div to scroll to that position using scrollTop():

document.getElementById('scrolling_div').scrollTop = topPos;

If you're using the prototype JS framework, you'd do the same thing like this:

var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];

Again, this will scroll the div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

You need to get the top offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container):

var myElement = document.getElementById('element_within_div');
var topPos = myElement.offsetTop;

The variable topPos is now set to the distance between the top of the scrolling div and the element you wish to have visible (in pixels).

Now we tell the div to scroll to that position using scrollTop:

document.getElementById('scrolling_div').scrollTop = topPos;

If you're using the prototype JS framework, you'd do the same thing like this:

var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];

Again, this will scroll the div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

Provided a non prototype method as well...
Source Link
Brian Barrett
  • 5.1k
  • 2
  • 19
  • 3

You need to get the top offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container):

var topPos = document.getElementById('element_within_div').offsetTop;

The prototype JS framework actually makes this pretty easyvariable topPos is now set to the distance between the top of the scrolling div and the element you wish to have visible (in pixels). Not sure if

Now we tell the div to scroll to that position using scrollTop():

document.getElementById('scrolling_div').scrollTop = topPos;

If you're using it but maybe you'll findthe prototype JS framework, you'd do the same thing like this helpful:

var positionArrayposArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = positionArray[1];posArray[1];

ThisAgain, this will scroll the div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

The positionedOffset returns an array in the form of [leftValue, topValue]. Also accessible as properties: { left: leftValue, top: topValue }.

You really don't need the prototype framework to do this of course, so long as you can get the offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container).

The prototype JS framework actually makes this pretty easy. Not sure if you're using it but maybe you'll find this helpful:

var positionArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = positionArray[1];

This will scroll div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

The positionedOffset returns an array in the form of [leftValue, topValue]. Also accessible as properties: { left: leftValue, top: topValue }.

You really don't need the prototype framework to do this of course, so long as you can get the offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container).

You need to get the top offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container):

var topPos = document.getElementById('element_within_div').offsetTop;

The variable topPos is now set to the distance between the top of the scrolling div and the element you wish to have visible (in pixels).

Now we tell the div to scroll to that position using scrollTop():

document.getElementById('scrolling_div').scrollTop = topPos;

If you're using the prototype JS framework, you'd do the same thing like this:

var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];

Again, this will scroll the div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

Source Link
Brian Barrett
  • 5.1k
  • 2
  • 19
  • 3

The prototype JS framework actually makes this pretty easy. Not sure if you're using it but maybe you'll find this helpful:

var positionArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = positionArray[1];

This will scroll div so that the element you wish to see is exactly at the top (or if that's not possible, scrolled as far down as it can so it's visible).

The positionedOffset returns an array in the form of [leftValue, topValue]. Also accessible as properties: { left: leftValue, top: topValue }.

You really don't need the prototype framework to do this of course, so long as you can get the offset of the element you'd like to scroll into view, relative to its parent (the scrolling div container).