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

176
Vistas
Change element's position next to the cursor

Ok so I have this:

x = `${e.clientX+window.scrollX}px`
y = `${e.clientY+window.scrollY}px`

And it works, but the problem is that the element can extend outside of the page and it creates a scrollbar.
So how can I make it position the div in some way that it wouldn't do that? Is it possible?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I tried to solve this through JS. Here is my code:

var pointerX = -1;
var pointerY = -1;
document.onmousemove = function(event) {
    pointerX = event.pageX;
    pointerY = event.pageY;
}
setInterval(pointerCheck, 1000);
function pointerCheck() {
    console.log('Cursor at: '+pointerX+', '+pointerY);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can take the viewport width / height and the element width / height into account to calculate the x/y coordinates of your element. See the pseudo-code attached to get the idea. Might need a tweak here and there :)

const { clientHeight, clientWidth } = document.documentElement
const { offsetHeight, offsetWidth } = element
const { clientY, clientX } = event
const { scrollY, scrollX } = window

let x = clientX + scrollX

// check if the box would leave the viewport on the right side
if (x + offsetWidth > clientWidth) {
    // move the element to the left
    x -= clientWidth - (x + offsetWidth)
}

let y = clientY + scrollY

// check if the box would leave the viewport on the bottom
if (y + offsetHeight > clientHeight) {
    // move the element to the top
    y -= clientHeight - (y + offsetHeight)
}
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