当前位置:首页 > JavaScript

js怎么实现滑动选取

2026-04-04 19:56:58JavaScript

滑动选取实现方法

监听鼠标事件

通过监听 mousedownmousemovemouseup 事件实现滑动选取功能。在 mousedown 时记录起始位置,mousemove 时计算选区范围,mouseup 时结束选取。

let isSelecting = false;
let startX, startY;

document.addEventListener('mousedown', (e) => {
  isSelecting = true;
  startX = e.clientX;
  startY = e.clientY;
});

document.addEventListener('mousemove', (e) => {
  if (!isSelecting) return;

  const currentX = e.clientX;
  const currentY = e.clientY;
  const width = currentX - startX;
  const height = currentY - startY;

  // 更新选区样式或逻辑
});

document.addEventListener('mouseup', () => {
  isSelecting = false;
});

绘制选区框

动态创建一个 div 元素作为选区框,通过 CSS 设置边框和半透明背景。

const selectionBox = document.createElement('div');
selectionBox.style.position = 'absolute';
selectionBox.style.border = '2px dashed #000';
selectionBox.style.backgroundColor = 'rgba(0, 0, 255, 0.1)';
document.body.appendChild(selectionBox);

// 在 mousemove 中更新选区框位置和尺寸
selectionBox.style.left = `${Math.min(startX, currentX)}px`;
selectionBox.style.top = `${Math.min(startY, currentY)}px`;
selectionBox.style.width = `${Math.abs(width)}px`;
selectionBox.style.height = `${Math.abs(height)}px`;

检测元素碰撞

通过比较元素位置与选区框的范围,判断是否被选中。

function isElementInSelection(element, selectionBox) {
  const rect = element.getBoundingClientRect();
  const boxRect = selectionBox.getBoundingClientRect();

  return !(
    rect.right < boxRect.left ||
    rect.left > boxRect.right ||
    rect.bottom < boxRect.top ||
    rect.top > boxRect.bottom
  );
}

// 遍历所有目标元素检测碰撞
document.querySelectorAll('.selectable').forEach(el => {
  if (isElementInSelection(el, selectionBox)) {
    el.classList.add('selected');
  } else {
    el.classList.remove('selected');
  }
});

优化性能

对于大量元素,使用 requestAnimationFrame 节流 mousemove 事件。

let rafId;
document.addEventListener('mousemove', (e) => {
  if (!isSelecting) return;

  cancelAnimationFrame(rafId);
  rafId = requestAnimationFrame(() => {
    // 更新选区逻辑
  });
});

触摸屏支持

添加 touchstarttouchmovetouchend 事件监听以实现移动端兼容。

document.addEventListener('touchstart', (e) => {
  isSelecting = true;
  startX = e.touches[0].clientX;
  startY = e.touches[0].clientY;
});

document.addEventListener('touchmove', (e) => {
  if (!isSelecting) return;
  const currentX = e.touches[0].clientX;
  const currentY = e.touches[0].clientY;
  // 更新选区逻辑
});

清除选区

mouseuptouchend 后移除选区框,或保留选择状态。

js怎么实现滑动选取

document.addEventListener('mouseup', () => {
  isSelecting = false;
  selectionBox.style.display = 'none';
});

以上方法组合可实现基础滑动选取功能,可根据实际需求调整样式和交互逻辑。

标签: js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含inpu…

js实现图片

js实现图片

图片加载与显示 在JavaScript中,可以通过Image对象动态加载图片。创建实例后设置src属性触发加载,通过onload回调处理加载完成后的操作: const img = new Ima…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…