Code language: JavaScript (javascript) Smooth scrolling could be done using CSS scroll-behavior:smooth; but this isn't yet supported in Safari. Smooth scrolling when clicking an anchor link, jQuery - Scroll element to the middle of the screen instead of to the top with an anchor link, jQuery Smooth Scroll to Top AND to Anchor by ID, jQuery smooth scroll to anchor with a fixed menu only works when at page top. Scroll to an element smoothly - HTML DOM How to Scroll to Top, Bottom or Any Section in React with a Button How To Create a Scroll Back To Top Button - W3Schools window.scroll ( { top: $ (this).data ('y'), left: 0, behavior: 'smooth' }); This works great everywhere (including Android phones) but on Safari (desktop and iphone). direction of scroll on page. 1. Just click on the nav menu options . Scroll to Top in Vanilla JavaScript Joel Olawanle Introduction Whenever you have a website that prompts users to scroll down a lengthy amount - offering a button to scroll back to the top is a nice gesture! I checked in chrome and mozilla. The discussion has been locked. Scroll to an element and center it on the screen. Let's start by creating a scrollToTop () function that will contain all the required JavaScript: function scrollToTop() { // all JavaScript will go here }; Thank you , href="#top" for anchors that can do it without JS, location.hash = "top"; for JavaScript (if any). css3 html5: HTML5 CSS3 To make the button scroll the page to the top when we click it, we call addEventListener on the button. Smooth Scrolling | CSS-Tricks - CSS-Tricks I already have smooth scrolling to separate anchors in the page with a .js file attatched to my site but I cannot use an anchor for the top, because I'm using a template from a free hosting site with built in page building tools that do not allow me to edit above the body area. How to draw a grid of grids-with-polygons? Also Read:- CSS Counter We used a simple javascript function and scroll-behaviour property in this smooth scrolling. I've also used window.scrollTo(0, 0); but it scrolls instantly. If you wanted instant scrolling then just use: Tip: for slower motion of the scrolling, increase the hardcoded number 8. 2022 w3collective - Practical HTML, CSS, & JavaScript tutorials. // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; // For Safari document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera} Conclusion To smooth scroll to top with JavaScript, we call the window.scrollTo method. Finally heres some CSS to create a rounded button that fades in/out when the show class is toggled: You should now have a fully functioning scroll-to-top button to use in you next web project. Dom\after injection\ bottom. How to set smooth scroll after clicking the link using JavaScript This can be very useful when the page has a lot of content. Start the conversation! theMaxx answer works in nuxt/vue, smooth scrolling is default behavior,