简体   繁体   English

如何实现拖放操作,拖动子元素将拖动整个父元素

[英]How to implement drag and drop so dragging child element will drag whole parent element

I'm working on a media import plugin for wordpress. 我正在为wordpress制作媒体导入插件。 I'm trying to implement a drag and drop feature to reorder media elements(audio and/or images) that the user has imported. 我正在尝试实现拖放功能,以重新排序用户导入的媒体元素(音频和/或图像)。 I have a <div> tag(we'll call these media divs ) that holds up to three other <span> , <img> , or <audio> tags. 我有一个<div>标签(我们称之为媒体div ),最多可容纳三个<span><img><audio>标签。 So I have all the media assets that have been imported displayed in a line and would like to be able to drag and drop to reorder the media divs . 因此,我将已导入的所有媒体资产显示在一行中,并希望能够拖放以重新排序媒体div I have no problem implementing the basic drag and drop using html5. 使用html5实现基本的拖放操作没有问题。 My problem is that right now when I click on the media child elements( <audio> or <img> ) inside the media divs , the child element is the target of the drag event. 我的问题是,当我点击媒体div中的媒体子元素( <audio><img> )时,子元素是拖动事件的目标。 Is there any way I can reset the target of the drag event to be the parent element so I drag the whole media div and not just the media element? 有没有什么办法可以将拖动事件的目标重置为父元素,所以我拖动整个媒体div而不仅仅是媒体元素? I've looked into e.stopPropogation() and read up on bubbling, and capturing but every way I've tried to utilize those, it doesn't solve my problem. 我已经查看了e.stopPropogation()并阅读了冒泡和捕获,但我试图利用它们的每一种方式,都没有解决我的问题。 Is there something I'm missing? 有什么我想念的吗? I would prefer to avoid jQuery if possible and definitely can't use any libraries or frameworks. 如果可能的话,我宁愿避免使用jQuery,也绝对不能使用任何库或框架。
TL;DR: How can I make the parent element the target of a drag event when a child element is clicked? TL; DR:当单击子元素时,如何将父元素作为拖动事件的目标?

<div class="npr-import-media npr-import-audio npr-import-images" id="media-container">

    <div class="npr-import-media-container" draggable="true">
        <audio src="<?php echo $audio->format->mp4->{'$text'}; ?>" controls></audio>
        <span class="npr-media-delete">X</span>
    </div>

    <div class="npr-import-image-container npr-import-media-container" draggable="true">
        <img class="npr-import-image" src="<?php echo $image->src; ?>" >
        <span class="npr-import-image-caption"><?php echo $image->caption->{'$text'} ?></span>
        <span class="npr-media-delete">X</span>
    </div>

    <div class="npr-import-add-media npr-import-media-container">
        Add Media+
    </div>

</div>

This is the HTML portion of my code. 这是我的代码的HTML部分。 There is originally some more php functionality to loop through the original source material to display all of the media elements being imported in from the original article, but I don't think it's necessary to include that. 最初有一些php功能可以循环显示原始源材料,以显示从原始文章导入的所有媒体元素,但我认为不必包含它。

You can set the handler for ondragstart event to return false, like below: 您可以将ondragstart事件的处理程序设置为false,如下所示:

<div class="npr-import-media npr-import-audio npr-import-images" id="media-container">
    <div class="npr-import-image-container npr-import-media-container" draggable="true" ondragstart="drag(event)" style="border:1px solid blue;padding:10px;">
        <img class="npr-import-image" src="img.jpg" >
        <span class="npr-import-image-caption">Caption</span>
        <span class="npr-media-delete">X</span>
    </div>

    <div class="npr-import-add-media npr-import-media-container">
        Add Media+
    </div>
</div>
<script>
function drag(e) {
    console.log(e);
}
var images = document.getElementsByTagName('img');
for(i = 0 ; i < images.length; i++) 
    images[i].ondragstart = function() { return false; }
</script>

On console output you can see: 在控制台输出上,您可以看到:

>> DragEvent {isTrusted: true, dataTransfer: DataTransfer, screenX: 66, screenY: 161, clientX: 66…}

UPDATE By setting drag event handlers on the parent elements either by html attributes as above or attaching event listeners by code as below: 更新通过如上所述的html属性设置父元素上的拖动事件处理程序或按代码附加事件侦听器,如下所示:

document.getElementById('draggable').addEventListener('dragstart', function(e) {
   console.log(e);
});

I was just in the same problem and couldn't work around img tag, so switched to div. 我只是在同样的问题,无法解决img标签,所以切换到div。

You can use 您可以使用

<div style="background-image: url('img.jpg')"></div>

instead of 代替

<img class="npr-import-image" src="img.jpg" >

I just came across this issue as well. 我刚刚遇到过这个问题。 I resolved it without javascript by simply adding draggable="false" to each child element. 只需在每个子元素中添加draggable =“false”,我就可以在没有javascript的情况下解决它。

<div class="npr-import-image-container npr-import-media-container" draggable="true" ondragstart="drag(event)" style="border:1px solid blue;padding:10px;">
    <img draggable="false" class="npr-import-image" src="img.jpg" >
    <span draggable="false" class="npr-import-image-caption">Caption</span>
    <span draggable="false" class="npr-media-delete">X</span>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM