You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
adding a an event listener scroll to window is pointless because the Parallax element scrolls and not the window itself. Therefore console logging window's scroll position doesn't work and so I was wondering how I am to get the scroll position within parallax so that I could add a few spring animations on scroll within parallax.
Ended up implementing a useOnScreen hook (per the link) that returns a boolean when content is visible. Works a charm for triggering animations (although still doesn't give you scroll position or offset).
Ended up implementing a useOnScreen hook (per the link) that returns a boolean when content is visible. Works a charm for triggering animations (although still doesn't give you scroll position or offset).
const useOnScreen = (ref = (<div />), rootMargin = '40px', threshold = 1) => {
// State and setter for storing whether element is visible
const [isIntersecting, setIntersecting] = useState(false);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
// Update our state when observer callback fires
setTimeout(() => setIntersecting(entry.isIntersecting), 0);
// setIntersecting(entry.isIntersecting);
},
{
rootMargin,
threshold,
},
);
if (ref.current) {
observer.observe(ref.current);
}
return () => {
if (ref.current) {
observer.unobserve(ref.current);
}
};
}, []); // Empty array ensures that effect is only run on mount and unmount
return isIntersecting;
};
export default useOnScreen;
That's how I implemented the hook (the timeout is just playing with delay). You might want to keep the div separate from your actual functional HTML and make it smaller than the entire viewHeight. <div ref={reff} /> . Try popping that somewhere convenient.
@michaelwachell's code is great for determining onScreen, but I'd still love to get the actual scroll position. (I had some UI logic built around window.pageYOffset, and I'm trying to drop in react-spring parallax without breaking.) Any help appreciated.
Reacted by straydrop411 - 54yd and Moyeong Lee (Mo)
I want to implement a feature that if I scroll down the page -> the nav bar hides and scroll up -> the nav bar shows up again. And I need to know window.pageYOffset value to achieve this feature. Please help!
Activity
michaelwachell commentedon Jul 22, 2020
+1
aleclarson commentedon Jul 22, 2020
Please add example code of the API you're expecting, if you can. Thanks 👍
g0byungsoo commentedon Jul 27, 2020
+1
HERMESMATEUS commentedon Jul 29, 2020
+1
I need to get the position of the pages.
Please if someone get the code how to do it please share the code thanks, because i have the same problem
michaelwachell commentedon Jul 29, 2020
Ended up implementing a
useOnScreenhook (per the link) that returns a boolean when content is visible. Works a charm for triggering animations (although still doesn't give you scroll position or offset).https://usehooks.com/useOnScreen/
HERMESMATEUS commentedon Aug 4, 2020
sorry can you help me with a little example, currently my code is similar to this but it doesn't work
michaelwachell commentedon Sep 9, 2020
That's how I implemented the hook (the timeout is just playing with delay). You might want to keep the
divseparate from your actual functional HTML and make it smaller than the entire viewHeight.<div ref={reff} />. Try popping that somewhere convenient.FormidablePencil commentedon Sep 18, 2020
Thx @michaelwachell.
starcrest commentedon Oct 29, 2020
@michaelwachell's code is great for determining onScreen, but I'd still love to get the actual scroll position. (I had some UI logic built around window.pageYOffset, and I'm trying to drop in react-spring parallax without breaking.) Any help appreciated.
2000prcs commentedon Feb 13, 2021
I want to implement a feature that if I scroll down the page -> the nav bar hides and scroll up -> the nav bar shows up again. And I need to know
window.pageYOffsetvalue to achieve this feature. Please help!