-
-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tamal Das <tamalcodes@gmail.com>
- Loading branch information
1 parent
389a288
commit 4bc2c5a
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.wrapper { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.top-btn { | ||
font-size: 2.4rem; | ||
width: 2.5rem; | ||
height: 2.5rem; | ||
color: black; | ||
background-color: #e76f51; | ||
border-radius: 10px; | ||
position: fixed; | ||
bottom: 5rem; | ||
right: 5rem; | ||
z-index: 999; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
} | ||
|
||
.top-btn:hover { | ||
background-color: transparent; | ||
border: 2px solid #e76f51; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useState } from "react"; | ||
import "../BacktoTop/BacktoTop.css"; | ||
import { BsArrowUpShort } from "react-icons/bs"; | ||
import { useEffect } from "react"; | ||
|
||
const GoToTop = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const goToBtn = () => { | ||
window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); | ||
return () => window.removeEventListener("scroll", listenToScroll); | ||
}; | ||
|
||
const listenToScroll = () => { | ||
let heightToHidden = 250; | ||
const winScroll = | ||
document.body.scrollTop || document.documentElement.scrollTop; | ||
if (winScroll > heightToHidden) { | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", listenToScroll); | ||
}, []); | ||
|
||
return ( | ||
<div className="wrapper"> | ||
{isVisible && ( | ||
<div className="top-btn" onClick={goToBtn}> | ||
<div className="icon-container"> | ||
{" "} | ||
<BsArrowUpShort className="icon" /> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default GoToTop; |