Next.js Discord

Discord Forum

NextJs 13.4.9 change the page when user scrolls down and up

Unanswered
Arboreal ant posted this in #help-forum
Open in Discord
Arboreal antOP
I having issue here, in my website if a user scrolls down ffrom home page then he /she must be redirected to another page and vice versa

this is the code I'm trying but nothing seems to happen

'use client'
import React, { useEffect } from 'react'
import styles from '@/styles/home.module.scss'
import Link from 'next/link'
import { useRouter } from 'next/navigation';

export default function Home() {
const router = useRouter();
const scrollHandler = () => {
const scrollPosition = window.pageYOffset;

if (scrollPosition === 0) {
router.push('/');
} else {
router.push('/services');
}
};
useEffect(() => {
window.addEventListener('scroll', scrollHandler);

return () => {
window.removeEventListener('scroll', scrollHandler);
};
}, []);
return (
<div>Home Page</div>
)
}

7 Replies

Arboreal antOP
do you have any code examples?
Kawakawa
Google intersection observer api. I know @mantine/hooks has one for it.
Then, go from there.
Arboreal antOP
thanks a lot
Kawakawa
👍