Skip to content Skip to sidebar Skip to footer

Drag & Drop Current Element Js

I have 2 divs and 2 paragraphs. Could someone say how could I put paragraph in current div? Example: 1paragraph --> 1div, but 1paragraph can't go to 2div. 2paragraph --> 2div

Solution 1:

Have you tried this?

Just set unique id of div and p tag.

var dragClass, dropClass;
functionallowDrop(ev) {
  ev.preventDefault();
}

functiondrag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

functiondrop(ev) {
  var data = ev.dataTransfer.getData("Text");
  ev.target.appendChild(document.getElementById(data));
  ev.preventDefault();
}
#div1 ,#div2{
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
   
<html><body><divid="div1"ondrop="drop(event)"ondragover="allowDrop(event)"class="1element"></div><divid="div2"ondrop="drop(event)"ondragover="allowDrop(event)"class="2element"></div><br><pid="drag1"draggable="true"ondragstart="drag(event)"class="1element">This is a draggable paragraph. Drag this element into the rectangle.</p><pid="drag2"draggable="true"ondragstart="drag(event)"class="2element">This is a draggable paragraph. Drag this element into the rectangle.</p></body></html>

Solution 2:

try this. It works with this one line

mover.assign('your-div-id') ;

It's okay to have multiple divs with style="display:inline-block"

const mover = {
    target      : null ,
    zMax        : 0 ,
    startX      : 0 ,
    startY      : 0 ,
    pX          : 0 ,
    pY          : 0 ,
    differenceX : 0 ,
    differenceY : 0 ,
    step        : 1 ,
    assign: function(elementId) {
        mover.target = document.getElementById(elementId) ;
        mover.target.addEventListener('mousedown',mover.moveStart,{once:false}) ;
        mover.target.addEventListener('mouseleave',mover.moved,{once:false}) ;
    } ,
    setCurrentPosition: function(reference) {
        let mom      = reference.parentElement ;
        let brothers = Array.from(mom.childNodes) ;
        if(brothers) {
            do{
                brother = brothers.pop() ;
                if(!brother) break ;
                if(brother.style.position) continue ;
                let styles = window.getComputedStyle(brother);
                let marginTop  = parseInt(styles['margin-left'],10) ;
                let marginLeft = parseInt(styles['margin-top'],10) ; ;
                brother.style.left = (brother.offsetLeft-marginLeft) + 'px' ;
                brother.style.top  = (brother.offsetTop-marginTop) + 'px' ;
                brother.style.position = 'absolute' ;
            }while(true) ;
        }
    } ,
    moveStart: function(event) {
        mover.setCurrentPosition(this) ;
        mover.zMax += mover.step ;
        mover.target.style.zIndex = mover.zMax ;
        mover.startX = this.offsetLeft  ;
        mover.startY = this.offsetTop ;
        mover.pX = event.pageX ;
        mover.pY = event.pageY ;
        mover.target.addEventListener('mousemove',mover.moving,{once:true}) ;
        mover.target.addEventListener('mouseup',mover.moved,{once:true}) ;
    } ,
    moving: function(event) {
        mover.differenceX = event.pageX - mover.pX  ;
        mover.differenceY = event.pageY - mover.pY ;
        mover.target.style.position = 'absolute' ;
        mover.target.style.left = mover.startX + mover.differenceX + 'px' ;
        mover.target.style.top  = mover.startY + mover.differenceY + 'px' ;
        mover.target.addEventListener('mousemove',mover.moving,{once:true}) ;
    } ,
    moved: function() {
        mover.target.removeEventListener('mousemove',mover.moving,{once:true}) ;
    } ,
} 

Post a Comment for "Drag & Drop Current Element Js"