The following examples show different implementations of a "Back to Top" button using HTML, CSS, and JavaScript. These buttons allow users to quickly navigate back to the top of a webpage after scrolling down. Unlike the second example, the button is constantly visible
The first example takes the much simpler approach of using just HTML and CSS to create a fixed button that appears when the user scrolls down the page. When the user clicks the button, the page smoothly scrolls back to the top.
The button's click event triggers the scrollTo method on it's parent DIV element to provide a smooth scrolling effect. This method can be used on the body of the webpage or any scrollable element.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
The second example uses JavaScript to create a button that only appears after the user has scrolled down the content a certain distance. Like in the previous example, the button is fixed to the bottom right corner of the viewport and smoothly scrolls the page back to the top when clicked.
However, because we've provided a user-definable custom attribute 'scroll-distance' (in pixels) for our button, it's good ettiquete to not use attach the attribute to a standard HTML button element, but to create our own custom HTML element that acts like a button. This helps to avoid confusion for other developers who might expect the 'scroll-distance' attribute to be on a standard HTML button when it isn't.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
The second button is much more flexible than the first in that, not only can the scroll distance be defined by the user, but the way in which the button appears can be customised by other means, such as fading in, sliding in, etc. You might also want to make it so that the scroll-distance attribute relates to a percentage of the vertical height scrolled, rather than a fixed pixel value.
For a more complete set of examples with code comments, please download the git repository found HERE.