From 00447070772d74c33d099eb3d1097fa9a549cd57 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sun, 17 Feb 2019 13:08:16 +0100 Subject: first draft --- backtop.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 backtop.js (limited to 'backtop.js') diff --git a/backtop.js b/backtop.js new file mode 100644 index 0000000..4bab48d --- /dev/null +++ b/backtop.js @@ -0,0 +1,17 @@ +// When the user scrolls down 20px from the top of the document, show the button +window.onscroll = function() {scrollFunction()}; +scrollFunction(); + +function scrollFunction() { + if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { + document.getElementById("myBtn").style.display = "block"; + } else { + document.getElementById("myBtn").style.display = "none"; + } +} + +// When the user clicks on the button, scroll to the top of the document +function topFunction() { + document.body.scrollTop = 0; // For Safari + document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera +} -- cgit v1.2.3