Last active
December 9, 2019 22:13
-
-
Save robbertvancaem/3e3e9db87985d58982f8447452c98ead to your computer and use it in GitHub Desktop.
parallax-spring-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useRef } from 'react'; | |
const Comp = () => { | |
const ref = useRef(); | |
const handleScroll = () => { | |
const posY = ref.current.getBoundingClientRect().top; | |
const offset = window.pageYOffset - posY; | |
console.log(offset); | |
}; | |
useEffect(() => { | |
window.addEventListener('scroll', handleScroll); | |
return () => { | |
window.removeEventListener('scroll', handleScroll); | |
}; | |
}); | |
return (<div ref={ref}>Contents of your component</div>) | |
} | |
export default Comp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment