+ Reply to Thread
Results 1 to 3 of 3

Thread: Javascript mouseoffset problem - Box that is offset stays in the same place

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Javascript mouseoffset problem - Box that is offset stays in the same place

    I am making a hover-offset menu and I got it mostly working. However, when you hover over the roots (links for each menu) the menu pops up, but unforutnately, the same place which is fine for root1, but when you get to root3 you can't get to the menu. I think the coordinates I get are of the parent element of the roots and displays it in the same place for all of them. Please help.

    HTML:
    HTML Code:
    <!-- ... -->
    <ol>
        <li>
            <a href="javascript:void(0)">Root 1</a>
                <ol>
                    <li>
                        <a href="javascript:void(0)">Root 1 Child 1</a>
                        <a href="javascript:void(0)">Root 1 Child 2</a>
                    </li>
                 </ol>
        </li>
        <li>
            <a href="javascript:void(0)">Root 2</a>
                <ol>
                    <li>
                        <a href="javascript:void(0)">Root 2 Child 1</a>
                        <a href="javascript:void(0)">Root 2 Child 2</a>
                     </li>
                 </ol>
        </li>
        <li>
            <a href="javascript:void(0)">Root 3</a>
                <ol>
                    <li>
                        <a href="javascript:void(0)">Root 3 Child 1</a>
                        <a href="javascript:void(0)">Root 3 Child 2</a>
                     </li>
                 </ol>
        </li>
    </ol>
    Javascript:
    Code:
    $(document).ready(function() {
        $("#hoverEvent li").find("ol").css({'display' : 'none', 'position' : 'absolute', 'width' : '100px'});
        $("#hoverEvent li").hover(
            function() {
                mouseCoords;
                $(this).find("ol").css({'display' : 'block'});
                $(this).find("ol").offset({'top' : posy, 'left' : posx});
            },
            function() {
                $(this).find("ol").css({'display' : 'none'});
            });
    });
    
    // Get the coordinates of the mouse
    function mouseCoords(e) {
            var
                posy =0,
                posx =0;
                
            if(!e) {var e = window.event;}
            if(e.pageX || e.pageY) {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if(e.clientX || e.clientY) {
                posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            }
            
            else {
                document.getElementById("help").innerHTML = "You cannot run this";
            }
            
    
        return posx,posy;
    }
    Here is a link to a live site.

    Also, a side note: I noticed that Google Chrome 5 doesn't like the li display:inline; as it always displays as a block list. Is there any fix for that?
    Last edited by as4s1n; 06-15-2010 at 02:36 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Javascript mouseoffset problem - Box that is offset stays in the same place

    Code:
    $(document).ready(function() {
        $("#hoverEvent li").find("ol").css({'display' : 'none', 'position' : 'absolute', 'width' : '100px'});
        $("#hoverEvent li").hover(
            function() {
                ????? = mouseCoords() ;
                $(this).find("ol").css({'display' : 'block'});
                $(this).find("ol").offset({'top' : posy, 'left' : posx});
            },
            function() {
                $(this).find("ol").css({'display' : 'none'});
            });
    });
    Misprint or what? You never set posx and posy in that function.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript mouseoffset problem - Box that is offset stays in the same place

    Quote Originally Posted by as4s1n View Post
    Code:
    $(document).ready(function() {
        $("#hoverEvent li").find("ol").css({'display' : 'none', 'position' : 'absolute', 'width' : '100px'});
        $("#hoverEvent li").hover(
            function() {
                mouseCoords;
    You're not calling mouseCoords, you're simply referring to it. An interactive debugger would have revealed this in short order. If code isn't doing what you expect, your first step should be to use a debugger.

    Quote Originally Posted by as4s1n View Post
    Code:
                $(this).find("ol").css({'display' : 'block'});
                $(this).find("ol").offset({'top' : posy, 'left' : posx});
    posx and posy aren't defined here.

    Quote Originally Posted by as4s1n View Post
    Code:
    // Get the coordinates of the mouse
    function mouseCoords(e) {
            [...]
            if(!e) {var e = window.event;}
    There's no need to declare e as a variable. Since it's a parameter, it's already local.

    Quote Originally Posted by as4s1n View Post
    Code:
        return posx,posy;
    }
    You can't return multiple values this way in JS. You either have to use an array or (better in this case) an object:

    Code:
    function mouseCoords(e) {
        var pos = {x: 0, y: 0};
        ...
        return pos;
    }
    However, none of that matters. You don't need mouseCoords(). jQuery provides a consistent interface for mouse position. Remember, one of the two main reasons for using a JS library is to write browser agnostic code. If you find yourself coding for specific browsers, you're probably missing something.

    Another option (one that doesn't require getting the mouse position) is to give the <li> relative positioning and set the <ol>'s position to {top: 1em; left: 0;}, placing them below the #hoverEvent li a. Then all the #hoverEvent li hover handlers need to do is show and hide the <ol>.

    Quote Originally Posted by as4s1n View Post
    Also, a side note: I noticed that Google Chrome 5 doesn't like the li display:inline; as it always displays as a block list. Is there any fix for that?
    Safari is the same way. It's not the inline display (the <li> actualy are inline, believe it or not), it's because the #hoverEvent element isn't wide enough. Try giving #hoverEvent a width, or try floating the <li>.
    Last edited by misson; 06-15-2010 at 06:33 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Javascript problem
    By didiya in forum Free Hosting
    Replies: 2
    Last Post: 07-23-2009, 03:51 PM
  2. Cannot use string offset as an array in...
    By darylcarpo in forum Programming Help
    Replies: 2
    Last Post: 10-28-2008, 11:03 AM
  3. Problem JavaScript
    By willemvo in forum Programming Help
    Replies: 1
    Last Post: 08-25-2008, 08:44 AM
  4. Problem JavaScript
    By willemvo in forum Programming Help
    Replies: 5
    Last Post: 08-16-2008, 04:11 PM
  5. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers