
如何实现谷歌浏览器进度条拖动至区域外仍触发鼠标移动事件
在谷歌浏览器中,传统的事件捕获方法(例如 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;
// 在此执行进度条拖动至区域外的处理逻辑
}
function handleMoveEnd(e: MouseEvent) {
window.removeEventListener('mousemove', handleMoving);
window.removeEventListener('mousedown', handleMoveEnd);
startPoint = undefined;
if (document.onselectstart !== originalOnSelectStart) {
document.onselectstart = originalOnSelectStart;
}
}此代码通过禁用文本选择(onselectstart)并添加mousemove事件侦听器,实现了进度条拖动至区域外仍触发鼠标移动事件。当鼠标按下时,它会记录开始点,并在此后持续触发移动事件,直到鼠标松开。
以上就是如何让谷歌浏览器进度条拖动至区域外仍触发鼠标移动事件?的详细内容,更多请关注php中文网其它相关文章!
谷歌浏览器Google Chrome是一款可让您更快速、轻松且安全地使用网络的浏览器。Google Chrome的设计超级简洁,使用起来得心应手。这里提供了谷歌浏览器纯净安装包,有需要的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号