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