Solved

Javascript for CSS driven menu needs a delay before the items disappear

Posted on 2006-07-10
6
241 Views
Last Modified: 2013-11-19
I am using a compact javascript (written by others and widely available) to build a dynamic menu based mostly on CSS. The script changes the CSS class on unordered list items.  Everything works fine, except that on sub-menus that fly out to the right, I need a slight time delay, so the menu doesn't disappear before the user can get to it.

Here's the base code:

function menuFix() {
      
/*--------------fix horizontal menu--------------------------*/
      var sfEls = document.getElementById("hNav").getElementsByTagName("li");
      for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
            this.className+=(this.className.length>0? " ": "") + "sfhover";
            }
            
            sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            }
            
      }
      
}

I've tried adding a setTimeout in front of the onmouseout function, like: sfEls[i].onmouseout=setTimeout("function()",1000) {

That doesn't work at all.

I've tried adding a setTimeout on the next line, like:
setTimeout('this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "")',1000);

When added here the menus never disappear.

Is it possible to add a delay to this javascript code, or will I have to scrap this approach and use an entire different structure?  Any help will be greatly appreciated.

Teri
0
Comment
Question by:thoyer
  • 2
6 Comments
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 250 total points
ID: 17104161
No the problem is simply in the - setTimeout() call.  It is a very conceptually complex call that must in fact call itself.  I struggled with this for a while, until I finally got it right.  Lets see if some links can help you understand the "unobviousness" of this javascript timer loop (it is hard to "get it" at first) --

www.pageresource.com/jscript/jtimeout.htm
http://www.codingforums.com/archive/index.php?t-65386.html
(see in this second one how the timeout is calling a FUNCTION, it is not running the function alone?)
www.the-cool-place.co.uk/javascript/tutorial/javascript2.html
http://www.pageresource.com/jscript/jtimeout.htm

Same with this last one, see how the setTimeout is actually calling the function that you are timing?

Hope that helps.  This is a case where LEARNING this complex function is better than an easy answer!!

0
 
LVL 9

Assisted Solution

by:jmanGJHS97
jmanGJHS97 earned 250 total points
ID: 17104911
In case that doesn't work for you, there's several cool menu scripts on dynamicdrive.com.  I happen to like this one:

http://www.dynamicdrive.com/dynamicindex1/jimmenu/index.htm

I totally agree with the idea of figuring out what's going on with your menu.  However, if you need a fresh look, there are tons of examples that contain working code, which may help you think of ways to resolve this current issue are facing.

jmaGJHS97
0
 

Author Comment

by:thoyer
ID: 17112422
I read through everything in the tutorials you provided. I tried calling the function within the function, like illustrated.  But nothing works.  I think this is more complex than it's worth, and I'm going to redesign so the menu doesn't fly out, but instead drops down.  This will work fine.

But thanks for the effort.
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 17112703
Which is not a bad idea.  The site where I used a setTimeout() and spent a LOT of time on it, I am thinking of redesigning that site too.  So maybe animated stuff doesnt work as good as other options.  Sometimes you are better to pick what the user expects, it makes it easire for them.   Good luck....
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question