开发者

Detect if mouse is over an element when page loads with Javascript

开发者 https://www.devze.com 2022-12-08 06:13 出处:网络
I have an image that I want to have trigger certain behaviors when the mouse is over, I have a mouseover and mouseout method, but if you happen to have your mouse over the image wh开发者_开发问答en th

I have an image that I want to have trigger certain behaviors when the mouse is over, I have a mouseover and mouseout method, but if you happen to have your mouse over the image wh开发者_开发问答en the page loads, the mouseover method never fires until you leave the image and come back over it.

Is there a way to detect if the mouse is over an element on the fly without the mouse having to be off of the element and then come over the element to trigger the JS mouseover event? Like is there a document.getElementById("blah").mouseIsOver() type function in Javascript?


I believe this is possible without any action from the user. When your page loads, bind the mouseover event to your image and hide your image (i.e. using CSS display:none). Use setTimeout() to show it again in a few milliseconds (10 should be enough). The even should be fired.

If you don't want to cause the 'flick' effect on your image, you may try using some temporary element instead, attaching event to it, and delegating the event onto your image.

I have no idea if this is cross-browser solution, but it worked from my Firefox 3.0 console ;)


You could use the mousemove event. That would trigger anytime the user moves a mouse; so the only instance of the trigger not firing would be if the user does not move the mouse at all, which should be rare.

The only problem with this is that the event would fire anytime the mouse would move over your image, so you would get a LOT of those events while over the component. What you would probably need to do is implement some sort of flag within your method when the event fires. You turn on the flag when the event first fires, and you turn it off when you leave the component.

This is less than ideal, but I think this will probably satisfy your problem scenario. The following is some quick pseudo code on what that solution might look like, I think it should work.

<img src="blah.png" onmousemove="JavaScript:triggerOn(event)" onmouseout="JavaScript:triggerOff(event)"/>


...

<script type='text/javascript'>

var TriggerActive = false;

function triggerOn(e){
  e = e||window.e;
  if( !TriggerActive){
    TriggerActive = true;
    // Do something
  } else {
    // Trigger already fired, ignore this event.
  }
}

function triggerOff(e){
  e = e||window.e;
  if(TriggerActive) 
    TriggerActive = false;
}

</script>

You can find some great mouse event information including browser compatibility notes here.


Use document.querySelectpor and onload/onready events.

var a = document.querySelector('#a:hover');
if (a) {
   // Mouse cursor is above a
}
else {
   // Mouse cursor is outside a
}


There is no way to get the mouse coordinates aside from listening for mouse events, namely mousemove, mouseover etc. However, these events are very sensitive in the sense that moving the cursor by just one pixel is enough to trigger them, so having the cursor hover over your image while perfectly still should be somewhat unusual.

0

精彩评论

暂无评论...
验证码 换一张
取 消