[英]how can i set this removeEventListener's param?
I was about to make some rotation programm (worked by mouse click & drag) but the 'removeEventListener' doesn't work.我正要制作一些旋转程序(通过鼠标单击和拖动来工作)但是“removeEventListener”不起作用。 can u explain me how to work it and why doesn't it work?
你能解释一下它是如何工作的吗?为什么它不起作用?
And this is my first question in here so if u find any problems about this question, I'll gladly accept it.这是我在这里的第一个问题,所以如果你发现这个问题有任何问题,我会很乐意接受。
<body>
<div class="wrap">
<div class="target">target</div>
</div>
</body>
const html = document.querySelector("html");
const info = document.querySelector(".info");
const target = document.querySelector(".target");
const wrap = document.querySelector(".wrap");
let center = {
x: target.getBoundingClientRect().left + target.clientWidth / 2,
y: target.getBoundingClientRect().top + target.clientHeight / 2,
};
window.addEventListener("resize", () => {
center = {
x: target.getBoundingClientRect().left + target.clientWidth / 2,
y: target.getBoundingClientRect().top + target.clientHeight / 2,
};
});
const rotate = function () {
target.addEventListener("mousemove", (e) => {
const x = center.x - e.clientX;
const y = center.y - e.clientY;
const radian = Math.atan2(y, x);
const degree = ((radian * 180) / Math.PI).toFixed(0);
target.style.transform = "rotate(" + degree + "deg)";
});
};
target.addEventListener("mousedown", rotate, true);
target.addEventListener("mouseup", () => {
target.removeEventListener("mousedown", rotate, false);
});
I've tried to change this part.我试图改变这部分。 target -> wrap and removeEventListener's param to another one.
target -> 包装和 removeEventListener 的参数到另一个。 But none of those worked
但这些都不起作用
target.addEventListener("mouseup", () => {
target.removeEventListener("mousedown", rotate, false);
});
The capture/ use capture falg must match to remove listener.捕获/使用捕获标志必须匹配才能删除侦听器。 Since you used true for your
mousedown
even listener you also must use true to remove event listener:由于您对
mousedown
甚至侦听器使用了 true ,因此您还必须使用 true 来删除事件侦听器:
target.addEventListener("mouseup", () => {
target.removeEventListener("mousedown", rotate, true);
});
See docs for more details: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener#matching_event_listeners_for_removal有关详细信息,请参阅文档: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener#matching_event_listeners_for_removal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.