Skip to content

Instantly share code, notes, and snippets.

@robbertvancaem
Last active December 9, 2019 22:13
Show Gist options
  • Save robbertvancaem/3e3e9db87985d58982f8447452c98ead to your computer and use it in GitHub Desktop.
Save robbertvancaem/3e3e9db87985d58982f8447452c98ead to your computer and use it in GitHub Desktop.
parallax-spring-1
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