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
2 Answers
Reset to default 4The 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条)