-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from MastanSayyad/dev
Re-Designed and Enhanced "Back to Top" Button Aesthetics and Functionality
- Loading branch information
Showing
5 changed files
with
138 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,49 +1,58 @@ | ||
import React from 'react' | ||
import "./BottomToTop.css" | ||
import { | ||
useState, | ||
useEffect | ||
} from 'react'; | ||
import { animateScroll as scroll } from 'react-scroll'; | ||
import React, { useState, useEffect } from "react"; | ||
import styled from "styled-components"; | ||
import { FaArrowUp } from "react-icons/fa"; | ||
|
||
const BackToTop = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
const GoToTop = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (window.scrollY > 100) { | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
const currentScrollY = window.scrollY; | ||
setIsVisible(currentScrollY > 200); // Change 200 to your desired scroll distance | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
|
||
const backToTop = () => { | ||
// window.scrollTo({ | ||
// top: 0, | ||
// behavior: 'smooth', | ||
// }); | ||
scroll.scrollToTop({ | ||
duration: 500, | ||
smooth: "easeInOutQuad", | ||
}); | ||
window.addEventListener("scroll", handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
|
||
const goToTop = () => { | ||
window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); | ||
}; | ||
|
||
return ( | ||
<> | ||
{isVisible && ( | ||
<Wrapper onClick={goToTop}> | ||
<FaArrowUp className="top-btn--icon" /> | ||
</Wrapper> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
position: fixed; | ||
bottom: 17px; | ||
right: 10px; | ||
color: white; | ||
background-color: rgb(59 130 246); | ||
width: 48px; | ||
height: 48px; | ||
z-index: 9999; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
transition: transform 0.3s ease, background-color 0.3s ease; | ||
&:hover { | ||
background-color: #003a79; | ||
transform: translateY(-5px); | ||
} | ||
`; | ||
|
||
return (< | ||
div className={ | ||
`scroll-to-top-button ${isVisible ? 'visible' : ''}` | ||
} | ||
onClick={ | ||
backToTop | ||
} > | ||
<i class="ri-arrow-up-s-line" > </i> </div> | ||
) | ||
} | ||
|
||
export default BackToTop | ||
export default GoToTop; |