Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
¿Cómo hacer que la navegación con la tecla de flecha en la cuadrícula de la tarjeta receptiva como Google Drive en React?

He intentado lograr la navegación con teclas de flecha en las 4 direcciones. Puedo lograr 2 direcciones left y right usando nextSibling y previousSibling . Pero no encontré ninguna forma de llegar por encima y por debajo de los divs. He visto muchos ejemplos de navegación con teclas de flecha en el diseño de la cuadrícula de la tarjeta, pero no encontré ninguno con un diseño receptivo. Cuando tiene soporte receptivo, la cantidad de tarjetas en cada fila cambia. En nuestro caso, tenemos un solo div con envoltura para crear la cuadrícula. He visto aplicaciones como Google Drive que tienen soporte receptivo y navegación con teclas de flecha. Muestra

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si está seguro de que todas sus tarjetas tienen el mismo ancho, puede optar por el siguiente enfoque:
Puede encontrar el elemento arriba o abajo usando sus coordenadas x e y, que puede obtener usando los métodos Element.getBoundingClientRect() ,window.scrollX ywindow.scrollY .

avanzando con su muestra ,
Agregue un método getRect() a su componente de diseño para obtener las coordenadas x e y.

 getRect = (id) => { const el = document.getElementById(id); const rect = el.getBoundingClientRect(); const x = rect.left + window.scrollX; const y = rect.top + window.scrollY; return { el, x, y }; };

Ahora agregue condiciones para las teclas ARROW_UP y ARROW_DOWN al método handleKeyPress()

 else if (e.key === NAVIGATION_KEYS.ARROW_UP) { const { x, y } = this.getRect(selected); // loop back in the list and select the first element // that is above the current selected element and has the same x coordinate for (let i = list.indexOf(selected) - 1; i >= 0; i--) { const { el, x: tx, y: ty } = this.getRect(list[i]); if (x === tx && y > ty) { this.setState({ selected: list[i] }); el.scrollIntoView(); // scrolls the element into view, try removing it to see what it does break; } } } else if (e.key === NAVIGATION_KEYS.ARROW_DOWN) { const { x, y } = this.getRect(selected); // loop forward in the list and select the first element // that is below the current selected element and has the same x coordinate for (let i = list.indexOf(selected) + 1; i < list.length; i++) { const { el, x: tx, y: ty } = this.getRect(list[i]); if (x === tx && y < ty) { this.setState({ selected: list[i] }); el.scrollIntoView(); // scrolls the element into view, try removing it to see what it does break; } } }

si desea un efecto de desplazamiento suave, puede agregar el siguiente css

 html { scroll-behaviour: smooth; }

He agregado comentarios para explicación, puedes preguntar si no entiendes algo.

aquí hay un ejemplo de trabajo bifurcado y extendido de su muestra

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!