I have a button on a page and I want that when it is clicked, the page scrolls to the 0 position.
HTML
<button id="button">Back to top </button>
jquery
$(document).scroll(function(){
var scroll_pos = $(window).scrollTop()
if(scroll_pos > 10){
$('#button').click(function(){
// what code to enter here??
});
}
});
Try this code: $("#button"). on("click", function() { $("body"). scrollTop(0); });
If you need to scroll to the top of the page when a button is clicked, set the onClick prop on the element, passing it a function. The function should call the window. scrollTo() method just like we did in the useEffect hook.
Scroll one page at a time in all major browsers including Microsoft Internet Explorer and Mozilla Firefox by pressing the Spacebar key. Move back up the page by pressing Shift + Spacebar or the Home key on the keyboard.
Definition and UsageThe scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.
you can use window function for scrollTop
jquery
$("#button").click( function() {
$(window).scrollTop(0);
});
When you click button, this page scroll to top of the position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With