Solved

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

Posted on 2006-07-10
6
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

695 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