SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    onmouseout being called in ff when i don't think it should be

    what's going on here? in the following code the popup div is set to dissapear onmouseout of the div. in firefox (2 mac) that happens but also it happens within the popup div -- the blue area shows where the "hole" is:



    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	<title>Untitled</title>
    	<style type="text/css">
    	body { font-size:120%; font-family:sans-serif; line-height:1.5; margin:3em; }
    	#roll { border:2px dashed #aaa; padding:.1em .3em; white-space:nowrap; }
    	#popup { border:1px solid #999; position:absolute; width:10em; padding:1em; background-color:#fff; }
    	</style>
    	<script type="text/javascript">//<![CDATA[
    function Menu() {
    	this.displaying = false;
    	this.popup = document.createElement("div");
    	this.popup.setAttribute("id", "popup");
    	this.popup.setAttribute("onmouseout", "popup.hide();");
    	this.popup.innerHTML = "<ul><li>one<\/li><li>two<\/li><\/ul>";
    }
    Menu.prototype.display = function() {
    	if( this.displaying == false ) {
    		var roll = document.getElementById("roll");
    		this.popup.style.top = getY(roll) + "px";
    		this.popup.style.left = getX(roll) + "px";
    		document.body.appendChild(this.popup);
    	}
    }
    Menu.prototype.hide = function() {
    	this.displaying = false;
    	this.popup.parentNode.removeChild(this.popup);
    }
    
    var popup = new Menu();
    
    function getX(e) {
    	var x = 0;
    	while(e) {
    		x += e.offsetLeft;
    		e = e.offsetParent;
    	}
    	return x;
    }
    function getY(e) {
    	var x = 0;
    	while(e) {
    		x += e.offsetTop;
    		e = e.offsetParent;
    	}
    	return x;
    }
    
    	//]]>
    	</script>
    </head>
    <body>
    
    <p>
    This is my test text. This is my test text. This is my test text.
    This is my test text. This is my test text. This is my test text.
    This is my <span id="roll" onmouseover="popup.display();">test rollover menu</span>.
    This is my test text. This is my test text. This is my test text.
    This is my test text. This is my test text. This is my test text.
    This is my test text. This is my test text. This is my test text.
    </p>
    
    </body>
    </html>
    why is that "hole" there? bug? something i've dont wrong?

    thanks.

    ps, looks like possibly the ul is making the "hole"? it's also happening in safari so i guess it's not a ff bug.

    pps, yup, that looks correct (the ul making the "hole"). i've just put a bakcground colour onto the ul and it is that exact area which is the hole (my blue drawn box comes down much too low). so how to stop the ul making a hole? thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    this.popup.setAttribute("onmouseout", "popup.hide();");

    is not going to work properly as you can't set event handlers that way.

    Try coding it like this instead:

    this.popup.onmouseout = popup.hide;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks, but having done that i now get an error for that new line as soon as the page loads in firefox:

    Error: popup has no properties.

    and the popup doesn't work at all. is that because popup.hide isn't defined until after that line. i wasn't getting an error before btw. i don't know how reliable or correct firefox's javascript errors are.

    any ideas? thanks.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,681
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The following takes you through why the onmouseout event is firing when you don't want it to, and what can be done about it.
    http://www.quirksmode.org/js/events_mouse.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    right thanks. i'm still having trouble understanding what to do but possibly getting there. having read various bits i think i may have an idea of what the basic strategy for dealing with this is. here it is and it'd be great if anyone could say if this is the right line or not:

    (before that just to restate the problem: mouseout handler set on a parent div gets called when mouse rolls over a child element of the div element, and i don't want that because the child element is in the div)

    in the div's mouseout handler function receive and process the event object; get the info which says which element the mouse has gone into (using relatedTarget and the IE versions). if it's one of the children of the div return from mouseout handler function immediately without doing what it does.

    correct? thanks.

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i've tried this:
    Code:
    <head>
       <style type="text/css">
       #thediv { padding:1em; position:absolute; background:yellow; font-size:2em; top:200px; left:200px; }
       p { background:red; }
       </style>
    </head>
    <body>
    <div id="thediv">
       <p id="the-p">
          paragraph of text
       </p>
    </div>
    <script type="text/javascript">
    function handleMouseOut(e) {
       if( e.relatedTarget == document.getElementById("the-p") ||
          e.relatedTarget == document.getElementById("the-div") ) return;
       alert("mouse out of div area");   
    }
    var mydiv = document.getElementById("thediv");
    mydiv.addEventListener("mouseout", handleMouseOut, true);//false make no difference there
    </script>
    </body>
    </html>
    the alert line in handleMouseOut still gets called when rolling over between div area and child paragraph area.

    could anyone please tell me how to have an entire div not cause a mouseout until the mouse really does go out of the div area?

    surely this is a very basic issue to anyone who knows js? i've only been learning it for a couple of weeks myself, which is why i'm struggling with it. any help much appreciated. thanks.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,681
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The answer is on the previously referenced page, in the section titled "Mousing out of a layer"
    http://www.quirksmode.org/js/events_mouse.html

    Get the target element and carry on only if it's the div you want
    Get the related target, and carry on only if it's outside the div

    Here's how your function should be coded.

    Code javascript:
    function handleMouseOut(evt) {
    	evt = evt || window.event
    	var targ = evt.target || evt.srcElement;
    	if (targ.nodeName !== 'DIV' || targ.id !== 'thediv') {
    		return;
    	}
    	var relTarg = evt.relatedTarget || evt.toElement;
    	while (relTarg !== targ && relTarg.nodeName !== 'HTML') {
    		relTarg = relTarg.parentNode;
    	}
    	if (relTarg === targ) {
    		return;
    	}
       alert("mouse out of div area");   
    }
    Last edited by paul_wilkins; Aug 12, 2008 at 14:49.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ah right, i see. thanks very much for that. was getting in quite a muddle.

    thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •