如何通过mousedown>mousemove>mouseup(纯JS)选择多个DIV。

huangapple go评论70阅读模式
英文:

How to select multiple DIVs by mousedown>mousemove>mouseup (pure JS)

问题

我试图在单击空白点并移动指针时选择元素。

在这个示例中,我期望选择两个元素:

如何通过mousedown>mousemove>mouseup(纯JS)选择多个DIV。

我尝试了范围和选择,但结果不正确。

const mainDiv = document.createElement("div");
mainDiv.style.width = "500px";
mainDiv.style.height = "500px";
document.body.appendChild(mainDiv);

const div1 = document.createElement("div");
div1.style.position = "absolute";
div1.style.top = `${50}px`;
div1.style.left = `${50}px`;
div1.style.width = "100px";
div1.style.height = "100px";
div1.style.background = "red";
mainDiv.appendChild(div1);

const div2 = document.createElement("div");
div2.style.top = `${250}px`;
div2.style.left = `${250}px`;
div2.style.width = "100px";
div2.style.height = "100px";
div2.style.background = "green";
div2.style.position = "absolute";
mainDiv.appendChild(div2);

mainDiv.onmousedown = function(event) {
  function onMouseMove(event) {
    // 将 div 添加到选择中
  }
  mainDiv.addEventListener('mousemove', onMouseMove);

  mainDiv.onmouseup = function() {
    console.log("已选择的 div 元素")
  }
}
英文:

I'm trying to make a selection of elements when I click on empty point and move pointer.

In this example I'm expecting to get selection of two elements:

如何通过mousedown>mousemove>mouseup(纯JS)选择多个DIV。

I've tried range and selection, but not with the proper result.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const mainDiv = document.createElement(&quot;div&quot;);
mainDiv.style.width = &quot;500px&quot;;
mainDiv.style.height = &quot;500px&quot;;
document.body.appendChild(mainDiv);

const div1 = document.createElement(&quot;div&quot;);
div1.style.position = &quot;absolute&quot;;
div1.style.top = `px`;
div1.style.left = `px`;
div1.style.width = &quot;100px&quot;;
div1.style.height = &quot;100px&quot;;
div1.style.background = &quot;red&quot;;
mainDiv.appendChild(div1);

const div2 = document.createElement(&quot;div&quot;);
div2.style.top = `${250}px`;
div2.style.left = `${250}px`;
div2.style.width = &quot;100px&quot;;
div2.style.height = &quot;100px&quot;;
div2.style.background = &quot;green&quot;;
div2.style.position = &quot;absolute&quot;;
mainDiv.appendChild(div2);

mainDiv.onmousedown = function(event) {
  function onMouseMove(event) {
    //add divs to selection
  }
  mainDiv.addEventListener(&#39;mousemove&#39;, onMouseMove);

  mainDiv.onmouseup = function() {
    console.log(&quot;selected divs&quot;)
  }
}

<!-- end snippet -->

答案1

得分: 0

你可以通过保持一个选定项目的数组,在鼠标移动到项目上并且鼠标被按下时将项目推送到数组中来解决这个问题。

const mainDiv = document.createElement("div");
mainDiv.style.width = "500px";
mainDiv.style.height = "500px";
document.body.appendChild(mainDiv);

const div1 = document.createElement("div");
div1.style.position = "absolute";
div1.style.top = `${50}px`;
div1.style.left = `${50}px`;
div1.style.width = "100px";
div1.style.height = "100px";
div1.style.background = "red";
mainDiv.appendChild(div1);

const div2 = document.createElement("div");
div2.style.top = `${250}px`;
div2.style.left = `${250}px`;
div2.style.width = "100px";
div2.style.height = "100px";
div2.style.background = "green";
div2.style.position = "absolute";
mainDiv.appendChild(div2);

(() => {
  let selection = [];
  let selecting = false;
  function beginSelection(e) {
    selection = [];
    selecting = true;
    checkSelection(e);
  }
  function mouseMove(e) {
    checkSelection(e);
  }
  function mouseUp(e) {
    selecting = false;
    if (selection.length) {
      console.log("selection: ", selection); // 在重置之前访问选择
      selection = [];
    } else {
      // 没有选择
    }
  }
  function checkSelection(e) {
    if (!selecting) {
      return; // 忽略
    }
    const selected = e.target.parentNode === mainDiv && e.target;
    if (selected && !selection.includes(selected)) {
      selection.push(selected);
    }
  }
  mainDiv.addEventListener("mousedown", beginSelection);
  mainDiv.addEventListener("mousemove", mouseMove);
  window.addEventListener("mouseup", mouseUp);
})();

请注意,这是你提供的代码的翻译。

英文:

You can solve this by keeping an array of selected items and pushing items to it when the mouse moves over the items if the mouse is depressed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const mainDiv = document.createElement(&quot;div&quot;);
mainDiv.style.width = &quot;500px&quot;;
mainDiv.style.height = &quot;500px&quot;;
document.body.appendChild(mainDiv);
const div1 = document.createElement(&quot;div&quot;);
div1.style.position = &quot;absolute&quot;;
div1.style.top = `${50}px`;
div1.style.left = `${50}px`;
div1.style.width = &quot;100px&quot;;
div1.style.height = &quot;100px&quot;;
div1.style.background = &quot;red&quot;;
mainDiv.appendChild(div1);
const div2 = document.createElement(&quot;div&quot;);
div2.style.top = `${250}px`;
div2.style.left = `${250}px`;
div2.style.width = &quot;100px&quot;;
div2.style.height = &quot;100px&quot;;
div2.style.background = &quot;green&quot;;
div2.style.position = &quot;absolute&quot;;
mainDiv.appendChild(div2);
!(() =&gt; {
let selection = [];
let selecting = false;
function beginSelection(e) {
selection = [];
selecting = true;
checkSelection(e);
}
function mouseMove(e) {
checkSelection(e);
}
function mouseUp(e) {
selecting = false;
if (selection.length) {
console.log(&quot;selection: &quot;, selection); // access selection before reset
selection = [];
} else {
// no selection
}
}
function checkSelection(e) {
if (!selecting) {
return; // ignore
}
const selected = e.target.parentNode === mainDiv &amp;&amp; e.target;
if (selected &amp;&amp; !selection.includes(selected)) {
selection.push(selected);
}
}
mainDiv.addEventListener(&quot;mousedown&quot;, beginSelection);
mainDiv.addEventListener(&quot;mousemove&quot;, mouseMove);
window.addEventListener(&quot;mouseup&quot;, mouseUp);
})();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 20:38:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定