how do implment scrollTo certain section ? #541
Unanswered
sandeshsapkota
asked this question in
Q&A
Replies: 1 comment
-
Hi 👋 You have to store import { useEffect, useRef } from "react";
import SmoothScrollbar from "smooth-scrollbar";
import OverscrollPlugin from "smooth-scrollbar/plugins/overscroll";
const overscrollOptions = {
enable: true,
effect: "glow",
damping: 0.15,
maxOverscroll: 150,
glowColor: "#E5E5E5",
};
const options = {
damping: 0.07,
plugins: {
overscroll: { ...overscrollOptions },
},
};
const Scroll = ({ children }: { children: React.ReactNode }) => {
const scrollbarInstance = useRef<SmoothScrollbar | null>(null);
const mainScrollbar = useRef<HTMLElement | null>(null);
useEffect(() => {
if (typeof window === "undefined") {
return;
}
if (mainScrollbar.current) {
/*** Store scrollbar instance ***/
scrollbarInstance.current = SmoothScrollbar.init(mainScrollbar.current!, options);
}
return () => {
if (scrollbarInstance.current) scrollbarInstance.current.destroy();
};
}, []);
function scrollToSpecificSection() {
/*** Scroll to a certain section ***/
scrollbarInstance.current.scrollIntoView(el, options)
}
return (
<div ref={mainScrollbar}>
<div>{children}</div>
</div>
);
};
export default Scroll; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi !
I have a button when clicking I want to scroll to certain section. I can see somewhere that smooth scrollbar provides the function to do the
scrollTo
functionality.I have implemented the scrollbar functionality in react.
But It shows me error when I do the following
scrollbar implementation.
here is the code for the Scroll component
Beta Was this translation helpful? Give feedback.
All reactions