[英]How to add 2 addeventlisteners to work together (e.g.: run event on dblclick when mouse pointer is at left side of screen)
New to JavaScript, trying to figure out how to combine addEventListener events. JavaScript 的新手,试图弄清楚如何组合 addEventListener 事件。
For example, I want to trigger something when I double-click the side of the screen, within 30 or 40 pixels.例如,我想在我双击屏幕的一侧时触发一些东西,在 30 或 40 像素内。
I assume I need to combine these somehow:我想我需要以某种方式结合这些:
document.addEventListener("dblclick",function (event) {
document.addEventListener("mouseover", (event) => {
How is that done?这是怎么做的? I'm also struggling to get that mouseover working even alone, if anyone cares to take a stab at it!
如果有人愿意尝试一下,我也很难让鼠标悬停在一个人的工作中!
You don't need mouseover event listener to do that.您不需要 mouseover 事件侦听器来执行此操作。
Method 1: First create 30-40pixel div element, with position:fixed .方法一:首先创建 30-40pixel 的div元素,使用position:fixed 。 Then add double click event to that div element.
然后将双击事件添加到该div元素。
Method 2: Add doubleclick event to the document, read event.clientX (or event.screenX).方法二:给文档添加双击事件,读取event.clientX(或event.screenX)。 if event.clientX < 30, run what you want.
如果 event.clientX < 30,运行你想要的。
document.addEventListener("dblclick",function (event) {
if(event.clientX < 30 ) {
// some logic
}
})
Here is how you can listen for double click events and get the mouse x coordinate (which you can then use to check if it's within 30 or 40 pixels of the left side).这是您如何监听双击事件并获取鼠标 x 坐标的方法(然后您可以使用它来检查它是否在左侧 30 或 40 像素内)。
const elem = document.querySelector('body'); elem.addEventListener('dblclick', e => { console.log(e.clientX); });
<div style="height: 500px;"> Double click to log mouse x coordinate. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.