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
I want when I make double click the element drop

I need help because I can't make double clicking drop the element.

var lista = document.getElementById("uno");

lista.onclick = function(event) {
  lista.style.position = 'absolute';
  lista.style.zIndex = 1000;

  document.body.append(lista);

  function moveAt(pageX, pageY) {
    lista.style.left = pageX - lista.offsetWidth / 2 + 'px';
    lista.style.top = pageY - lista.offsetHeight / 2 + 'px';
  }

  moveAt(event.pageX, event.pageY);

  function onMouseMove(event) {
    moveAt(event.pageX, event.pageY);
  }

  document.addEventListener('mousemove', onMouseMove);

  lista.ondblclick = function() {
    document.removeEventListener("mousemove", onMouseMove);
  }
};
.row1 {
  position: relative;
}

#uno {
  border: 1px solid black;
  width: 6vw;
  height: 10vh;
  background-color: blue;
}
<body>
  <div class="row1">
    <div id="uno"></div>
  </div>


  <script src="scriptV\ejercicio3.js"></script>
</body>

What is my mistake?

If I put

lista.onclick = function () {
document.removeEventListener("mousemove", onMouseMove);

It works but I want double click and I don't know how I can do it.

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

0

The solution below works for me. I moved the functions moveAt and onMouseMove and lista.ondblclick outside the scope of of the lista.onclick method, and also added a boolean, locked so that the event listener is only added and removed when it is not locked / locked respectively:

var lista = document.getElementById("uno");
let locked = false;

function moveAt(pageX, pageY) {
    lista.style.left = pageX - lista.offsetWidth / 2 + 'px';
    lista.style.top = pageY - lista.offsetHeight / 2 + 'px';
}
    
function onMouseMove(event) {
    moveAt(event.pageX, event.pageY);
}


lista.onclick = function (event) {
    console.log("sng");
    if(!locked){
        console.log("not locked");
        locked = true;
    
    lista.style.position = 'absolute';
    lista.style.zIndex = 1000;

    document.body.append(lista);
    }
    

    moveAt(event.pageX, event.pageY);

    

    document.addEventListener('mousemove', onMouseMove);

    
};  

lista.ondblclick = function () {
    console.log("dbl")
    if(locked){
        console.log("locked")
        locked = false;
        document.removeEventListener("mousemove", onMouseMove);
    }      
}
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!