提问人:isaiah day 提问时间:11/17/2023 更新时间:11/17/2023 访问量:23
检测用户是否正在调整文本框的大小
Detect if the user is resizing a textbox
问:
我有一些代码允许移动文本区域:
// Note that this code is paraphrased. It may not work exactly.
function createSelection(e) {
if (dragging == false) {
newSelection = document.createElement('textarea');
newSelection.classList.add('resizeBox');
newSelection.style.position = "absolute";
newSelection.style.left = e.pageX + 'px';
newSelection.style.top = e.pageY + 'px';
newSelection.style.width = '0%';
newSelection.style.height = '0%';
newSelection.style.border = "dashed 2px orange";
newSelection.style.outline = "0"
startX = e.pageX;
startY = e.pageY
document.body.appendChild(newSelection);
newSelection.addEventListener('mousedown', dragSelection);
window.addEventListener('mousemove', resizeSelection);
window.addEventListener('mouseup', finalizeSelection);
}
}
function dragSelection(e) {
var target = e.target
dragging = true
target.style.cursor = 'grabbing';
// TODO: Make this box ontop of everything else
let offsetX = e.offsetX;
let offsetY = e.offsetY;
window.addEventListener('mousemove', moveSelection);
window.addEventListener('mouseup', stopDragSelection);
function moveSelection(e) {
if (e.target.style.cursor == "nwse-resize") {
return null;
}
console.log(2)
target.style.left = (e.pageX - offsetX) + 'px';
target.style.top = (e.pageY - offsetY) + 'px';
}
function stopDragSelection() {
target.style.cursor = 'grab';
dragging = false
window.removeEventListener('mousemove', moveSelection);
window.removeEventListener('mouseup', stopDragSelection);
}
}
function resizeSelection(e) {
var width = e.pageX - startX;
var height = e.pageY - startY;
newSelection.style.width = Math.abs(width) + 'px';
newSelection.style.height = Math.abs(height) + 'px';
newSelection.style.left = (width < 0 ? e.pageX : startX) + 'px';
newSelection.style.top = (height < 0 ? e.pageY : startY) + 'px';
newSelection.style.border = "dashed 2px orange";
}
function finalizeSelection(e) {
window.removeEventListener('mousemove', resizeSelection);
window.removeEventListener('mouseup', finalizeSelection);
}
这工作正常,但是当我尝试调整文本框的大小时,通过单击右下角的小图标,它会移动它。如何判断用户是否正在点击该图标?
我试图判断光标是否是某个图标:
if (e.target.style.cursor == "nwse-resize") {
return null;
}
但这没有用。
我还试图查看文本框的宽度和高度是否正在更改,但这不起作用,因为它没有被调整大小,它只是被拖动。
提前致谢!
答:
1赞
Eksidow
11/17/2023
#1
若要检测文本区域上的大小调整操作,请在代码中检查光标相对于其右下角的位置,如下所示:
function dragSelection(e) {
var target = e.target;
dragging = true;
target.style.cursor = 'grabbing';
// Get the dimensions of the textarea
var textareaRect = target.getBoundingClientRect();
// Calculate the distance from the cursor to the bottom right corner
var offsetX = textareaRect.width - (e.clientX - textareaRect.left);
var offsetY = textareaRect.height - (e.clientY - textareaRect.top);
window.addEventListener('mousemove', moveSelection);
window.addEventListener('mouseup', stopDragSelection);
function moveSelection(e) {
// Check if the cursor is near the bottom right corner
if (offsetX <= 10 && offsetY <= 10) {
// Cursor is in the resize area
resizeSelection(e);
} else {
// Cursor is outside the resize area, so move the textarea
target.style.left = (e.pageX - offsetX) + 'px';
target.style.top = (e.pageY - offsetY) + 'px';
}
}
function stopDragSelection() {
target.style.cursor = 'grab';
dragging = false;
window.removeEventListener('mousemove', moveSelection);
window.removeEventListener('mouseup', stopDragSelection);
}
}
总结:计算光标到右下角的距离,检查是否在10像素以内。如果是这样,则使用 resizeSelection 调整大小,否则移动文本区域。根据需要调整阈值。
上一个:深度复制派生类中的指针向量
评论