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

209
Vistas
How to move text horizontally as you scroll when getting to a specific part of the page with React?

I have two lines of text in the middle of my page that I want them to scroll horizontally from outside of the page to come in then leave through the other side. The top one comes from the left and exist through the right and the second line does the opposite. My problem is that scrollY does not stay constant when you change the screen size.

Is there a good solution to only scroll the text when you get to that particular section of the page? Right now the section comes in at 2200px when in full view and I'm using that number to trigger the scroll

I have this hook that listens for the scroll:

export default function useScrollListener() {
    const [data, setData] = useState({
        x: 0,
        y: 0,
        lastX: 0,
        lastY: 0
     });

    // set up event listeners
    useEffect(() => {
        const handleScroll = () => {
           setData((last) => {
               return {
                   x: window.scrollX,
                   y: window.scrollY,
                   lastX: last.x,
                   lastY: last.y
                };
            });
        };

    handleScroll();
    window.addEventListener("scroll", handleScroll);

    return () => {
        window.removeEventListener("scroll", handleScroll);
        };
    }, []);

    return data;
}

In the page that has the two lines I have :

const scroll = useScrollListener();
const [position, setPosition] = useState('')

useEffect(() => {
    let pos = scroll.y
    let scrollPos = pos - 3000
     
    // Section with the lines of text starts around 2200 on scrollY
    if(scroll.y > 2200){
        setPosition(scrollPos.toString())
    }
}, [scroll.y]);

The text is wrapped around a div that is relative and the text has a position absolute pushing the element to the right or left by 800px.

<div className="line1">
    <p className="text1" style={{"left": `${position}px`}}>
         Lorem ipsum dolor sit amet.
    </p>
</div>
 
<div className="line2">
    <p className="text1" style={{"right": `${position}px`}}>
         Lorem ipsum dolor sit amet.
    </p>
</div>
about 4 years ago · Juan Pablo Isaza
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