【问题标题】:HTML5 drag and drop with only javascript仅使用 javascript 进行 HTML5 拖放
【发布时间】:2017-01-17 06:31:54
【问题描述】:

全部,

我正在尝试使用事件委托、拖放和仅 javascript。

在我双击 sourceContainer 中的第 1 项后......然后在destinationContainer 中创建该项目。这里没问题。

但是,当我尝试在destinationContainer 中拖动新项目时,我无法为选定的div 设置顶部和左侧样式。 event.target.id 或 this.id 总是相同的。我希望 id 为 1,这是destinationContainer 中存在的 div 新创建的 id。

谁能帮我理解我如何才能仅更改在destinationContainer 中创建的div 的顶部和左侧样式。

这是一个小提琴:https://jsfiddle.net/mdevera/oys3tLww/1/

这里是drop函数:

function drop(event) {

console.log("event.target.id: " + event.target.id);
console.log("this.id: " + this.id);

// This does not allow me to set the selected div's appropriate top and left styles.
// I do not want the whole destinationContainer to not move.  Just the selected child element.

// var offset = event.dataTransfer.getData("text/plain").split(',');
//
// this.style.top = (event.clientY + parseInt(offset[0], 10)) + 'px';
// this.style.left = (event.clientX + parseInt(offset[1], 10)) + 'px';

}

【问题讨论】:

    标签: javascript html events drag-and-drop


    【解决方案1】:
    /*Drag and Drop events*/
    var source;
    var before;
    function isbefore(a, b) {
        if (a.parentNode == b.parentNode) {
            for (var cur = a; cur; cur = cur.previousSibling) {
                if (cur === b) {
                    return true;
                }
            }
        }
        return false;
    }
    
    function dragenter(e) {
        before =isbefore(source, e.target);
        if (before) {
            e.target.parentNode.insertBefore(source, e.target);
        } else {
            e.target.parentNode.insertBefore(source, e.target.nextSibling);
        }
    }
    function dragstart(e) {
        source = e.target;
        e.dataTransfer.effectAllowed = 'move';
        e.dataTransfer.setData("Text", source.getAttribute('id'));
        return true;
    }
    function dragDrop(e) {
        var source = e.dataTransfer.getData("Text");
        if (!before) {
            ev.target.appendChild(document.getElementById(src));
        }
        ev.stopPropagation();
    return false;
    }
    

    【讨论】:

    • 希望有帮助
    • 我没有逐字使用你的例子。但是,我确实将源设置为选定的 li。出于某种原因,drop 事件处理程序将选定元素设置为父容器(例如destinationContainer)。这意味着您不能使用 this 或 event.target 来确定选择了哪个 li 元素。因此,我在 drag_start 函数中将 source 的值设置为 event.target 。这让我可以在 drop 函数中正确设置偏移量。
    【解决方案2】:
    <body>
    
    <div class="dropdown">
    
        <button class="btn btn-primary dropdown-toggle"
                type="button" id="dropdownMenu1"
                data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            Border-style
            <span class="caret"></span>
        </button>
    
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
            <li><a href="#" id="border-solid" onClick="return_id(this.id)">Solid</a></li>
            <li><a href="#" id="border-dashed" onClick="return_id(this.id)">Dashed</a></li>
            <li><a href="#" id="border-dotted" onClick="return_id(this.id)">dotted</a></li>
        </ul>
    </div>
    
    
    <center><input type="text" id="text1" onClick="ele_id(event)"/></center>
    <center><input type="text" id="text2" onClick="ele_id(event)"/></center>
    
    <center><input type="text" id="text5" onClick="ele_id(event)"/></center>
    

    【讨论】:

      【解决方案3】:

      纯 Javascript。要点是玩mouseup,mousedown和mousemove事件来模拟拖放。使用偏移量从拖动对象的鼠标光标处获得所需的偏移量。

      在整页中运行代码 sn-p 以获得更好的视图。滚动是拖放的敌人

      var elements=document.getElementsByClassName("item");
      var dest=document.getElementById("dest");
      
      
      var drag=false;
      var tempObj=null; //active  object to drag
      
      //add events to item class 
      for(obj of elements)
      {
      	obj.addEventListener('mousedown',function dragInitiate()
        {
        	drag=true;
          tempObj=this;
          
        });
        
        
      }
      document.addEventListener('mouseup',function dragInitiate(e)
        {
        	drag=false;
          if(tempObj!=null)
          {
          	if(itemInsideDestination(tempObj,e)==true)
            {
              tempObj.innerHTML="Success!!!";
              dest.appendChild(tempObj);
              tempObj.style.position="relative";
              tempObj.style.left="0px";
              tempObj.style.top="0px";
              
              
              
            }else
              {
                
                tempObj.style.left="0px";
                tempObj.style.top="0px";
              }
            
          }
        });
      document.addEventListener('mousemove',function dragStop(e)
        {
        	if(drag==true && tempObj!=null)
          {
            //play with offset to get accurate position of item with mouse cursor
            var offset=100;
          	tempObj.style.left=(e.clientX)+"px";
            tempObj.style.top=(e.clientY-offset)+"px";
            if(itemInsideDestination(tempObj,e)==true)
            {
              tempObj.innerHTML="I am inside Destination";
            }else
              {
                tempObj.innerHTML="Do not Drop me!!!";
              }
          }
        	
        });
      
      function itemInsideDestination(obj,e)
      {
      	var mx=e.clientX,my=e.clientY;
        var itemRect=obj.getBoundingClientRect();  
        var destRect=dest.getBoundingClientRect();
        
        if((itemRect.left+itemRect.width)>=destRect.left && (itemRect.left+itemRect.width)<=destRect.left+destRect.width)
        {
        if((itemRect.top)>=destRect.top && (itemRect.top)<=destRect.top+destRect.height)
        {
        	//console.log('box inside');
          return true;
        }
        }
        
        
      }
      #source
      {
       
       border:1px solid gray;
       height:200px;
       width:200px;
      }
      #dest
      {
       
       border:1px solid gray;
       height:200px;
       width:400px;
      }
      .item
      {
        background-color:lightblue;
        width:200px;
        height:30px;
        position:relative;
        border:1px solid gray;
        cursor:move;
      }
      .inDest
      {
        background-color:lightblue;
        width:200px;
        height:30px;
        position:relative;
        border:1px solid gray;
        left:0px;
        top:0px;
      }
      .item:active
      {
        border:1px solid blue;
      }
      <p>Source</p>
      <div id="source">
        <div class="item">Item1</div>
        <div class="item">Item2</div>
        <div class="item">Item3</div>
        <div class="item">Item4</div>
        
      </div>
      <p>Destination</p>
      <div id="dest">
        
      </div>

      【讨论】:

        猜你喜欢
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        相关资源
        最近更新 更多