React Moving Scroll
Quick Start
Tutorials
Quick Start
✅ How to start?
import { useScroll } from "scroll-moving";
export default function App() {
const { handleScroll, ref } = useScroll();
const list = ["Click Me 1", "Click Me 2" ];
return (
<div>
<div onClick={(event) => handleScroll(event, list)}>
<button>Click Me 1</button>
<button>Click Me 2</button>
</div>
<div ref={ref(0)} style={{ height: "100vh" }}>Content 1</div>
<div ref={ref(1)} style={{ height: "100vh" }}>Content 2</div>
</div>
);
}
Import useScroll and get handleScroll and ref. The handleScroll is a function that provides scrolling and the ref is useRef for deciding where scroll should go. The function handleScroll would look for innerText that matches the text contained in the list.
If you clicked Click Me 1, the scroll would move to Content 1. Also, if you clicked Click Me 2, the scroll would move to Content 2. The string array, list, correspond to ref. The list[0] correspond to ref(0), and list[1] correspond to ref(1).
Note: If you used Next.js App Router, you should put 'use client' for using ref.
✅ handleScroll
It's a function to provide smooth scrolling to specific contents. It has two props, event and list.
name
type
description
event
React.MouseEvent<HTMLElement>
The function, handleScroll, should make click event. Therefore, MouseEvent should be prop of handleScroll.
list
string array
It would be button list. You can decide what button would trigger scrolling event by this list.
✅ ref
It's a reference that decide where scroll would go. You can use it the same way that using useRef. It has one parameter, index.
name
type
description
index
number
The ref would determine where scroll go. You can put index on ref on html tag, like <div> or something.