Skip to content

Commit

Permalink
Merge pull request #198 from banasmita24/add-scroll
Browse files Browse the repository at this point in the history
Added scroll button
  • Loading branch information
VesperAkshay authored Oct 14, 2024
2 parents db8a719 + f92e123 commit 9105fb5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { Toaster } from "react-hot-toast";
import ProtectedRoute from "./components/ProtectedRoute";
import NotFoundPage from "./components/Error404";

import ScrollButton from "./components/ScrollButton"; // Import the ScrollButton

function BackgroundWrapper({ children }) {
const location = useLocation();

Expand Down Expand Up @@ -136,6 +138,7 @@ export default function App() {
/>
<Route path="*" element={<NotFoundPage />} />
</Routes>
<ScrollButton />
</BackgroundWrapper>
<Toaster />
</Router>
Expand Down
9 changes: 9 additions & 0 deletions src/components/ScrollButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.scroll-button {
transition: opacity 0.3s ease;
opacity: 0.7;
}

.scroll-button.visible {
opacity: 1;
}

50 changes: 50 additions & 0 deletions src/components/ScrollButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useEffect, useState } from 'react';

const ScrollButton = () => {
const [isVisible, setIsVisible] = useState(false);

const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth', // Smooth scrolling
});
};

useEffect(() => {
window.addEventListener('scroll', toggleVisibility);
return () => {
window.removeEventListener('scroll', toggleVisibility);
};
}, []);

return (
<div
className={`scroll-button ${isVisible ? 'visible' : ''}`}
onClick={scrollToTop}
style={{
position: 'fixed',
right: '20px',
top: '50%',
transform: 'translateY(-50%)',
backgroundColor: 'blue',
color: 'white',
borderRadius: '50%',
padding: '10px',
cursor: 'pointer',
display: isVisible ? 'block' : 'none', // Show/hide based on scroll position
}}
>
</div>
);
};

export default ScrollButton;

0 comments on commit 9105fb5

Please sign in to comment.