Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. (function() {
  2.  
  3. function createButton() {
  4.  
  5. var button = document.createElement("button");
  6.  
  7. button.classList.add("backToTop", "hidden");
  8. button.textContent = "Powrót do góry";
  9. document.body.appendChild(button);
  10.  
  11. return button;
  12.  
  13. }
  14.  
  15. var button = createButton();
  16.  
  17. function animateScroll() {
  18.  
  19. if(document.body.scrollTop > 0) {
  20. window.scrollBy(0, -5);
  21. setTimeout(animateScroll, 10);
  22. }
  23.  
  24. }
  25.  
  26. button.addEventListener("click", function(e) {
  27.  
  28. e.stopPropagation();
  29.  
  30. animateScroll();
  31.  
  32. }, false);
  33.  
  34. window.addEventListener("scroll", function(e) {
  35.  
  36. if(document.body.scrollTop >= 100) {
  37. button.classList.remove("hidden");
  38. } else {
  39. button.classList.add("hidden");
  40. }
  41.  
  42. }, false);
  43.  
  44.  
  45. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement