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
Mapa a la posición del elemento con css puro

Problema: tengo un elemento que necesito arrastrar con el cursor del mouse. Tengo un elemento contenedor (por ejemplo, 400x800) y necesito mover mi elemento secundario (2x2) al pasar el mouse sobre el contenedor. Lo hice con React, pero tengo algunos problemas de rendimiento. ¿Puede darme algún consejo sobre cómo puedo hacerlo con Pure CSS sin crear elementos div para cada px?

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

0

Algoritmo de arrastrar y soltar

El algoritmo básico de arrastrar y soltar se ve así:

1. Con el mouse hacia abajo: prepare el elemento para moverlo, si es necesario (tal vez cree un clon del mismo, agréguele una clase o lo que sea).
2. Luego, al mover el mouse, muévalo cambiando izquierda/arriba con position:absolute.
3. Al subir el ratón: realice todas las acciones relacionadas con la finalización de arrastrar y soltar.

Aquí está la implementación de arrastrar una bola:

ball.onmousedown = función (evento) {

 let shiftX = event.clientX - ball.getBoundingClientRect().left; let shiftY = event.clientY - ball.getBoundingClientRect().top; ball.style.position = 'absolute'; ball.style.zIndex = 1000; document.body.append(ball); moveAt(event.pageX, event.pageY); // moves the ball at (pageX, pageY) coordinates // taking initial shifts into account function moveAt(pageX, pageY) { ball.style.left = pageX - shiftX + 'px'; ball.style.top = pageY - shiftY + 'px'; } function onMouseMove(event) { moveAt(event.pageX, event.pageY); } // move the ball on mousemove document.addEventListener('mousemove', onMouseMove); // drop the ball, remove unneeded handlers ball.onmouseup = function() { document.removeEventListener('mousemove', onMouseMove); ball.onmouseup = null; }; }; ball.ondragstart = function() { return false; };
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!