-
Notifications
You must be signed in to change notification settings - Fork 128
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 #224 from SurajPrakash24/scroll-to-top
closes: #211 Added scroll to top button
- Loading branch information
Showing
3 changed files
with
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.scroll-to-top { | ||
position: fixed; | ||
bottom: 20px; | ||
right: 20px; | ||
z-index: 1000; | ||
} | ||
|
||
.scroll-btn { | ||
/* background-color: #007bff; */ | ||
background-color: #723aeb; | ||
color: white; | ||
border: none; | ||
padding: 10px 20px; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
font-size: 20px; | ||
font-weight: bold; | ||
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.4); | ||
opacity: 0.9; | ||
} | ||
|
||
.scroll-btn:hover { | ||
opacity: 1; | ||
} | ||
|
||
.scroll-btn:focus { | ||
outline: none; | ||
} |
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,42 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import './scrollToTop.css'; | ||
|
||
const ScrollToTopButton = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
// Show or hide the button based on scroll position | ||
const toggleVisibility = () => { | ||
if (window.scrollY > 200) { | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
// Scroll the window back to the top | ||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth' | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', toggleVisibility); | ||
return () => { | ||
window.removeEventListener('scroll', toggleVisibility); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className="scroll-to-top"> | ||
{isVisible && ( | ||
<button onClick={scrollToTop} className="scroll-btn"> | ||
↑ | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ScrollToTopButton; |
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