我試圖拖動包含div中的圖像,覆蓋視頻。當我mousedown
,圖像從當前位置跳轉,沒有我移動它。我怎樣才能解決這個問題?。另外,是否可以限制圖像在父級內的移動?在純Javascript中拖動一個覆蓋視頻的圖像
window.onload = addListeners();
function moveImage(e) {
var move = document.getElementById('overlay');
move.style.position = 'absolute';
move.style.top = e.clientY + 'px';
move.style.left = e.clientX + 'px';
}
function addListeners(){
document.getElementById('overlay').addEventListener('mousedown', mouseDown, false);
window.addEventListener('mouseup', mouseUp, false);
}
function mouseUp()
{
window.removeEventListener('mousemove', moveImage, true);
}
function mouseDown(e){
window.addEventListener('mousemove', moveImage, true);
}
#overlay-box {
position: relative;
}
#overlay {
position: absolute;
z-index: 30000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="overlay-box">
<img style="width: 150px; height: 150px" id="overlay" src="https://s-media-cache-ak0.pinimg.com/originals/0a/cf/7e/0acf7efc731f9b6a544e85198e484286.png">
<video id="video" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" autoplay style="position: relative; border:1px solid green" width="400" height="300">
</video>
</div>