简体   繁体   English

如何添加 2 个 addeventlisteners 一起工作(例如:当鼠标指针位于屏幕左侧时,在 dblclick 上运行事件)

[英]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.

相关问题 Node.js / Express-如何考虑客户端数据库的工作? (例如,不能使用require()) - Node.js / Express - how to think about client-side database work? (e.g. can't use require()) 如何在dblClick蚂蚁事件(evt)上运行selectfunction - How to run a selectfunction on dblClick ant event (evt) 如何在现有语言(例如打字稿)中添加标记? - How to add a token to an existing language (e.g. typescript)? 使用带有副作用的 React 自定义 Hook 进行事件处理(例如 OnClick) - Using A React Custom Hook With Side Effects For Event Handling (e.g. OnClick) 如何在鼠标指针的右侧/左侧显示传单弹出窗口? - How to display the leaflet popup on the right/left side of mouse pointer? 看起来您正在为屏幕“添加”的“组件”道具传递内联 function(例如 component={() =><somecomponent /> }) - Looks like you're passing an inline function for 'component' prop for the screen 'Add' (e.g. component={() => <SomeComponent />}) 如何用鼠标 select 一系列元素(例如日历日期范围选择器)? - How to select a range of elements with mouse (e.g. calendar date-range picker)? 如果没有屏幕,screen.width的值是什么? 例如机器人 - What value is screen.width if there is no screen? e.g. bots 当处理程序访问 originalEvent 时触发事件(例如通过 jQuery) - Trigger event (e.g. via jQuery) when handler accesses originalEvent 将“异步”方法绑定到“on”事件(例如单击)时是否存在任何潜在问题? - Are there any potential issues when binding an “async” method to a “on” event (e.g. click)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM