在 Chrome 浏览器中,如何解决进度条区域外拖动进度条时鼠标移动事件无法触发的问题?
chrome 区域外事件捕捉
在 chrome 浏览器中,由于 setcapture() 和 window.captureevents() 已被弃用,在进度条区域外拖动进度条时,将无法触发鼠标移动事件。以下代码通过监听 mousedown 事件来避免这种情况,并允许在进度条区域外继续触发鼠标移动事件:
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; } }
以上就是在 Chrome 浏览器中,如何解决进度条区域外拖动进度条时鼠标移动事件无法触发的问题?的详细内容,更多请关注www.sxiaw.com其它相关文章!