admin管理员组文章数量:1335135
I am building a website with a scrolling Javascript time line, using code from this tutorial. 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
. 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
. Then on mouseup
we check whether to "pass on" the mouseup
event to the browser. As you can tell, I'm not overly familiar with js!
Any help appreciated.
I am building a website with a scrolling Javascript time line, using code from this tutorial. 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
. 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
. Then on mouseup
we check whether to "pass on" the mouseup
event to the browser. As you can tell, I'm not overly familiar with js!
Any help appreciated.
Share Improve this question edited Jan 25, 2022 at 19:32 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 20, 2010 at 9:24 mojonesmojones 1,6803 gold badges15 silver badges26 bronze badges3 Answers
Reset to default 2Your correct. The solution is just create a flag variable that whenever the user drags the timeline, (event.preventDefault()) the anchor tags. 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.
You can stop all events in javascript (jQuery framework for instance) when the mouse click on a link. Event.stop() You can try this or you can program that when the cursor is over the link, wait an instant to activate it.
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. Quirksmode has pretty good documentation on reading coordinates from mouse events:
http://www.quirksmode/js/events_properties.html
本文标签:
版权声明:本文标题:jquery - How to capture mouse click with Javascript when the mouse has moved between mousedown and mouseup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742357596a2459728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论