-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtop-to-bottom.tsx
More file actions
41 lines (36 loc) · 1.09 KB
/
top-to-bottom.tsx
File metadata and controls
41 lines (36 loc) · 1.09 KB
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
37
38
39
40
41
import React, { useEffect, useState } from "react";
import { FaArrowDown } from "react-icons/fa";
export default function ScrollTopToBottom() {
const [showButton, setShowButton] = useState(false);
const scrollToBottom = () => {
window.scrollTo({
top: document.documentElement.scrollHeight,
behavior: "smooth",
});
};
const handleScroll = () => {
const bottomThreshold =
document.documentElement.scrollHeight - window.innerHeight - 100;
if (window.scrollY < bottomThreshold) {
setShowButton(true);
} else {
setShowButton(false);
}
};
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
return (
showButton && (
<button
onClick={scrollToBottom}
className="fixed right-5 bottom-5 z-50 cursor-pointer rounded-lg border-none bg-green-600 p-2.5 text-white opacity-80 shadow-md transition-opacity duration-300 hover:bg-green-700 hover:opacity-100"
>
<FaArrowDown />
</button>
)
);
}