Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda