Skip to content

Commit

Permalink
Added Smooth Scrolling in B2T (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shariq2003 authored Oct 18, 2024
1 parent 80e4c3f commit 842e96d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions webmasterlog/src/components/shared/ScrollToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const ScrollToTop = () => {
}
};

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
const smoothScrollToTop = () => {
const scrollY = window.pageYOffset;
const scrollStep = Math.max(10, Math.floor(scrollY / 20));
if (scrollY > 0) {
window.scrollBy(0, -scrollStep);
requestAnimationFrame(smoothScrollToTop);
}
};

useEffect(() => {
Expand All @@ -30,7 +32,7 @@ const ScrollToTop = () => {
<div>
{isVisible && (
<Button
onClick={scrollToTop}
onClick={() => requestAnimationFrame(smoothScrollToTop)}
className="fixed bottom-4 right-4 p-3 bg-blue-500 text-white rounded-full shadow-md hover:bg-blue-600 transition"
>
Expand Down

0 comments on commit 842e96d

Please sign in to comment.