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

179
Vistas
Detect when user has scrolled through a text to enable a button

How to implement a "scroll detection" feature using React? I would like to present the user with a div with a given message, and enable a checkbox only once the user has scrolled through it. This is my current implementation, which is not working.

function Disclaimer({ text, checkboxText }) {
  const divRef = useRef();
  const [userHasReviewed, setUserHasReviewed] = useState(false);

  const scrollTracker = () => {
    if (divRef.current) {
      const { scrollTop, scrollHeight, clientHeight } =
        divRef.current;
      if (scrollTop + clientHeight === scrollHeight) {
        setUserHasReviewed(true);
      }
    }
  };

  return (
    <div>
      <div
        ref={divRef}
        onScroll={() => scrollTracker()}
      >
        {text}
      </div>
      <div>
        <Checkbox
          label={checkboxText}
          disabled={!userHasReviewed}
        />
      </div>
    </div>
  );
}

I've placed debug points inside of the scrollTracker function, but the debugger never enters the function. I think this is because the text is not large enough to actually overflow and require the scrolling space/action. In this case, how can I automatically detect this so that the userHasReviewed is set to true automatically?

almost 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I would use the css hover selector to enable the button

almost 4 years ago · Santiago Trujillo Denunciar

0

To detect when a user has scrolled through text and enable a button accordingly on a web page, you can use JavaScript/jQuery. Here is an example of how to achieve this:

 <!DOCTYPE html>
<html>
<head>
 <title>Detención de Desplazamiento de Texto</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
 <div id="content">
 <p>Este es un texto largo. Desplázate hacia abajo para habilitar el botón.</p>
 </div>
 <button id="myButton" disabled>Botón Deshabilitado</button>
 <script src="script.js"></script>
</body>
</html>
 $(document).ready(function() {
 const $button = $("#myButton");
 const $content = $("#content");

 let usuarioSeDesplazo = false;

 // Detecta el evento de desplazamiento en el contenido
 $content.on("scroll", function() {
 usuarioSeDesplazo = true;
 // Habilita el botón si el usuario se ha desplazado
 $button.prop("disabled", false);
 });
});


This code uses jQuery to detect the scroll event on the #content element. When the user scrolls through the content, the button with the id myButton is enabled. Make sure you include jQuery in your page before running this code.


almost 4 years ago · Santiago Trujillo 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