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

前端 未结 2 1053
刺人心
刺人心 2020-12-31 22:37

If I have something like this:

\"alt

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

2条回答
  •  太阳男子
    2020-12-31 23:02

    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 + '
    '); } 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'); } });

提交回复
热议问题