Skip to content Skip to sidebar Skip to footer

Jquery Hover Not Triggered When Element Is Programatically Moved Under The Mouse

I have an image with a hover effect (higher opacity when mouse is over it). It works as desired when the mouse moves in and out. However, the image itself is moving (I'm periodical

Solution 1:

You won't be able to trigger mouse events if the mouse isn't moving, though you will be able to check where the mouse is when the image is moving. What you need to do is track the mouse position in a global variable, and check to see if that position is inside your image when it moves.

jQuery has a nice article about how to do it using their library: http://docs.jquery.com/Tutorials:Mouse_Position

To find the position of your image you can use the jQuery position function: http://api.jquery.com/position/

With that position you can create a bounds using the height/width of your image. On your image move check to see if that global mouse position is inside your image bounds and you should be good to go.

This is how I would write the code(completely untested btw):

var mousex = 0;
var mousey = 0;

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
      mousex = e.pageX;
      mousey = e.pageY;
   }); 
})

img.move(function(){
  ...move code...
  var p = $(this).position();
  if(mousex >= p.left && mousex <= p.left + $(this).width
     && mousey <= p.top && mousey >= p.top + $(this).height)
  {
   ...opacity code...
  }
});

Solution 2:

You could manually test to see if the mouse is in the image when you move the image then fire the desired event.

Mouse position using jQuery outside of events will show you how to keep track of the mouse position. Then just find the offset of the image and see if it's inside the image.

Solution 3:

In addition to wajiw's and ryan's answers, you should trigger the mouseenter and mouseleave events as you detect that the mouse is over/not over the image, so that whatever code you bound to .hover() is still executed:

$(".my-image").trigger("mouseenter");
$(".my-image").trigger("mouseleave");

Solution 4:

@wajiw has posted a great solution, but unfortunately it's plagued with typos meaning it won't work out of the box until you fix it.

Here is a class you can use which is tested and works which will allow you to test if an object is under the mouse.

Class definition

// keeps track of recent mouse position and provides functionality to check if mouse is over an object// useful for when nodes appear underneath the mouse without mouse movement and we need to trigger hover// see http://stackoverflow.com/questions/4403518functionMouseTracker($) {
  var mouseX, mouseY;

  $(document).mousemove(function(e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  });

  return {
    isOver: function(node) {
      var p = $(node).offset();
      if (mouseX >= p.left && mouseX <= p.left + $(node).width()
         && mouseY >= p.top && mouseY <= p.top + $(node).height())
      {
        returntrue;
      }
      returnfalse;
    }
  }
}

Usage example

var mouseTracker = newMouseTracker(jQuery);
if (mouseTracker.isOver($('#my-object-in-question'))) {
  $('#my-object-in-question').trigger("mouseenter");
}

Hope that helps.

I could make this into a jQuery plugin very easily if anyone wants it, just drop me a line and I'll go ahead.

Matt

Post a Comment for "Jquery Hover Not Triggered When Element Is Programatically Moved Under The Mouse"