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

236
Visualizações
How to prevent function on children's elements?

I have a code for draggable element

var offsetX;
var offsetY;
Element.prototype.makeDraggable=function(){
  var o=this
  o.onmousedown=function(e){
     offsetX=e.pageX-parseInt(o.style.left)
     offsetY=e.pageY-parseInt(o.style.top)

    document.onmousemove=function(e) {
      o.style.left=Math.min(Math.max(e.pageX-offsetX,o.parentNode.clientWidth-o.clientWidth),0)+'px';
      o.style.top=Math.min(Math.max(e.pageY-offsetY,o.parentNode.clientHeight-o.clientHeight),0)+'px';
    }
    document.onmouseup = function(e) {
      document.onmousemove=o.onmouseup=null
    }
  }
  o.ondragstart = function(){return 0}
}
document.getElementById('object1').makeDraggable();

let spot = document.querySelectorAll('#grid-holder > div');
for(var i = 0; i < spot.length; i ++)
{
  spot[i].onclick = function(){ inventar(); };
}
function inventar()
{
  alert();
}
<div id="parent">
  <div id="object1" style="left: 1px; top: 1px;">

     <div class="grid-holder" id="grid-holder">
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
         <div></div><div></div><div></div>
     </div>
  
  </div>
</div>

When i click #grid-holder > div i have alert message, but how to prevent this action when i drag #object1 element by holding onto children's element?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

var offsetX;
var offsetY;
var moved = false;

Element.prototype.makeDraggable = function(ev){
  var o = this;

  o.onmousedown = function(e){
  offsetX = e.pageX-parseInt(o.style.left);
  offsetY = e.pageY-parseInt(o.style.top);
  moved = false;  

    document.onmousemove = function(e) {
      o.style.left = Math.min(Math.max(e.pageX-offsetX,o.parentNode.clientWidth-o.clientWidth),0)+'px';
      o.style.top = Math.min(Math.max(e.pageY-offsetY,o.parentNode.clientHeight-o.clientHeight),0)+'px';
      moved = true;    
    }
    document.onmouseup = function(e) {
      document.onmousemove = o.onmouseup = null;
    }
  }
  o.ondragstart = function(){
   return 0; 
  }
}
document.getElementById('object1').makeDraggable();

let spot = document.querySelectorAll('#grid-holder > div');
for(var i = 0; i < spot.length; i ++)
{
  spot[i].onclick = function(){ inventar(); };
}
function inventar()
{
  if(!moved) alert();
}
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