Solved

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

Posted on 2006-07-10
6
235 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now