Move active element loses mouseout event in Internet Explorer

后端 未结 4 885
小蘑菇
小蘑菇 2020-12-10 03:31

In a library I am using I have the task of moving an element to the front of the dom when it is hovered over. (I make it bigger so I need to see it, then shrink it back when

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 04:35

    The problem is that IE handles mouseover differently, because it behaves like mouseenter and mousemove combined on an element. In other browsers it's just mouseenter.

    So even after your mouse has entered the target element and you've changed it's look and reappended it to it's parent mouseover will still fire for every movement of the mouse, the element gets reappended again, which prevents other event handlers from being called.

    The solution is to emulate the correct mouseover behavior so that actions in onmouseover are executed only once.

    $("li").mouseover( function() {
        // make sure these actions are executed only once
        if ( this.style.borderColor != "red" ) {
            this.style.borderColor = "red";
            this.parentNode.appendChild(this);
        }
    });
    

    Examples

    1. Extented demo of yours
    2. Example demonstrating the mouseover difference between browsers (bonus: native javascript)

提交回复
热议问题