不能在拖放后再拖放元素
Can't drop element after the drag
我是JS的新手,试图用纯JS制作简单的界面,用户可以动态添加元素并移动它们。我找到了创建元素和拖放元素的解决方案,它们可以完美地分开工作。但是当我试图将它们结合起来时,drop函数停止工作了…
我做错了什么?
模板:
<head>
<title>Int Demo</title>
<script src='js/script.js'></script>
<style type="text/css">
.wrapper {width: 300px; height: 200px; background: #A3A1A1;}
.textblock {cursor: pointer; background: red;}
</style>
</head>
<body>
<div class="button" id="button">Add Textbox</div>
<div class="wrapper" id="wrapper"></div>
</body>
</html>
My JS file:
document.addEventListener("DOMContentLoaded", function() { init(); }, false);
function init() {
button = document.getElementById('button');
wrapper = document.getElementById('wrapper');
button.addEventListener('click', btnClick, false);
wrapper.ondblclick = catchIt;
}
//add element
function btnClick() {
wrapper.innerHTML += '<p class="draggable textblock">Text Here!</p>';
sessionStorage.inputBoxes = wrapper;
console.log(sessionStorage);
}
// Activate Drag'n'Drop
document.onmousedown = function(e) {
var dragElement = e.target;
if (!dragElement.classList.contains('draggable')) return;
var coords, shiftX, shiftY;
startDrag(e.clientX, e.clientY);
document.onmousemove = function(e) {
moveAt(e.clientX, e.clientY);
};
dragElement.onmouseup = function() {
finishDrag();
};
function startDrag(clientX, clientY) {
shiftX = clientX - dragElement.getBoundingClientRect().left;
shiftY = clientY - dragElement.getBoundingClientRect().top;
dragElement.style.position = 'fixed';
document.body.appendChild(dragElement);
moveAt(clientX, clientY);
};
function finishDrag() {
dragElement.style.top = parseInt(dragElement.style.top) + pageYOffset + 'px';
dragElement.style.position = 'absolute';
document.onmousemove = null;
dragElement.onmouseup = null;
}
function moveAt(clientX, clientY) {
var newX = clientX - shiftX;
var newY = clientY - shiftY;
// bottom offset
var newBottom = newY + dragElement.offsetHeight;
if (newBottom > document.documentElement.clientHeight) {
var docBottom = document.documentElement.getBoundingClientRect().bottom;
var scrollY = Math.min(docBottom - newBottom, 10);
if (scrollY < 0) scrollY = 0;
window.scrollBy(0, scrollY);
newY = Math.min(newY, document.documentElement.clientHeight - dragElement.offsetHeight);
}
// top offset
if (newY < 0) {
var scrollY = Math.min(-newY, 10);
if (scrollY < 0) scrollY = 0;
window.scrollBy(0, -scrollY);
newY = Math.max(newY, 0);
}
if (newX < 0) newX = 0;
if (newX > document.documentElement.clientWidth - dragElement.offsetHeight) {
newX = document.documentElement.clientWidth - dragElement.offsetHeight;
}
dragElement.style.left = newX + 'px';
dragElement.style.top = newY + 'px';
}
return false;
}
我改变了
dragElement.onmouseup = function() {
finishDrag();
};
到
document.onmouseup = function() {
finishDrag();
};
,它开始为我工作。这确实意味着最终用户必须按住鼠标按钮才能继续拖动,并且元素将立即被放置在鼠标按钮上。但我假设这是你想要的功能,或者你想要在第二次点击鼠标时才放东西?
相关文章:
- 区分不同的可拖放元素(jQuery 拖放)
- JavaScript/jQuery在两个iframe之间拖放元素
- 由javascript创建的HTML5拖放元素:
- 拖放 - 元素不会拖放
- 如何使用HTML2canvas截取拖放元素的屏幕截图
- 拖放元素可以移动 x 但不能移动 y
- HTML5 拖放 - 将选择文本拖放到可拖放元素
- 使用jQuery拖放元素
- 添加可拖放元素后,使其相对于可拖放元素的坐标
- 如何找到可拖放元素相对于可拖放区域的位置?
- 获取日历中可拖放元素的开始和结束日期
- 如何使动态创建的元素接受可拖放元素
- 如何保留拖放元素的位置?
- Jquery .data在可拖放区域的可拖放元素上一直没有定义
- Javascript: handleDragLeave和handleDragEnd没有移除拖放元素时的类效果
- 在用户生成的可拖放矩阵中对可拖放元素居中
- 不能在拖放后再拖放元素
- js在两张纸之间拖放元素
- 为带有量角器的离子应用程序创建一些测试,以测试用户是否可以成功地在chrome上拖放元素
- 如何在拖放元素时执行函数