Problems with mouseout event

后端 未结 6 1388
忘了有多久
忘了有多久 2021-02-09 15:11

I\'m using JavaScript to hide an image and show some text thats hidden under it. But, when the text is shown if you scroll over it, it fires the mouseout event on the container,

6条回答
  •  北海茫月
    2021-02-09 15:25

    It sounds like what you really want is mouseenter/mouseleave (IE proprietary events, but easy to emulate):

    // Observe mouseEnterLeave on mouseover/mouseout
    var mouseEnterLeave = function(e) {
        var rel = e.relatedTarget, cur = e.currentTarget;
        if (rel && rel.nodeType == 3) {
            rel = rel.parentNode;
        }
        if(
            // Outside window
            rel == undefined ||
            // Firefox/other XUL app chrome
            (rel.tagName && rel.tagName.match(/^xul\:/i)) ||
            // Some external element
            (rel && rel != cur && rel.descendantOf && !rel.descendantOf(cur))
        ) {
            e.currentTarget.fire('mouse:' + this, e);
            return;
        }
    };
    $(yourDiv).observe('mouseover', mouseEnterLeave.bind('enter'));
    $(yourDiv).observe('mouseout', mouseEnterLeave.bind('leave'));
    
    // Use mouse:enter and mouse:leave for your events
    $(yourDiv).observe(!!Prototype.Browser.IE ? 'mouseenter' : 'mouse:enter', yourObserver);
    $(yourDiv).observe(!!Prototype.Browser.IE ? 'mouseleave' : 'mouse:leave', yourObserver);
    

    Alternatively, patch prototype.js and use mouseenter and mouseleave with confidence. Note that I've expanded the check for leaving the window or entering XUL chrome; this seemed to fix some edge cases in Firefox for me.

提交回复
热议问题