DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Back to Top in Pure JavaScript

Back to Top in Pure JavaScript

A popular trend you see on many websites is the ''back to top'' button where the user will click this button to automatically scroll back to the top of a page. Author Paul Lundberg shows us how to do it in pure JS.

Paul Underwood user avatar by
Paul Underwood
·
Aug. 31, 16 · Tutorial
Like (4)
Save
Tweet
Share
17.47K Views

Join the DZone community and get the full member experience.

Join For Free

A popular trend you see on many websites is the ''back to top'' button where the user will click this button to automatically scroll back to the top of a page.

In a previous tutorial (Back To Top With jQuery), I revealed how you can do this using jQuery using the animate function to smooth scroll the browser back to the top.

I used this script a lot over the years on multiple new websites I worked on, but recently I've been putting a lot more effort into the performance of websites and working on how you can speed up the page loading times. One of the biggest things I've been working on is removing the need to load jQuery into my application and simply using pure JavaScript.

I know there's a place for jQuery and I won't say you don't need to use it at all, but if all you're doing is simple click events then normally there isn't a need to use jQuery. Therefore, I decided to look again at this jQuery smooth scroll to top function and achieve the same thing by using pure JavaScript and removing the dependency on jQuery.

The HTML

First, you need to have an HTML element on the page for the user to click. This can be a button, a div, or a span, anything that you can add a click event onto.

<div class="back-to-top pointer" onclick="scrollToTop();return false;">Back to top ^</div>


Notice the onclick attribute in the above HTML, this will now run the scrollToTop() function.

For this function, we want to scroll the user back to the top, but to make it look like smooth scrolling, we will need to scroll the page slowly in increments.

First, we check to see if the page is already at the top of the window, if not then we continue with the script.

if (document.body.scrollTop!=0 || document.documentElement.scrollTop!=0)
{

}


We then take the window and scroll -50px to the top of the window.

Image title


After scrolling up 50px, we need to set a timeout to repeat this scrollToTop() function every 10 milliseconds. This will create the smooth scroll effect back to the top of the page until it lands back to vertical position 0.

Image title


JavaScript

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • (Deep) Cloning Objects in JavaScript
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Apache Kafka vs. Memphis.dev

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: