gpt4 book ai didi

javascript - 使用 javascript 的拖放问题

转载 作者:行者123 更新时间:2023-12-05 00:32:28 25 4
gpt4 key购买 nike

亲爱的,
我正在尝试构建拖放功能,我将在其中移动 HTML 字段,例如(文本字段、复选框、文本区域等)
除了复选框和单选按钮之外,该代码适用于所有类型的输入字段!它将这两个字段移动到错误的位置!
你能帮忙吗?

dragElement(document.getElementById("ChkBox"));
dragElement(document.getElementById("TxtFld"));
dragElement(document.getElementById("RadioButton"));

function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
var MouseMoveCounterVar = 0;

document.getElementById(elmnt.id).onmousedown = dragMouseDown;

function dragMouseDown(e) {
MouseMoveCounterVar = 0;
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup compaired to the window:
pos3 = e.clientX;
pos4 = e.clientY;

document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}

function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;

elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}

function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
.FieldDrag {
position: absolute;
z-index: 9;
cursor: move;
}
<body style="margin: 0; background-color:silver">
<div id="ViewArea" style="width: 100%; height:100%;">
<input id="TxtFld" type="text" class="FieldDrag" /><br/>
<input id="ChkBox" type="checkbox" class="FieldDrag"/><br/>
<input id="RadioButton" type="radio" class="FieldDrag"/><br/>
</div>
</body>

最佳答案

通过在变量中拖动保存元素目标(我称之为 inputField)
在 mousedown 事件中保存元素的偏移量
并通过以下方式更改计算

    inputField.style.left = (e.clientX + offset[0]) + 'px';
inputField.style.top = (e.clientY + offset[1]) + 'px';

dragElement(document.getElementById("ChkBox"));
dragElement(document.getElementById("TxtFld"));
dragElement(document.getElementById("TxtFld2"));
dragElement(document.getElementById("RadioButton"));

function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
var MouseMoveCounterVar = 0;
var offset = [0,0];

var inputField = document.getElementById(elmnt.id);
inputField.onmousedown = dragMouseDown;

function dragMouseDown(e) {
MouseMoveCounterVar = 0;
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup compaired to the window:

offset = [
inputField.offsetLeft - e.clientX,
inputField.offsetTop - e.clientY
];

document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}

function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
inputField.style.left = (e.clientX + offset[0]) + 'px';
inputField.style.top = (e.clientY + offset[1]) + 'px';
}

function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
.FieldDrag {
position: absolute;
z-index: 9;
cursor: move;
}
<body style="margin: 0; background-color:silver">
<div id="ViewArea" style="width: 100%; height:100%;position:relative; top:0; left:0;">
<input id="TxtFld" type="text" class="FieldDrag" /><br/>
<input id="ChkBox" type="checkbox" class="FieldDrag"/><br/>
<input id="RadioButton" type="radio" class="FieldDrag"/><br/>
<input id="TxtFld2" type="text" class="FieldDrag" /><br/>
</div>
</body>

关于javascript - 使用 javascript 的拖放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70604198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com