Focus on a particular portion of a web page using javascript

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 65

Focus on a particular portion of a web page using javascript

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.

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>

Similar Topics