Search code examples
jqueryz-indexjquery-events

Is there a way to fire over events ignoring z-index?


If I have something like this:

alt text

Is there a way to fire the mouseover events on BOTH divs?

Edit: Sorry all .. I tried to simplify the problem so it was clear to understand and I forgot to mention I have more than 100 divs like that so probably those solutions don't work. I'm going to see if I can adapt them.


Solution

  • I put together a working example here with JSFiddle:

    http://jsfiddle.net/gfosco/CU5YT/

    It's similar to madeinstefanos answer, but specific to your example..

    var mouseX = 0;
    var mouseY = 0;
    var front = 0;
    var back = 0;
    
    function log(text) {
        $("#log").append(text + '<BR>');
    }
    
    function mouseWithin(selector) {
      var pos = $(selector).position();
      var top = pos.top;
      var left = pos.left;
      var height = $(selector).height();
      var width = $(selector).width();
    
      if (mouseX >= left && mouseY >= top && mouseX <= left + width 
                         && mouseY <= top + height) {
        return true;
      }
      return false;
    }
    
    $(document).bind('mousemove', function(e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
        if (front == 1 && !mouseWithin("#front")) {
                front = 0;
                log('Front Leave');
        }
        if (back == 1 && !mouseWithin("#back")) {
                back = 0;
                log('Back Leave');
        }    
        if (front === 0 && mouseWithin("#front")) {     
                front = 1;
                log('Front Hover');
        }
        if (back === 0 && mouseWithin("#back")) { 
                back = 1;
                log('Back Hover');
        }        
    
    });