Advertisement
afsarwebdev

ScrollBottomToTop

Aug 22nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. HTML:
  2.  
  3. <button id=scrollme type="button">To the top</button>
  4.  
  5.  
  6. js:
  7.  
  8. function runScroll() {
  9. scrollTo(document.body, 0, 600);
  10. }
  11. var scrollme;
  12. scrollme = document.querySelector("#scrollme");
  13. scrollme.addEventListener("click",runScroll,false)
  14.  
  15. function scrollTo(element, to, duration) {
  16. if (duration <= 0) return;
  17. var difference = to - element.scrollTop;
  18. var perTick = difference / duration * 10;
  19.  
  20. setTimeout(function() {
  21. element.scrollTop = element.scrollTop + perTick;
  22. if (element.scrollTop == to) return;
  23. scrollTo(element, to, duration - 10);
  24. }, 10);
  25. }
  26.  
  27.  
  28.  
  29. //Another script for the Back To Top
  30.  
  31. $('#backToTop').each(function(){
  32. $(this).click(function(){
  33. $('html,body').animate({ scrollTop: 0 }, 'slow');
  34. return false;
  35. });
  36. });
  37.  
  38. For the Button :
  39.  
  40. if (scroller < 100) { $("#backToTop").css('transform', 'translate(0, 100px)'); }
  41. else { $("#backToTop").css('transform', 'translate(0, 0)'); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement