Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML5仅使用javascript进行拖放_Javascript_Html_Events_Drag And Drop - Fatal编程技术网

HTML5仅使用javascript进行拖放

HTML5仅使用javascript进行拖放,javascript,html,events,drag-and-drop,Javascript,Html,Events,Drag And Drop,全部, 我尝试使用事件委派、拖放和javascript 在我双击sourceContainer中的项目1之后,该项目将在destinationContainer中创建。没问题 但是,当我尝试在destinationContainer中拖动新项目时,我无法设置所选div的顶部和左侧样式。event.target.id或this.id始终相同。我希望id为1,这是destinationContainer中存在的div的新创建id 有谁能帮助我理解,我如何才能仅更改destinationContain

全部,

我尝试使用事件委派、拖放和javascript

在我双击sourceContainer中的项目1之后,该项目将在destinationContainer中创建。没问题

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

有谁能帮助我理解,我如何才能仅更改destinationContainer中创建的div的左上方样式

这是一把小提琴:

以下是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编写。重点是使用mouseup、mousedown和mousemove事件来模拟拖放。播放“偏移”以从拖动对象的鼠标光标获得所需的偏移

在整个页面中运行代码片段以获得更好的查看效果。滚动是拖放的敌人

var elements=document.getElementsByClassName(“项目”);
var dest=document.getElementById(“dest”);
var-drag=false;
var tempObj=null//要拖动的活动对象
//将事件添加到item类
用于(元件的obj)
{
obj.addEventListener('mousedown',函数draginInitiate()
{
阻力=真;
tempObj=这个;
});
}
document.addEventListener('mouseup',函数draginInitiate(e)
{
阻力=假;
if(tempObj!=null)
{
if(itemInsidedDestination(tempObj,e)=真)
{
tempObj.innerHTML=“成功!!!”;
目的地附属物(临时目的地);
tempObj.style.position=“相对”;
tempObj.style.left=“0px”;
tempObj.style.top=“0px”;
}否则
{
tempObj.style.left=“0px”;
tempObj.style.top=“0px”;
}
}
});
document.addEventListener('mousemove',函数dragStop(e)
{
如果(拖动==true&&tempObj!=null)
{
//使用“偏移”来使用鼠标光标获得项目的准确位置
var偏移=100;
tempObj.style.left=(e.clientX)+“px”;
tempObj.style.top=(e.clientY-offset)+“px”;
if(itemInsidedDestination(tempObj,e)=真)
{
tempObj.innerHTML=“我在目的地内”;
}否则
{
tempObj.innerHTML=“不要丢下我!!!”;
}
}
});
功能项InsidedDestination(obj,e)
{
var mx=e.clientX,my=e.clientY;
var itemRect=obj.getBoundingClientRect();
var desect=dest.getBoundingClientRect();

如果((itemRect.left+itemRect.width)>=destRect.left&&(itemRect.left+itemRect.width)=destRect.top&&(itemRect.top)希望帮助我没有逐字使用您的示例。但是,我确实将源设置为所选li。出于某种原因,drop事件处理程序将所选元素设置为父容器(例如destinationContainer)。这意味着您不能使用This或event.target来确定选择了哪个li元素。因此,我在drag_start函数中将source的值设置为event.target。这样,我就可以在drop函数中正确设置偏移。
/*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;
}
<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>