Skip to content

Commit

Permalink
add scroll button
Browse files Browse the repository at this point in the history
  • Loading branch information
techmannih committed Nov 25, 2024
1 parent 4eeb0ac commit eaefc1c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
45 changes: 45 additions & 0 deletions components/Scrollbutton/ScrolButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useEffect, useState } from 'react';
import Image from 'next/image';

function ScrollButton() {
const [backToTopButton, setBackToTopButton] = useState(false);

useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 150) {
setBackToTopButton(true);
} else {
setBackToTopButton(false);
}
};

window.addEventListener('scroll', handleScroll);

// Clean up the event listener on component unmount
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, []);

const scrollUp = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
};

return (
<div className="fixed bottom-14 right-4 h-16 w-12 z-40">
{backToTopButton && (
<button
className="rounded-full transition-transform hover:-translate-y-2 duration-150 ease-in-out shadow-lg bg-white"
onClick={scrollUp}
>
<Image alt="scroll up" width={40} height={40} src="/img/scroll.svg" />
</button>
)}
</div>
);
}

export default ScrollButton;
60 changes: 60 additions & 0 deletions 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 pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import speakers from '../config/speakers.json';
import Link from 'next/link';
import Button from '../components/Buttons/button';
import Dropdown from '../components/Dropdown/dropdown';
import ScrollButton from '../components/Scrollbutton/ScrolButton'; // Adjust the import path if needed


export default function Home() {
const isTablet = useMediaQuery({ maxWidth: '1118px' });
Expand Down Expand Up @@ -191,6 +193,7 @@ export default function Home() {
<div className='mt-5'>
<Subscription/>
</div>
<ScrollButton />
</div>
</div>
);
Expand Down
14 changes: 14 additions & 0 deletions public/img/scroll.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eaefc1c

Please sign in to comment.