js实现框选功能?

用html实现以下功能:
1,页面中间有一个1000*600的画布
2,画布上按住鼠标左键拖拽可以实现一个框选的功能
3,鼠标松开后选框消失

要求:框选用div实现,不要用canvas

阅读 2k
2 个回答

image.png

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Div Selection Demo</title>
    <style>
        #canvas {
            position: relative;
            width: 1000px;
            height: 600px;
            border: 1px solid black;
        }

        #selection-box {
            position: absolute;
            border: 2px dashed blue; /* 将边框样式改成虚线 */
            display: none;
        }
    </style>
</head>
<body>
<div id="canvas">
    <div id="selection-box"></div>
</div>
<script>
    const canvas = document.getElementById('canvas');
    const selectionBox = document.getElementById('selection-box');
    let isDragging = false;
    let startX = 0;
    let startY = 0;

    canvas.addEventListener('mousedown', e => {
        isDragging = true;
        startX = e.clientX - canvas.offsetLeft;
        startY = e.clientY - canvas.offsetTop;
        selectionBox.style.left = startX + 'px';
        selectionBox.style.top = startY + 'px';
        selectionBox.style.width = '0px';
        selectionBox.style.height = '0px';
        selectionBox.style.display = 'block';
    });

    canvas.addEventListener('mousemove', e => {
        if (isDragging) {
            const currentX = e.clientX - canvas.offsetLeft;
            const currentY = e.clientY - canvas.offsetTop;
            const width = currentX - startX;
            const height = currentY - startY;
            selectionBox.style.width = Math.abs(width) + 'px';
            selectionBox.style.height = Math.abs(height) + 'px';
            selectionBox.style.left = Math.min(currentX, startX) + 'px';
            selectionBox.style.top = Math.min(currentY, startY) + 'px';
        }
    });

    canvas.addEventListener('mouseup', e => {
        isDragging = false;
        selectionBox.style.display = 'none';
    });
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #canvas-container {
      background-color: aqua;
    }
    #selection-box {
      background: rgba(0, 0, 255, 0.2);
    }
  </style>
</head>

<body>
  <div id="canvas-container" style="position: relative; width: 1000px; height: 600px;">
    <div id="selection-box" style="position: absolute; border: 2px solid black; display: none;"></div>
  </div>

  <script>
    const canvasContainer = document.getElementById("canvas-container");
    const selectionBox = document.getElementById("selection-box");
    let isDragging = false;
    let startX, startY, endX, endY;

    canvasContainer.addEventListener("mousedown", (e) => {
      isDragging = true;
      startX = e.clientX - canvasContainer.offsetLeft;
      startY = e.clientY - canvasContainer.offsetTop;
    });

    canvasContainer.addEventListener("mousemove", (e) => {
      if (isDragging) {
        endX = e.clientX - canvasContainer.offsetLeft;
        endY = e.clientY - canvasContainer.offsetTop;
        selectionBox.style.display = "block";
        selectionBox.style.left = Math.min(endX, startX) + "px";
        selectionBox.style.top = Math.min(endY, startY) + "px";
        selectionBox.style.width = Math.abs(endX - startX) + "px";
        selectionBox.style.height = Math.abs(endY - startY) + "px";
      }
    });

    canvasContainer.addEventListener("mouseup", (e) => {
      isDragging = false;
      selectionBox.style.display = "none";
      // Add code here to select the elements within the rectangle
    });
  </script>
</body>

</html>
推荐问题
logo
Microsoft
子站问答
访问
宣传栏