用前端代码写一段监听鼠标拖拽事件的例子
时间: 2023-03-15 10:04:09 浏览: 132
答:下面是一段监听鼠标拖拽事件的前端代码:let mousePosition;document.addEventListener('mousedown', (e) => {
mousePosition = { x: e.pageX, y: e.pageY };
});document.addEventListener('mouseup', (e) => {
const distance = Math.sqrt(
Math.pow(mousePosition.x - e.pageX, 2) + Math.pow(mousePosition.y - e.pageY, 2)
);
if (distance > 5) {
// 鼠标拖拽事件
}
});
阅读全文