Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
formatting useref in style for paralax effect

I'm using fixed positioning to try and create a simple parallax effect

export const Home = () => {
    const paralaxref = React.useRef();


    const backdropImgStyle = {
        height: '200vh',
        position: 'fixed',
        top: paralaxref.current?.clientHeight? window.pageYOffset / paralaxref.current.clientHeight*-500 : 0,
        left: 0,
        zIndex: -100,
        filter: 'grayscale(.7)',
    }

however, it doesn't update as I scroll how would I do that?

---EDIT--- Final solution for anyone in the future:

export const Home = () => {
    const paralaxref = React.useRef();

    const [paralaxPosition, setParalaxPosition] = React.useState(0);

    useEffect(() => {
        window.onscroll = () => {
            setParalaxPosition(paralaxref.current?.clientHeight? window.pageYOffset / paralaxref.current.clientHeight*-window.innerHeight : 0)
            //(returns 0-1 for percent scrolled)*(multiplier for image height)
        }
        }, [])


    const backdropImgStyle = {
        height: '200vh',
        width: '100vw',
        position: 'fixed',
        top: paralaxPosition,
        left: 0,
        right: 0,
        zIndex: -100,
    }
return(
<div ref={paralaxref} style={foregroundStyle}>
            <img style={backdropImgStyle}  
               src={"https://i.imgur.com/image.png"}/>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

use window.onScroll and then update your component styles inside it. This will work but use it inside useEffect and you can remove the listener when component will unmount.

useEffect(() => {
window.onscroll = () => {
// add logic
}
}, []);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda