Menú Desplegable

{ Menú Desplegable }

Tengo una extraña afición por los menú's que se colapsan y todo ese asunto aunque muchos piensan que pueden llegar a ser inútiles a mi me agradan bastante, resulta que varias veces han surgido este tipo de dudas y explicarlo en ese momento resulta un poquito cansado, además que la forma no se me hacía la más adecuada para hacerlo, después de leer un artículo que ví sobre la DivManía, les dejo este tutorial que después de un buen rato de intentarle por fin salió como quería.

 

image

{ Javascript }

Para empezar copiamos este código y lo nombramos collapse.js (Sorry por no poner los derechos, no sé quién sea el autor, pero si alguien sabe que me diga, por que está muy bueno)

 

  1. function initit(list, img, stylesheetpath)
  2. {
  3.       var show = GetCookie(list);
  4. var listElementStyle=document.getElementById(list).style;
  5.       if (show == "true"){
  6.                    
  7.                    listElementStyle.display="block";
  8.       }
  9.       else{
  10.  
  11.                    listElementStyle.display="none";
  12.       }
  13.  
  14. }
  15.  
  16. function switchit(list,img,stylesheetpath){
  17.        var listElementStyle=document.getElementById(list).style;
  18.                 if (listElementStyle.display=="none"){
  19.                    
  20.                    listElementStyle.display="block";
  21.                    SetCookie(list,"true");
  22.                 }
  23.                 else {
  24.                  
  25.                    listElementStyle.display="none";
  26.                    SetCookie(list,"false");
  27.                  }
  28. }
  29.  
  30. function SetCookie(name, value, expires, path, domain)
  31. { document.cookie = name + "=" + escape(value) +
  32.   ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  33.   ((path == null)    ? "" : "; path=" + path) +
  34.   ((domain == null)  ? "" : "; domain=" + domain);
  35. }
  36.  
  37. function GetCookie(name)
  38. { var cname = name + "=";               
  39.   var dc = document.cookie;             
  40.   if (dc.length> 0)
  41.   { begin = dc.indexOf(cname);       
  42.     if (begin != -1)
  43.     { begin += cname.length;       
  44.       end = dc.indexOf(";", begin);
  45.       if (end == -1) end = dc.length;
  46.       return unescape(dc.substring(begin, end));
  47.     }
  48.   }
  49.   return null;
  50. }
  51.  
  52. function DelCookie (name,path,domain)
  53. { if (getCookie(name))
  54.   { document.cookie = name + "=" +
  55.     ((path == null) ? "" : "; path=" + path) +
  56.     ((domain == null) ? "" : "; domain=" + domain) +
  57.     "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  58.   }
  59. }

{ Nuestro HTML}

Ahora, lo mandamos llamar en el header de nuestra página

 

HTML:

  1. <script type="text/javascript" language="javascript" src="collapse.js"></script>

 

Una vez hecho esto, vamos a crear nuestra lista que en realidad será nuestro menú, ya en el body de nuestro sitio ponemos el siguiente código:

 

HTML:

  1. <div id="navigation">
  2.   <h1>InfectedFx.net</h1>
  3.   <ul class="mainMenu">
  4.     <li><a href="javascript:switchit('navOne','collapse1','')">Category 01</a>
  5.       <ul class="subMenu" id="navOne">
  6.         <li><a href="#">SubCategory 01-1</a></li>
  7.         <li><a href="#">SubCategory 01-2</a></li>
  8.         <li><a href="#">SubCategory 01-3</a></li>
  9.         <li><a href="#">SubCategory 01-4</a></li>
  10.       </ul>
  11.       <script type="text/javascript">initit('navOne','','');</script>
  12.     </li>
  13.     <li><a href="javascript:switchit('navTwo','collapse2','')">Category 02</a>
  14.       <ul class="subMenu" id="navTwo">
  15.         <li><a href="#">SubCategory 02-1</a></li>
  16.         <li><a href="#">SubCategory 02-2</a></li>
  17.         <li><a href="#">SubCategory 02-3</a></li>
  18.         <li><a href="#">SubCategory 02-4</a></li>
  19.       </ul>
  20.       <script type="text/javascript">initit('navTwo','','');</script>
  21.     </li>
  22.     <li><a href="#">Category 03</a></li>
  23.     <li><a href="#">Category 04</a></li>
  24.   </ul>
  25. </div>

 

navigation: Nuestro contenedor
mainMenu: Definición de la lista que contiene todo el menú
subMenu: Es la definición de quienes serán "hijos"
navOne: Es el ID asignado a cada elemeno que se va a expandir (tienen que ser distintos) si se dan cuenta dentro del javascript y el ID del subMenu es exactamente la misma, si no es así entonces no hará nada, de la misma forma donde está el javascript con la función init

{ Cascading Style Sheets }

Para terminar con este tutorial, solo debemos de crearle el diseño a nuestro menú, basándonos en el estilo tutorial anterior pondré este ejemplo, ya ustedes le dán el formato que gusten.

 

CSS:

  1. /* Contenedor de nuestro menú*/
  2. #navigation{
  3.     width: 195px;
  4.     margin:auto;
  5.     border:4px solid #444;
  6.     font-family:Arial, Helvetica, sans-serif;
  7.     font-size:10px;
  8.     font-weight:bold;
  9. }
  10.  
  11. #navigation h1{
  12.     width:195px;
  13.     height:53px;
  14.     text-indent:-5000px;
  15.     font-size:12px;
  16.     background: #FFF url("pix/topMnu.jpg") no-repeat top left;
  17.     border-bottom:5px solid #C73B0A;
  18.     margin:0px;
  19.     padding:0px;
  20. }
  21.  
  22.  
  23. #navigation ul {
  24.     padding:0px;
  25.     margin:0px;
  26.     list-style-type: none;
  27.     font-size:10px;
  28.    
  29. }
  30.  
  31. /* Menu Principal*/
  32.  
  33. .mainMenu a{
  34.     display: block;
  35.     padding: 5px 0px 5px 25px;
  36.     border-bottom:1px solid #E3E3E3;
  37.     border-top:1px solid #FFF;
  38.     width:170px;
  39.     text-decoration: none;
  40.     font-weight:normal;
  41. }
  42.  
  43. .mainMenu a:link, .mainMenu a:visited{
  44.     color: #3399CC;
  45.     background: #F9F9F9 url("pix/post.gif") no-repeat left;
  46. }
  47.  
  48. .mainMenu a:hover{
  49.     color: #C73B0A;
  50.     background: #F7F7F7 url("pix/post_o.gif") no-repeat left;
  51. }
  52.  
  53.  
  54. /* SubMenu*/
  55.  
  56. .subMenu a:link, .subMenu a:visited{
  57.     color: #999999;
  58.     background: #FFFFFF url("pix/sub.gif") no-repeat left;
  59. }
  60.  
  61. .subMenu a:hover{
  62.     color: #666666;
  63.     background: #FFFFFF url("pix/sub_o.gif") no-repeat left;
  64. }

{ El ejemplo }

Si pueden comentar ó escribir que les pareció el tutorial, que más les gustaría ver, que temas les gustarían que tocáramos en ifx estaría genial.

Saludos!

Déjanos tu comentario, es importante para nosotros.