Skip to content

Instantly share code, notes, and snippets.

@robbertvancaem
Last active December 9, 2019 22:13
Show Gist options
  • Save robbertvancaem/678e9a6b501afb7e91c3e4d37c151796 to your computer and use it in GitHub Desktop.
Save robbertvancaem/678e9a6b501afb7e91c3e4d37c151796 to your computer and use it in GitHub Desktop.
parallax-spring-2
import React, { useEffect, useRef } from 'react';
import { useSpring } from 'react-spring';
const Comp = () => {
const ref = useRef();
const [{ offset }, set] = useSpring(() => ({ offset: 0 }));
const handleScroll = () => {
const posY = ref.current.getBoundingClientRect().top;
const offset = window.pageYOffset - posY;
set({ 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