Scroll web page to top page in jquery năm 2024

This post shows a simple way to make anchor tags with e.g. href="

top" scroll to the top of the webpage when clicked using jQuery. Note as per the comment by Jono you can use the id of an element to scroll to. Have a read through this post then a look at his comment.

The Javascript

The following Javascript will change all links to scroll to the top instead of the instant jump that normally happens when the link is clicked.

$(document).ready(function() {

$('a[href=
# top]').click(function(){
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
});
});

Note the "return false;" line. This is required otherwise the default action will be done and the instant scroll will happen instead of the nice animated scrolling effect.

The position the document is scrolled to can be different by changing the scrollTop value, the speed by changing the ‘slow’ value and what appears in the href by chaging the

href value.

Note that an anchor should still be present in the page for browsers that do not have Javascript on or support jQuery.

The HTML

... Back to top

The does not have to be present, but see the note above about supporting browsers without Javascript on etc.

You could just link to an anchor tag e.g. and

but by using jQuery you can have a smooth animation instead.

Browser versions supported

This technique works in all modern browsers. I tested in a number of browsers including IE6 and FF1 using the HTML 5 DOCTYPE.

jQuery version requirements

jQuery 1.2 or higher is required. I’ve tested this in 1.2, 1.2.6, 1.3.2, 1.4.4, 1.5.2, 1.6.2

Scroll to the bottom of a page

The technique is exactly the same for scrolling to the bottom of a page, but scroll to the height of the document instead. I’ve covered how to do this in the post Scroll to the bottom of a page with jQuery.

Working example

Click through here to a full working example which shows scrolling to both the bottom and top of the page. View the source of that page to get the full HTML required.

Scroll to Top is very useful and used feature on many websites. With the scroll to top button, we can enable website visitors to jump back to the top section of our website with a single click.

With the javascript scrolltotop button, you can boost page ranking to the top of search results by making your website easily navigatable. Speaking of good navigation, check out our Crud in CodeIgniter 4 latest version.

Here we will show you how to make a custom Javascript scroll to top. Or you use jQuery as well. We will teach you both methods and a bonus method.

So without further ado, let’s get started.


Using jQuery Method

Using the jQuery method, your code is easy to read and understand. And since jQuery is a highly popular library and used in all kinds of websites, this method is one of the best.

Then let’s get to the coding section:

index.html

Scroll to Top

Scroll To Top

Scroll web page to top page in jquery năm 2024
Scroll web page to top page in jquery năm 2024
Scroll web page to top page in jquery năm 2024
Scroll to Top

We have added the dummy images so our HTML page becomes a little longer for scrolling to properly work. Also, we added some basic styling for our ScrollToTop button in style.css.

Now actual logic is written in our “app.js” file. Let’s explain it:

  1. First, we track the click of our tag using the click() function of jQuery.
  2. Next, we have added the animate() function which allows us the smooth scroll when we implement scrollTop to zero.
  3. Voila! You have successfully implemented js scroll to top of your webpage.

$("a").click(function (e) { e.preventDefault(); $('html, body').animate({ scrollTop: 0 }, 'slow'); });

Feel free to check this whole code. You can download it from our Github Repo directly with this link:

Want more?

What if you want to activate your scroll-to-top functionality after a certain distance is scrolled down on your webpage? Then just make these small modifications to your jQuery code:

Using the jQuery scroll() function we first determine if enough page is scrolled. Then only we start showing the scrolltotop button to website visitors. And once we reach the top of the page, the button disappears again.

$(window).scroll(function () { if ($(window).scrollTop() >= 600) {

$("a").css("display", "block");
} else {
$("a").css("display", "none");
} }); $("a").click(function (e) { $('html, body').animate({ scrollTop: 0 }, 'slow'); });


JavaScript ScrollToTop

Using javascript scrolltotop is ideal in a situation where you cannot use the jQuery library. There are multiple ways to achieve this. Here are the 3 most used methods to build javascript scrolltotop:

With window.onscroll()

This is the easiest method when using only javascript in your project.

With document.documentElement.scrollTop()

If for any reason, our previous Javascript scrolltotop method didn’t work. Feel free to try this method. Check out the below CodePen where we give you a live example:

With document.documentElement.scrollIntoView()

Lastly, try this new javascript scrolltotop method using the document object modal. We explain it in the below CodePen example:

Note: Adding “scroll-behavior: smooth” is a must in your CSS file. Without this code, your scrolling animation will be instantaneous and will feel jerky.

If you are looking to bling up your website with Page Scroll animations, check out our post on 6 methods to implement page scroll animation.

Check out our code at Github Repo directly:


Scroll to Top with No Javascript(using HTML fragments)

HTML fragments allow scrolling to any section of a webpage linked with the “id” and “

id”> tag” combination. So we can use this combo to simulate scrolling to the top without js.

But we don’t recommend using it professionally in your project.

Still, this way you can see that HTML fragments are a pretty powerful way for navigating your website section.

Scroll to Top

Scroll To Top

Scroll web page to top page in jquery năm 2024
Scroll web page to top page in jquery năm 2024
Scroll web page to top page in jquery năm 2024
Scroll to Top

By the way, if you are trying to learn how to build an HTML form with PHPmailer check out this guide.

You can check this code in our Github repo:


Conclusion

We highly recommend using the jQuery method because it’s short and less verbose. Use Javascript Scroll To Top functionality only when your project doesn’t need jQuery at all.

We hope you enjoyed our article on how to build a js scroll to top button for your website. With this knowledge, we know that you can implement scrolling to the top of your website to make it easier for your customers to find important information.

So what are you waiting for? Check out our other post where we build an Image Format Converter. Also if you are a fan of Isekai Anime, check our article on 15 Isekai Anime with OP mc.

Thanks so much for reading. If you have any doubts, comment and ask us. We always try to give prompt replies. Keep coding! Ta-da!

How do I scrollTo the top of my screen?

Alt+Ctrl+PageUp and Alt+Ctrl+PageDown are the built-in keyboard shortcuts to move the insertion point to the top and bottom of the window.

How do I make my HTML page scrollTo the top?

A standard HTML tag will suffice... +1 This is good if you need to navigate to specific element rather just to the top. Use 'Top' to jump to the top of the page.

How do you auto scrollTo the top of the page in JavaScript?

To make the page jump on the top instantly can be done with the native JavaScript method, namely — window. scrollTo(x-coord, y-coord) . This property of the window interface scrolls to a particular set of coordinates in the document.

How do you jump to the top of the page?

Please use Ctrl + Home command from your keyboard to scroll to the top of the current page.