Javascript: Back To Top Script (Easing)


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function backToTop() {
  2. var x1 = x2 = x3 = 0;
  3. var y1 = y2 = y3 = 0;
  4. if (document.documentElement) {
  5. x1 = document.documentElement.scrollLeft || 0;
  6. y1 = document.documentElement.scrollTop || 0;
  7. }
  8. if (document.body) {
  9. x2 = document.body.scrollLeft || 0;
  10. y2 = document.body.scrollTop || 0;
  11. }
  12. x3 = window.scrollX || 0;
  13. y3 = window.scrollY || 0;
  14. var x = Math.max(x1, Math.max(x2, x3));
  15. var y = Math.max(y1, Math.max(y2, y3));
  16. window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
  17. if (x > 0 || y > 0) {
  18. window.setTimeout("backToTop()", 25);
  19. }
  20. }
  21.  
  22.  
  23.  
  24. <a href="#" onclick="backToTop(); return false">Back to top</a>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.