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

156
Views
Javascript verifica si el cursor está tocando un elemento en la página que sigue una lista específica de etiquetas de elementos. (Sin JQuery. Solo Vanilla JS)

Así que estoy implementando un pequeño círculo alrededor de su cursor que se expande cuando su mouse toca un button , una a y una image . Tengo mucho código para hacer que esto funcione, y no voy a hacer que lo revises todo. La esencia es que tengo una función: checkIfTouching. Aquí está el código (desordenado pero idc):

 function findTopLeft(element) { var rec = element.getBoundingClientRect(); return {top: rec.top + window.scrollY, left: rec.left + window.scrollX}; } //call it like findTopLeft('#header'); function getElementBBox(element) { var rec = element.getBoundingClientRect(); return {width: rec.width, height: rec.height}; } function checkIfTouching(element, x, y) { // check if the cursor is in the elementm (from top left to bottom right) const elementBBox = getElementBBox(element); const elementTopLeft = findTopLeft(element); const elementBottomRight = {top: elementTopLeft.top + elementBBox.height, left: elementTopLeft.left + elementBBox.width}; // check if it is near the cursor const cursorDistances = { top: Math.abs(elementTopLeft.top - y), left: Math.abs(elementTopLeft.left - x), bottom: Math.abs(elementBottomRight.top - y), right: Math.abs(elementBottomRight.left - x) }; // if the cursor is within 100px of the element, return true if (cursorDistances.top < 100 && cursorDistances.left < 100 && cursorDistances.bottom < 100 && cursorDistances.right < 100) { if (x >= elementTopLeft.left && x <= elementBottomRight.left && y >= elementTopLeft.top && y <= elementBottomRight.top) { return true; } else { return false; } } else { return false; } }

Funciona perfectamente. El problema que tengo es que obtengo todos los elementos en la página, los archivo y luego recorro cada uno de ellos usando un bucle for. Esto evita que el círculo se expanda por completo porque estoy sobre un objeto y tiene que cambiar entre ambos. Mi pregunta es: ¿hay alguna manera de ejecutar una función en todos los elementos de una lista o hacer que el ciclo for se ejecute más rápido? Puede ver mi archivo cursor.js completo aquí: https://gist.github.com/BlueFalconHD/eb8a246035e317bfc5d3b0a210b71b6c . Realmente no entiendo esto lo suficiente, y honestamente no tengo idea de la mitad de lo que hace este código. Fue escrito por el copiloto. De todos modos, gracias por la ayuda! :)

about 4 years ago · Juan Pablo Isaza
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!