NextJs 13.4.9 change the page when user scrolls down and up
Unanswered
Arboreal ant posted this in #help-forum
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>
)
}
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 ant 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>
)
}
Kawakawa
No clue why you'd want to do this, as it looks like you are trying to treat pages as sections on a page.
If you really want to do it, however - I'd reccomend using the Intersection Observer API to check if the viewport is observing part of the page, and then I'd run a callback to e.g. redirect the person.
If you really want to do it, however - I'd reccomend using the Intersection Observer API to check if the viewport is observing part of the page, and then I'd run a callback to e.g. redirect the person.
Still a bad UX, though.
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
ðŸ‘