An open API service indexing awesome lists of open source software.

https://github.com/being-devahmad/scrolltotop

just code for scroll to top
https://github.com/being-devahmad/scrolltotop

Last synced: 3 months ago
JSON representation

just code for scroll to top

Awesome Lists containing this project

README

          

# ScrollToTop Component

This is a React component that adds a "Scroll to Top" button to your web page. The button appears when the user scrolls down the page and allows them to quickly return to the top of the page with a smooth scrolling animation.

## Features

- Appears after scrolling down 300 pixels
- Smooth scrolling animation
- Responsive design
- Customizable styling

## Usage

To use this component in your React project, simply import it and add it to your main layout or any page where you want the scroll-to-top functionality.

```jsx
import { useEffect, useState } from "react";

export default function ScrollToTop() {
const [isVisible, setIsVisible] = useState(false);

// Top: 0 takes us all the way back to the top of the page
// Behavior: smooth keeps it smooth!
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};

useEffect(() => {
// Button is displayed after scrolling for 500 pixels
const toggleVisibility = () => {
if (window.pageYOffset > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

window.addEventListener("scroll", toggleVisibility);

return () => window.removeEventListener("scroll", toggleVisibility);
}, []);

return (


{isVisible && (



)}

);
}