Page 1 of 1

Focus on a particular portion of a web page using javascript

Posted: 18 Dec 2014, 15:59
by Marius
How to focus on particular part of a web page by using javascript events?
Let's say I clicked a button and it will scroll down or scroll up to a particular portion a page.

Focus on a particular portion of a web page using javascript

Posted: 18 Dec 2014, 16:10
by MarPlo
Hi,
To scroll to a specified element, use the element.scrollIntoView() method (this method scrolls the current element into the visible area of the browser window):

Code: Select all

<script>
var element = document.getElementById('elm_id');
element.scrollIntoView();
</script>
- To scroll to a pixel location, you can use the scrollTop property

Code: Select all

<script>
document.body.scrollTop = 800;    // 800 pixels from top of the page
</script>