Jquery Detect First Mouse Movement
I'm looking to detect the first occurrence of a mouse movement so I can target human visitors with jquery affects. Any leads on how I might accomplish this. I'd also like to do this without t开发者_运维技巧argeting class names or object ids... so a blanked target on the tag should do. Any advice?
$('body').one('mousemove', function() { .... } );
This will look for mouse movement anywhere, but will only do it once.
Note: This isn't exactly a reliable method, smartphones don't have mice. And a non-human program can still move the mouse.
But it does answer your question.
Mouse events bubble to the document unless blocked. You can generally see them with a mousemove handler on the document:
document.onmousemove = function() {console.log("mousemove");}
or in jQuery:
$(document).mousemove(function() {
// put your code here
});
If you only want to see a few initial mouse movements, you can remove the event handler at the desired time or just ignore future events once you've detected a human.
Why would you bother not running animations when it's not a human? Does it cost you something? Animation is a client-side thing, right? Chances are, if it's not a human (like a bot of some type), it isn't running javascript anyway.
Not sure if I understand your purpose corretly, but how about something like this:
var detectMouseMovement = function() {
$(document).unbind('mousemove', detectMouseMovement);
// do whatever you need to do..
};
$(document).bind('mousemove', detectMouseMovement);
精彩评论