简体   繁体   English

HTML5:您如何针对被拖动然后放入div中的不同图像进行不同的事件?

[英]HTML5 : How do you make different events for different images being dragged then dropped into a div?

I'm having a problem where I have 2 Images and I want each when dropped and dragged into a div to play a different video. 我遇到一个问题,我有2张图片,我希望每张图片都拖放到div中以播放不同的视频。 Ie Image 1 plays Video 1 , Image 2 plays video 2. The problem is Images 1 and 2 both play the same video. 即,图像1播放视频1,图像2播放视频2。问题是图像1和2都播放相同的视频。 The Image id's are "drag1" and "drag3". 图片ID为“ drag1”和“ drag3”。 Again I would like when "drag1" is dragged and dropped then appended into the div for "video1" to play and when "drag3" is dragged and dropped into the div then appended for another video I haven't inputted yet to play. 同样,我想将“ drag1”拖放到div中然后添加到div中以便“ video1”播放,将“ drag3”拖放到div中之后再添加到另一个我尚未输入的视频中。 How do I do this? 我该怎么做呢? Right now "drag1" and "drag3" both play "video1". 现在,“ drag1”和“ drag3”都播放“ video1”。 Here's an example of my code. 这是我的代码示例。

<script>

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
    ev.target.removeChild(document.getElementById(data));


    var video = document.getElementById('video1');
     video.style.display = 'block';
     video.play();

    document.getElementById('video1').addEventListener('ended',myHandler,false);
    function myHandler(e) {
        if(!e) { e = window.event; }
        video.style.display = 'none';
    }

}

</script>
</head>

<body>

<video id="video1" controls>
  <source src="carrots.mp4" type="video/mp4">
</video>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>


<img id="drag2" src="Pot.jpg" draggable="false"
ondragstart="drag(event)" >




<img id="drag1" src="carrots.jpg" draggable="true"
ondragstart="drag(event)" >

<img id="drag3" src="pepper.png" draggable="true"
ondragstart="drag(event)" >


</body>
</html>

and this is the CSS3 这是CSS3

<style>
body {
    background: url(food-table.jpg);
     background-repeat: no-repeat;
    background-size: 100% 100%;
} 
html 
{ height:100%}

#div1 {width:20%;height:20%;
position:absolute;
left:50%;
top:50%;
margin-right: -50%;
transform: translate(-50%, -50%);
z-index:-1;

}
#drag2{width:20%; height:20%; position:absolute;

left:50%;
top:50%;
margin-right: -50%;
transform: translate(-50%, -50%);
z-index:-2;

    }
#drag1{ width:10%; height:10%;
position:absolute;
top:50%;
left:20%;
margin-right: -20%;
transform: translate(-20%, -25%);
z-index:-3;
    }

#drag3{ width:10%; height:10%;
position:absolute;
top:70%;
left:30%;
margin-right: -30%;
transform: translate(-30%, -70%);
z-index:-3;
    }   

#video1{ width:50%; height: 30%;
position:absolute;
top:50%;
left:50%;
margin-right: -50%;
transform: translate(-50%, -50%);
display:none;
}

I would add an id to your source like: 我会在您的来源中添加一个ID,例如:

<video id="video1" controls>
  <source id="video1-source" src="carrots.mp4" type="video/mp4">
</video>

and then change the sources 'src' attribute to the appropriate source. 然后将来源的“ src”属性更改为适当的来源。

 var video = document.getElementById('video1');
 document.getElementById('video1-source').setAttribute('src', 'youNewSource');
 video.style.display = 'block';
 video.play();

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

相关问题 在HTML5中,由于将特殊图像拖放到div中,如何使特殊视频播放器弹出? - In HTML5 how do you make spefic video players pop up as a result of dropping and dragging spefic images into a div? 防止HTML元素被拖放 - Prevent HTML elements from being dragged/dropped 如何让div在被拖动时跟随鼠标 - How to make div follow mouse when being dragged 如何区分文件或文件夹在被拖放之前是否被拖动? - How to distinguish if a file or folder is being dragged prior to it being dropped? 在HTML5中加载不同的背景图片 - Loading different Background images in HTML5 如何防止在移动设备上拖动项目 - How do you prevent an item from being dragged on a mobile 如何在 div 上拖动文本或在 div 上放置文本时触发事件 - How to trigger event when dragged text on a div or dropped a text on a div 如何使拖动的 div 落在它被拖动到的 position 上,以及如何在 html 拖放中删除“globe”-img - How to make a dragged div land on the position it was dragged to, and how to remove the the "globe"-img on drag in html drag-and-drop 如何使用不同的图像和描述向 Leaflet map 添加标记? - How do you add markers to a Leaflet map with different images and descriptions? 如何将拖动的项目与放置的项目重叠? - How do I overlap a dragged item over a dropped item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM