Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move active element loses mouseout event in Internet Explorer

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 mouse out).

The library I am using has neat solution which uses appendChildren on the active element to move it to the end its parent so further towards the end of the dom and in turn on top.

The problem is I believe that because the element you are moving is the one you are hovering over the mouseout event is lost. Your mouse is still over the node but the mouseout event isn't being fired.

I have stripped the functionality down to confirm the issue. It works fine in Firefox but not in any version of IE. I'm using jQuery here for speed. Solutions can be in plain old Javascript, which would be a preference as it might need to go back up stream.

I can't use z-index here as the elements are vml, the library is Raphael and I am using the toFront call. Sample using ul/li to show issue in a simple example

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="js/jquery.min.js" type="text/javascript"></script>
<style>
    li
    {
        border:1px solid black;
    }
</style>
</head>
<body>
<ul><li>Test 1</li></ul>
<ul><li>Test 2</li></ul>
<ul><li>Test 3</li></ul>
<ul><li>Test 4</li></ul>
<script>
$(function(){
    $("li").mouseover(function(){
        $(this).css("border-color","red");
        this.parentNode.appendChild(this);
    });

    $("li").mouseout(function(){
        $(this).css("border-color","black");
    });
});
</script>
</body>
</html>

Edit: Here is a link to a js paste bin to see it in action. http://jsbin.com/obesa4

**Edit 2: ** See all comments on all answers before posting as lots more info in that.

like image 516
johnwards Avatar asked Sep 10 '10 15:09

johnwards


People also ask

Which is an event that happens when the user moves the mouse away from an element in Javascript?

The onmouseleave event occurs when the mouse pointer is moved out of an element.

Is an event that happens when the user moves the mouse away from an element Onmouseout?

The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children. Tip: This event is often used together with the onmouseover event, which occurs when the pointer is moved onto an element, or onto one of its children.

Which event handler will be triggered by the mouse being moved off an element?

If you depress the mouse button on an element and move your mouse off the element, and then release the mouse button. The only mousedown event fires on the element. Likewise, if you depress the mouse button, and move the mouse over the element, and release the mouse button, the only mouseup event fires on the element.

Which event occurs when the mouse pointer is moved onto an element or onto one of its children?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children.


1 Answers

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)
like image 188
25 revs, 4 users 83% Avatar answered Oct 14 '22 22:10

25 revs, 4 users 83%