简体   繁体   English

当鼠标在mousedown和mouseup之间移动时,如何使用javascript捕获鼠标单击

[英]How to capture mouse click with javascript when the mouse has moved between mousedown and mouseup

I am building a website with a scrolling javascript time line, using code from this tutorial . 我正在使用本教程中的代码构建一个滚动javascript时间线的网站。 There is a demo to go with the tutorial here . 有一个演示去的教程在这里

My problem is as follows: if the user clicks on the timeline to drag it, and they happen to click on a link, then when the mouse button is released, the browser interprets that as a click on the link. 我的问题如下:如果用户点击时间轴进行拖动,并且他们碰巧点击链接,那么当释放鼠标按钮时,浏览器会将其解释为点击链接。 Hence, it's very easy to navigate away from the timeline by accident. 因此,很容易意外地离开时间轴。

The behaviour I would like is as follows: clicking on a link only triggers navigation if the mouse has not been moved between mousedown and mouseup. 我想要的行为如下: 如果鼠标尚未在mousedown和mouseup之间移动, 单击链接触发导航。 If the mouse has been moved while the button is held down, then the link is not followed, since the user is trying to move the timeline rather than click on a link. 如果在按住按钮的同时移动鼠标,则不会跟踪链接,因为用户正在尝试移动时间轴而不是单击链接。

Is this possible? 这可能吗? I have a feeling we need a is_mouse_moved boolean variable that is set to false on mousedown and set to true on mousemove. 我有一种感觉,我们需要一个is_mouse_moved布尔变量,在mousedown上设置为false,在mousemove上设置为true。 Then on mouseup we check whether to "pass on" the mouseup event to the browser. 然后在mouseup上检查是否将mouseup事件“传递”到浏览器。 As you can tell, I'm not overly familiar with js! 你可以告诉我,我对js并不过分熟悉!

Any help appreciated. 任何帮助赞赏。

You can stop all events in javascript (jQuery framework for instance) when the mouse click on a link. 当鼠标单击链接时,您可以停止javascript(例如jQuery框架)中的所有事件。 Event.stop() You can try this or you can program that when the cursor is over the link, wait an instant to activate it. Event.stop()你可以尝试这个,或者你可以编程当光标在链接上时,等待一下即可激活它。

You probably want to store the coordinates when a mousedown event is triggered, and read the coordinates in your mouseup to see if they have changed by a significant amount. 您可能希望在触发mousedown事件时存储坐标,并读取mouseup中的坐标以查看它们是否已经发生了相当大的变化。 Quirksmode has pretty good documentation on reading coordinates from mouse events: Quirksmode在阅读鼠标事件的坐标方面有很好的文档:

http://www.quirksmode.org/js/events_properties.html http://www.quirksmode.org/js/events_properties.html

Your correct. 你的正确。 The solution is just create a flag variable that whenever the user drags the timeline, ( event.preventDefault() ) the anchor tags. 解决方案只是创建一个标志变量,每当用户拖动时间轴时,( event.preventDefault() )锚标记。 This solution might pose more errors though because what if the user "accidentally" slightly drags the timeline but what he/she wants is to click the link? 这个解决方案可能会造成更多错误,因为如果用户“意外”轻微拖动时间线,但他/她想要的是点击链接? the timeline might not be that responsive anymore. 时间表可能不再具有响应性。

My suggestion is, prevent default all anchor tags then use double click to visit a particular link. 我的建议是,防止默认所有锚标签然后使用双击访问特定链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mousedown触发事件,然后将鼠标从元素上移开,而mouseup触发则不起作用 - Mousedown triggers event, then when mouse is moved off element and mouseup fires it doesn't work 即使元素在 mousedown 和 mouseup 之间从光标下方移动,如何处理“单击”事件? - How to handle "click" event even if element is moved from beneath the cursor between mousedown and mouseup? 如何检索mousedown到mouseup事件之间的所有鼠标坐标 - How can I retrieve all mouse coordinates between mousedown to mouseup event 如何知道鼠标按钮的当前状态(mouseup状态或mousedown状态) - How to know the current state of mouse button(mouseup state or mousedown state) 如果在mousedown之后移动鼠标,则取消传入的jQuery单击事件 - Cancel incoming jQuery click event if mouse is moved after mousedown 区分 mouseup mousedown 和 click - Differentiating between mouseup mousedown and click 如何检测鼠标何时触发鼠标移动? - How to detect if mouse is moving when mouseup is fired? 如何使用JavaScript在鼠标单击时捕获共享点页面上的每个元素? - How to capture every element on sharepoint page on mouse click using javascript? 如何在 JavaScript 上捕获双击鼠标中键(滚轮)事件? - How to capture the double click mouse middle button(wheel) event on JavaScript? 鼠标移动速度非常快时,无法正确捕获Javascript鼠标事件 - Javascript mouse event not captured properly when mouse moved very fast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM