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

223
Views
ondragstart - How to remove animation?

Whenever the user starts dragging an element with draggable="true", the element has a translucent copy of the element you are dragging. Here is the example from W3Schools: stop starting animation

Right side of the photo is important. That is the animation you get when you start dragging an element.

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_ondrag

I have tried to use event.preventDefault(). However, the problem here is that this prevents onDrag from going at all while the element is moving and I need the data (mouse position and such) from onDrag.

It seems there are posts out there for how to stop the animation when you drop it but not start.

Just going off the example on the W3Schools site, I want the drag information without the ondragstart animation.

So if I modify the code by adding a preventDefault() on the ondragstart function:

function dragStart(event) {
  console.log(event);
  event.preventDefault(); //stops animation in right side of photo, but then won't let ondrag fire
  event.dataTransfer.setData("Text", event.target.id);
}

function dragging(event) {
  console.log(event);
  document.getElementById("demo").innerHTML = "The p element is being dragged";
}

The animation you get when dragging goes away. However (as shown by my console.log) lines, the ondrag won't fire with this preventDefault on ondragstart. This is the information I need.

preventDefault inside ondrag doesn't stop that animation. Is this even possible?

SOLUTION If anyone is wondering, I found what I needed to do in this case. You can set the image in javascript to a transparent one: How to remove drag(Ghost) image? so drag is still technically running but that ghostly image is gone. Wasn't thinking of right search terms.

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

0

In this case i would go for a different approach: https://codepen.io/deibl31/pen/oNeXmPE?editors=1111

// HTML
<div id="myDiv">
</div>

// CSS
#myDiv {
  position: absolute;
  height: 100px;
  width: 100px;
  background: black;
  color: white;
}

// JS
let myDiv = document.getElementById('myDiv');
let mouseDown = false;
let divPos = {x: 0, y: 0};

myDiv.addEventListener('mousedown', (event) => {
  mouseDown = true;
});

window.addEventListener('mousemove', (event) => {
  if (mouseDown) {
    divPos.x = event.clientX;
    divPos.y = event.clientY;    
  }
});
 
window.addEventListener('mouseup', (event) => {
  if (mouseDown) {
    myDiv.style.top = divPos.y + 'px';
    myDiv.style.left = divPos.x + 'px';
  }
  mouseDown = 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!