How to focus on a specific part of a web page using javascript?

Can someone tell me how to focus on a specific part of a webpage using javascript events? Say I clicked on a button and it scrolls down or scrolls the page to a specific part. Here is my incomplete code:

<html> <head> <script type="text/javascript"> function focusOnForm() { document.getElementById('form').style.display = ''; } </script> </head> <body onload="document.getElementById('form').style.display = 'none';"> <div style="height: 900px; width: width: 500px; background-color: #000;"> </div> <br /> <input type="button" value="Add" style="float: right;" onclick="focusOnForm();" /> <br /> <div id="form"> First Name: <input type="text" /> <br /> Last Name: <input type="text" /> <br /> Address: <input type="text" /> <br /> Contact Number: <input type="text" /> <br /> Email: <input type="text" /> <br /> </div> </body> </html> 

I really need some help. Thanks in advance.

+4
source share
2 answers

Use the .scrollIntoView() method:

  function focusOnForm() { var form = document.getElementById('form'); form.style.display = ''; form.scrollIntoView(); } 

Fiddle: http://jsfiddle.net/g8Mqr/

+2
source

You can scroll to the pixel location with:

 document.body.scrollTop = 2000; 

http://jsfiddle.net/ThinkingStiff/A7JMZ/

+2
source

Source: https://habr.com/ru/post/1379794/


All Articles