-
Notifications
You must be signed in to change notification settings - Fork 43
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 #124 from AsmitaMishra24/main
Corrected 'Back-To-Top' Button
- Loading branch information
Showing
2 changed files
with
85 additions
and
18 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 |
---|---|---|
@@ -1,19 +1,84 @@ | ||
const FloatBtn = () => { | ||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: "smooth" | ||
}); | ||
}; | ||
|
||
return ( | ||
<button | ||
className="fixed bottom-10 right-5 bg-green-600 text-white border-none rounded-full w-12 h-12 text-2xl cursor-pointer flex justify-center items-center shadow-md transition-colors duration-300 hover:bg-gray-500" | ||
onClick={scrollToTop} | ||
> | ||
▲ | ||
</button> | ||
); | ||
'use client'; | ||
|
||
import React, { useState, useEffect } from "react"; | ||
import { animateScroll } from 'react-scroll'; | ||
import '@fortawesome/fontawesome-free/css/all.min.css'; | ||
|
||
const BackToTop = () => { | ||
const [showButton, setShowButton] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (window.scrollY > 90) { | ||
setShowButton(true); | ||
} else { | ||
setShowButton(false); | ||
} | ||
}; | ||
|
||
window.addEventListener("scroll", handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
|
||
const handleClick = () => { | ||
animateScroll.scrollToTop({ | ||
top: 0, | ||
behavior: "smooth", | ||
duration: 500 | ||
}); | ||
}; | ||
|
||
const buttonStyles = { | ||
position: 'fixed', | ||
bottom: '20px', | ||
right: '10px', | ||
zIndex: 1000, | ||
backgroundColor: '#7E22CE', | ||
color: '#ffffff', | ||
border: 'none', | ||
padding: '10px', | ||
fontSize: '14px', | ||
cursor: 'pointer', | ||
opacity: showButton ? 1 : 0, | ||
transition: 'opacity 0.1s ease', | ||
borderRadius: '60%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
width: '42px', | ||
height: '42px' | ||
}; | ||
|
||
const iconStyles = { | ||
fontSize: '20px' | ||
}; | ||
|
||
return ( | ||
<> | ||
<style>{` | ||
.back-to-top:hover { | ||
background-color: #f3e8ff !important; | ||
color: #7E22CE !important; | ||
} | ||
.back-to-top:focus { | ||
outline: none; | ||
} | ||
.back-to-top:active { | ||
transform: translateY(2px); | ||
} | ||
`}</style> | ||
<button | ||
className="back-to-top" | ||
onClick={handleClick} | ||
style={buttonStyles} | ||
> | ||
<i className="fas fa-arrow-up" style={iconStyles}></i> | ||
</button> | ||
</> | ||
); | ||
}; | ||
|
||
export default FloatBtn; | ||
export default BackToTop; |