-
Notifications
You must be signed in to change notification settings - Fork 0
/
java.js
36 lines (29 loc) · 1.1 KB
/
java.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
31
32
33
34
35
36
// We select the element we want to target
var target = document.getElementById("target");
var scrollToTopBtn = document.querySelector(".scrollToTopBtn")
var rootElement = document.documentElement
// Next we want to create a function that will be called when that element is intersected
function callback(entries, observer) {
// The callback will return an array of entries, even if you are only observing a single item
entries.forEach(entry => {
if (!entry.isIntersecting) {
// Show button
scrollToTopBtn.classList.add("showBtn")
} else {
// Hide button
scrollToTopBtn.classList.remove("showBtn")
}
});
}
function scrollToTop() {
rootElement.scrollTo({
top: 0,
behavior: "smooth"
})
}
scrollToTopBtn.addEventListener("click", scrollToTop);
// Next we instantiate the observer with the function we created above. This takes an optional configuration
// object that we will use in the other examples.
let observer = new IntersectionObserver(callback);
// Finally start observing the target element
observer.observe(target);