Popup menu problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron

    Popup menu problems

    I'm having problems getting my popup menu to work correctly, and was
    hoping someone here could point me in the right direction. I have the
    following script:

    <script>
    var activeMenu="";

    function getObject(id) {
    var object=eval("do cument.getEleme ntById('"+id+"' )");
    return object;
    }

    function PopMenu(M) {
    clearTimeout();
    HideActive();
    getObject(M).st yle.visibility= "visible";
    activeMenu=M;
    }

    function HideActive() {
    if (activeMenu !="") {
    getObject(activ eMenu).style.vi sibility="hidde n";
    activeMenu="";
    }
    }

    function menuTimeout() {
    setTimeout("Hid eActive()", 2000);
    }
    </script>

    which goes with the following html:

    <div id="menuhead" onMouseOver="Po pMenu('menu')">
    <img src="menuhead.g if" />
    </div>
    <div id="menu" onMouseOut="men uTimeout()">
    <a href="test1.htm ">Test 1</a>
    <a href="test2.htm ">Test 2</a>
    <a href="test3.htm ">Test 3</a>
    </div>


    The menu opens perfectly, but it closes 2 sec after I mouseOut() of
    'menuhead' instead of the actual 'menu'. Also, if I repeatly
    mouseOver() the 'menuhead', the timing gets thrown off, as if
    clearTimeout() is being called irregularly. Could someone with more
    experience give me a hand?

    Thanks,
    Aaron

  • Janwillem Borleffs

    #2
    Re: Popup menu problems

    Aaron wrote:[color=blue]
    > I'm having problems getting my popup menu to work correctly, and was
    > hoping someone here could point me in the right direction. I have the
    > following script:
    >[/color]

    Here's an alternative approach:

    <style type="text/css">
    #menu {
    position: absolute;
    clip: rect(auto auto 15px auto)
    }
    </style>
    ....
    <div id="menu"
    onmouseout="sty le.clip='rect(a uto auto 15px auto)'"
    onmouseover="st yle.clip='rect( auto auto auto auto)'">
    Show menu<br />
    <a href="test1.htm ">Test 1</a>
    <a href="test2.htm ">Test 2</a>
    <a href="test3.htm ">Test 3</a>
    </div>


    JW



    Comment

    • Aaron

      #3
      Re: Popup menu problems

      Here's an alternative approach:[color=blue]
      >
      > <style type="text/css">
      > #menu {
      > position: absolute;
      > clip: rect(auto auto 15px auto)
      > }
      > </style>
      > ...
      > <div id="menu"
      > onmouseout="sty le.clip='rect(a uto auto 15px auto)'"
      > onmouseover="st yle.clip='rect( auto auto auto auto)'">
      > Show menu<br />
      > <a href="test1.htm ">Test 1</a>
      > <a href="test2.htm ">Test 2</a>
      > <a href="test3.htm ">Test 3</a>
      > </div>
      >[/color]

      This works great...thanks so much, JW. Now, to get the setTimeout(),
      I've changed it to the following:

      <style type="text/css">
      #menu {position: absolute;
      clip: rect(auto auto 15px auto);
      }
      </style>


      <script type="text/javascript">
      function getObject(id) {
      var object=eval("do cument.getEleme ntById('"+id+"' )");
      return object;
      }
      function openMenu(id) {
      var name=getObject( id);
      name.style.clip ='rect(auto auto auto auto)';
      }
      function closeMenu(id) {
      var name=getObject( id);
      name.style.clip ='rect(auto auto 15px auto)';
      }
      function menuTimeout(id) {
      setTimeout("clo seMenu(id)", 2000);
      }
      </script>


      <div id="menu" onmouseout="men uTimeout('menu' )"
      onmouseover="op enMenu('menu')" >
      <p>Show menu</p>
      <ul>
      <li><a href="test1.htm ">Test 1</a></li>
      <li><a href="test2.htm ">Test 2</a></li>
      <li><a href="test3.htm ">Test 3</a></li>
      </ul>
      </div>



      Passing the id name to the menuTimeout() function doesn't seem to work
      at all. What am I doing wrong here?

      Thanks,
      Aaron


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Popup menu problems

        Aaron <agchandler@com cast.not> writes:
        [color=blue]
        > I'm having problems getting my popup menu to work correctly, and was
        > hoping someone here could point me in the right direction. I have the
        > following script:[/color]

        I'll try to give some suggestions that you can try.
        [color=blue]
        > <script>[/color]

        The "type" attribute is required in HTML.
        <script type="text/javascript">
        [color=blue]
        > var activeMenu="";
        >
        > function getObject(id) {
        > var object=eval("do cument.getEleme ntById('"+id+"' )");[/color]

        You *never* need to use eval for accessing elements by name. You
        probably never need to use eval at all, and shouldn't, since there are
        more efficient and less error prone alternatives. This function can
        just be written:

        return document.getEle mentById(id);
        [color=blue]
        > return object;
        > }
        >
        > function PopMenu(M) {
        > clearTimeout();[/color]

        The function "clearTimeo ut" takes an argument: the value returned by
        the call to setTimeout that you want to cancel.
        <URL:http://www.mozilla.org/docs/dom/domref/dom_window_ref8 .html#1016919>
        <URL:http://msdn.microsoft. com/workshop/author/dhtml/reference/methods/cleartimeout.as p>

        Make a global variable outside the function,
        var timeout_id;
        and call
        clearTimeout(ti meout_id);
        timeout_id = null;
        and set it again when you call setTimeout.
        [color=blue]
        > HideActive();
        > getObject(M).st yle.visibility= "visible";
        > activeMenu=M;
        > }
        >
        > function HideActive() {
        > if (activeMenu !="") {
        > getObject(activ eMenu).style.vi sibility="hidde n";
        > activeMenu="";
        > }
        > }
        >
        > function menuTimeout() {
        > setTimeout("Hid eActive()", 2000);[/color]

        first clear the existing timeout, if any:
        if (timeout_id != null) { clearTimeout(ti meout_id); }
        This avoids having two timeouts running at the same time,
        when you can only cancel one.

        and then start a new countdown:
        timeout_id = setTimeout("Hid eActive()",2000 ):
        (or even
        timeout_id = setTimeout(Hide Active, 2000);
        in modern browsers)
        [color=blue]
        > }
        > </script>
        >
        > which goes with the following html:
        >
        > <div id="menuhead" onMouseOver="Po pMenu('menu')">
        > <img src="menuhead.g if" />[/color]

        You should not write "/>" in HTML. It is only for XHTML, which you
        don't seem to use (attribute names are all lower case in XHTML).
        [color=blue]
        > The menu opens perfectly, but it closes 2 sec after I mouseOut() of
        > 'menuhead' instead of the actual 'menu'.[/color]

        Not for me. When I try your code in IE 6 (without the image :),
        the submenu starts visible. When I put the mouse over it and leaves
        again, the submenu disappears after two seconds (even if I am, at that
        point, over the menuhead ... most likely because clearTimeout fails to
        do anything).
        [color=blue]
        > Also, if I repeatly
        > mouseOver() the 'menuhead', the timing gets thrown off, as if
        > clearTimeout() is being called irregularly.[/color]

        clearTimeout, as you call it, does nothing, so every mouseout will
        schedule a hide, with the only limit to how many you can have pending
        being how fast you can twiddle your moust.

        Good luck.
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Popup menu problems

          Aaron <agchandler@com cast.not> writes:
          [color=blue]
          > <style type="text/css">
          > #menu {position: absolute;
          > clip: rect(auto auto 15px auto);[/color]

          I would recommend changing "15px" to "1.2em" (or whatever the line
          height is). To see why, try adding:
          font-size: large;
          That can happen to people with different default font settings too.
          [color=blue]
          > function menuTimeout(id) {
          > setTimeout("clo seMenu(id)", 2000);[/color]

          This won't work. The code
          closeMenu(id)
          will be executed in the global context, and will not have access
          to the local variables of the menuTimeout function, including "id".

          You can either use:
          setTimeout("clo seMenu('"+id+"' );", 2000);
          (i.e., build the id into the expression as a string literal) or
          setTimeout(func tion(){closeMen u(id);}, 2000);
          (use a closure to capture the value of "id").

          See my other reply for other suggestions.
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Aaron

            #6
            Re: Popup menu problems

            Thanks for help, Lasse. Works great now.

            Aaron

            Comment

            Working...