Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript set scrollHeight

In JavaScript what's the right way to set the scrollHeight of one element to that of another element? Direct assignment has no effect. Thanks, Greg

like image 268
Greg Beddow Avatar asked Oct 22 '09 17:10

Greg Beddow


People also ask

What is scrollHeight in JavaScript?

The scrollHeight property returns the height of an element including padding, but excluding borders, scrollbars, or margins.

How do I set scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How is scrollHeight calculated?

scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar. The height is measured in the same way as clientHeight: it includes the element's padding, but not its border, margin or horizontal scrollbar.


1 Answers

It's not possible directly. The scrollHeight is a read only property that contain the total height of element content in pixels.

If have element A and you want to have element B with the same scrollHeight as element A, make it so that element B has a single child DIV element (move all previous element B content as child nodes of the DIV) that is set to:

width : 100%;
overflow : hidden;

and using javascript set the height of DIV to scrollHeight of element A (in pixels):

document.getElementById('B').childNodes.item(0).style.height = document.getElementById('A').scrollHeight + 'px';
like image 117
qbeuek Avatar answered Oct 13 '22 16:10

qbeuek