chrome 区域外事件捕捉
在 chrome 浏览器中,使用 setcapture() 和 window.captureevents() 来实现区域外事件捕捉已不再可行。那么,如何实现进度条拖动至进度条区域外,仍然触发鼠标移动事件呢?
为此,我们采用以下解决方案:
具体代码如下:
const button = document.querySelector('button'); button?.addEventListener('mousedown', handleMoveStart); let startPoint: { x: number; y: number } | undefined; let originalOnSelectStart: Document['onselectstart'] = null; function handleMoveStart(e: MouseEvent) { e.stopPropagation(); if (e.ctrlKey || [1, 2].includes(e.button)) return; window.getSelection()?.removeAllRanges(); e.stopImmediatePropagation(); window.addEventListener('mousemove', handleMoving); window.addEventListener('mousedown', handleMoveEnd); originalOnSelectStart = document.onselectstart; document.onselectstart = () => false; startPoint = { x: e.x, y: e.y }; } function handleMoving(e: MouseEvent) { if (!startPoint) return; // DO Something } function handleMoveEnd(e: MouseEvent) { window.removeEventListener('mousemove', handleMoving); window.removeEventListener('mousedown', handleMoveEnd); startPoint = undefined; if (document.onselectstart !== originalOnSelectStart) { document.onselectstart = originalOnSelectStart; } }
以上就是如何在不使用 setCapture() 的情况下实现区域外拖动事件触发?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号