forked from Kritika30032002/To-Do-List-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
30 lines (27 loc) · 810 Bytes
/
scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
document.addEventListener("DOMContentLoaded", function() {
let mybutton = document.getElementById("myBtn");
window.addEventListener("scroll", scrollFunction);
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
mybutton.addEventListener("click", function() {
// Check for browser support
if ("scrollBehavior" in document.documentElement.style) {
// Smooth scroll to the top
window.scrollTo({
top: 0,
behavior: "smooth"
});
} else {
// Fallback for browsers that do not support smooth scrolling
window.scrollTo({
top: 0,
behavior: "smooth"
});
}
});
});