javascript - Drop down menu hiding menus -
i'm building drop down menu project i'm working on, don't working entirely should. want show sub-menus when hover root-level menus, , close again after short delay when menu or sub-menu not hovered anymore. of works; sub-menus showed when root-level items hovered , hidden when stop hovering root-level item. problem sub-menus hidden when move cursor root-level item sub-menu item other first , hover that. not good, appreciated.
i created jsfiddle code shows issue more clearly.
so, here's code:
menu.htm
<div id=m_wrapper> <ul id=menu> <li onmouseover="show_sub_menu('0')" onmouseout="start_timer()">item 1 <div id=s0 onmouseover="show_sub_menu('0')" onmouseout="start_timer()"> <a href=#>item 1.1</a> <a href=#>item 1.2</a> <a href=#>item 1.3</a> </div> </li> </ul> </div>
menu.css
#m_wrapper { position:relative; display:table; } #menu { position:relative; } #menu li { width:100px; position:relative; float:left; list-style-type:none; } #menu div { position:absolute; visibility:hidden; display:inherit; width:100%; z-index:30 } #menu div { position:relative; display:block; z-index:35; }
menu.js
var countdown = 300; var timer = null; var menu_item = null; window.show_sub_menu = function(cath) { if (menu_item) { menu_item.style.visibility = 'hidden'; //make sure show 1 menu @ time } menu_item = window.document.getelementbyid("s" + cath); menu_item.style.visibility = 'visible'; //show menu if (timer) { window.cleartimeout(timer); //reset timer, menu kept open timer = null; } }; window.start_timer = function() { timer = window.settimeout(close_sub_menu, countdown); }; window.close_sub_menu = function() { menu_item.style.visibility = 'hidden'; };
you don't have make complex.
ofcourse can same through javascript, see how easy, readable , simple through jquery.
see demo
just use following script
$('#menu li').hover( function(){ $(this).stop().animate({height: '100px'},1000,function(){}); $(this).find('div').show(600); }//gets called upon mousehover , function(){ $(this).stop().animate({height: '20px'},1000,function(){}); } //gets called upon mouseout ); //hover ends
and also, don't know why have written tonns of css. use these:
#menu { list-style:none; } #menu li { width:100px; border:1px solid #ccc; text-align:center; cursor:pointer; height:20px; overflow:hidden; } #menu li div { border:1px solid #ccc; } #s0 { height:auto; } #s0 { display:block; }
there's plenty can through it, selected dropdown item. selection through arrow key , not. jquery makes simple you.
Comments
Post a Comment