video tutoriel Traduction WPML site wordpress – Droit et Croissance
video tutoriel Traduction WPML site wordpress – Droit et Croissance
The scroll bar or scrolling feature determines the position in which the scrolling takes place. A scroll bar can move horizontally as well as vertically. The horizontal scroll bar allows us to scroll the content horizontally i.e., left or right. While the tieso scroll bar allows us to scroll the content vertically i.e., up or down.
Now to question is how to enable tieso scrolling in JavaScript or jQuery so that whenever a user clicks on a button, the page scrolls to the topmost position? Well! We have a couple of approaches that can be used to accomplish this task.
This post will explain the working of the below-listed approaches to scroll the page to the topmost position:
So, let’s get started!
In JavaScript, the window interface provides a built-in method named scrollTo() that can be used to scroll to some particular position on the page.
You to follow the below syntax in order to work with the scrollTo() method:
1 |
window.scrollTo(x–coordinate, y–coordinate); |
The above snippet shows that the window.scrollTo() method accepts x-coordinate and y-coordinate as parameters. If we specify both coordinates as “0” then the scrollTo() method will move/scroll the page to the topmost point.
1 |
<html>
<body> <style> p { background-color: antiquewhite; } </style> <h1 style=«background-color: black; color: white; text-align: center;»> |
The above program performed the below-given tasks:
The output verified that clicking the button scrolled the page to the topmost position.
Jquery provides a method named “scrollTop()” that is used to return/set the tieso scrollbar position for the targeted element. Position 0 represents that the scrollbar is on the top. So, we have to pass “0” as an argument to the “scrollTop()” method in order to scroll back to the top of the page.
Follow the below-given syntax to get the tieso scrollbar position:
1 |
$(selector).scrollTop(); |
Follow the below-given syntax to set the tieso scrollbar position:
1 |
$(selector).scrollTop(position); |
Let’s consider the following code block to understand the working of the scrollTop() method:
1 |
<html>
<body> <style> p { background-color: antiquewhite; } </style> <h1 style=«background-color: black; color: white; text-align: center;»> Welcome to linuxhint </h1> <h3 style=«background-color: coral; color: white; text-align: center;»> Anees Asghar </h3> </h3> <p> How to scroll to the top of the page using JavaScript/jQuery </p> <p style=«height: 500px;»> Click on the «Click here!» button to scroll back on the top of the page using jQuery </p> <button onclick=«topFun()»> Click Here! </button> <script src=«https://code.jquery.com/jquery-3.3.1.min.js»> </script> <script> function topFun() { $(window).scrollTop(0); } </script> </body> </html> |
The above code block performed the following functionalities:
This is how the scrollTop() method works in jQuery
In JavaScript, passing “0, 0” as parameter to the window.scrollTo() method will scroll the page to the topmost position. In jQuery passing “0” as an argument to the “scrollTop()” method will scroll the page to the topmost position. This post considered a couple of examples to provide a detailed knowledge about the window.scrollTo() and scrollTop() methods.