javascript - Trigger an event IMMEDIATELY on mouse click, NOT after I let go of the mouse? - Stack Overflow

I have probably a dumb question but here we go:When you trigger a function with onclick="function

I have probably a dumb question but here we go:

When you trigger a function with onclick="function()" or addEventListener onclick the function gets triggered only AFTER I click AND let go of the mouse.

Is there any way to trigger a function IMMEDIATELY on clicking an html element, NOT after clicking and letting go.

I want this to work anywhere I click on the body element.

I have probably a dumb question but here we go:

When you trigger a function with onclick="function()" or addEventListener onclick the function gets triggered only AFTER I click AND let go of the mouse.

Is there any way to trigger a function IMMEDIATELY on clicking an html element, NOT after clicking and letting go.

I want this to work anywhere I click on the body element.

Share Improve this question asked Aug 17, 2020 at 15:45 Ivelin Ivelin 1592 silver badges12 bronze badges 3
  • 1 There's a "mousedown" event. – Pointy Commented Aug 17, 2020 at 15:48
  • 4 While mousedown has been pointed out, let's think about it logically: since you can drag-and-drop in JS, there must be a way to get just the "down" and "up" events. Given that, searching the web for "js mouse down event" would have answered this for you almost immediately. I wouldn't call it a dumb question--but it's a question that can be self-answered relatively easily. That is the lesson I'd take away. – Dave Newton Commented Aug 17, 2020 at 15:49
  • Yes, you're right. I don't know where my mind went. I forgot about this pletely. Thanks guys. – Ivelin Commented Aug 17, 2020 at 16:16
Add a ment  | 

2 Answers 2

Reset to default 4

The click event is triggered after the entire mouse action has been pleted.

Use mousedown instead of click.

document.addEventListener("mousedown", function(){
  console.log("You did it!");
});

Please check the mousedown event: https://www.w3schools./jsref/event_onmousedown.asp

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745109240a4611747.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信