分享一下第一次做WP模板的下拉菜单代码

时间:2007-11-30 10:35:22      类别:Web技术|WEB Tech, Wordpress      本文链接:生活点滴Enjoy Life

首先看看下面的例子
可能有很多朋友和我以前的爱好一样,喜欢悬停的下拉菜单,所以这里我就将这个代码分享出来。代码分三个部分:1.wordpress模板代码;2.css代码;3.javascript代码。

wordpress模板代码

  1. <div id="nav_a">
  2.    <ul id="navsite">
  3.      < ?php
  4.         $mycat = $cat;//通过全局变量获取当前cat id
  5.         $cat = get_category_parentID($cat);//获取父类ID,以保证在每页都显示全部分类
  6.         wp_list_cats('use_desc_for_title=1&sort_column=id&sort_order=desc&children=1');
  7.     ?>
  8.   </ul>
  9. </div>
通过上面的代码,便可以将两级分类获取出来。

css代码根据个人模板不同的风格,你可能要对某些参数进行修改,下面是代码,解说将在注释中。

  1. ul#navsite,
  2. #navsite ul {
  3.     font: 12px Verdana, sans-serif;
  4.     padding: 0;
  5.     margin: 0;
  6.     list-style: none;
  7.     }
  8. #navsite li {
  9.     float: left;  /* 将li标签水平排列 */
  10.     text-align: center;
  11.     }
  12.  
  13. #navsite li ul {
  14.     position: absolute;
  15.     top: auto;
  16.     left: -999em; /*隐藏 */
  17.     }
  18. * > html #navsite li ul {  /* "Tan Hack" for IE Mac only */
  19.     display: none;
  20.     left: auto;
  21.     }
  22. #navsite li:hover ul,
  23. * html #navsite li.sfhover ul {  /* for IE-Win and other non-IE browsers */
  24.     left: auto;
  25.     }
  26. * > html #navsite li.sfhover ul { /* "Tan Hack" for IE Mac only */
  27.     display: block;  /* IE-Mac gets confused by the left:-999em rule, so we do this. */
  28.     border: 2px solid #778;  /* to visually confirm this rule is being used on IE-Mac */
  29.     }
  30. #navsite li ul li {
  31.     float: none;  /* sub-menu items will stack vertically as normal list */
  32.     text-align: left;
  33.     }
  34. #nav_a ul li  A {
  35. display:block;
  36. padding-top:4px !important;
  37. padding-top:8px;
  38. WIDTH: 130px;
  39. height:21px !important;
  40. height:21px;
  41. color:#ffffff;
  42. BACKGROUND:  red ;/*主菜单色调*/
  43. }
  44.  
  45. #nav_a ul li.current-cat  A ,#nav_a ul li.current-cat-parent  A {
  46. BACKGROUND: blue;
  47. color:#000;
  48. }
  49. #nav_a ul li ul.children {
  50. padding:0 0 5px 0;
  51. width:130px !important;
  52. width:127px;
  53. BACKGROUND: url(images/ulbt.gif) #ffffff no-repeat bottom left;
  54.  
  55. }
  56. #nav_a ul li ul.children li  A {
  57. BACKGROUND: blue;/*二级菜单背景颜色*/
  58. color:#fff;
  59. }
  60. #nav_a ul li ul.children li  A:hover {
  61. BACKGROUND: red;/*二级菜单鼠标悬停背景颜色*/
  62. color:#fff;
  63. }
  64.  
  65. av_a ul li ul.children li.current-cat  A ,#nav_a ul li ul.children li.current-cat-parent  A {
  66. BACKGROUND: green;
  67. color:#000;
  68. font-weight:bold;
  69. }
  70. #nav_a ul li ul li  A {
  71. display:block;
  72. padding-top:4px;
  73. width:130px;
  74. border:0;
  75. border-top:1px solid #999900;
  76. text-indent:4px;
  77. color:#ffffff;
  78. height:25px;
  79. BACKGROUND: url() #99cc00 no-repeat 0px 0px;
  80. }

javascript代码由于IE不兼容某些css描述,因此这里要用到javascript来弥补之不足,代码如下:

  1. <script LANGUAGE="JavaScript">
  2. <!--
  3.         sfHover = function() {
  4.     var sfEls = document.getElementById("navsite").getElementsByTagName("li");
  5.     for (var i=0; i<sfEls.length; i++) {
  6.       sfEls[i].onmouseover=function() {
  7.         this.className+=" sfhover";
  8.       }
  9.       sfEls[i].onmouseout=function() {
  10.         this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
  11.       }
  12.     }
  13.   }
  14.   if (window.attachEvent) window.attachEvent("onload", sfHover);
  15.   else { //IE 5.2 Mac does not support attachEvent
  16.     var old = window.onload;
  17.     window.onload = function() { if (old) old(); sfHover(); }
  18.   }
  19. //-->
  20. </script>

建议需要使用的兄弟直接在实例中进行修改:menu.html

鸣谢:部分css代码借鉴了先辈的代码,而javascript则是直接copy过来使用的,由于很久以前使用的,所以忘记css部分代码和js全部代码的原创者是谁了.

标签:, ,
发表于 2007-11-30 10:35:22 目录:Web技术|WEB Tech, Wordpress [RSS 2.0] 你可以发表评论, 或者从您的网站 trackback
如果您喜欢本blog,欢迎你的feed订阅,谢谢你的支持

前2排已经被占据了 快抢好位置哦

  • 1楼 非常秀 在 2008.01.03 13:17发表评论如下:

    不错,学习学习

  • 2楼 Ian 在 2008.04.23 12:01发表评论如下:

    您好,我想在我把我的网页上Page显示成dropdown menu形式的。但是我不知道怎么把上面的code复制过去。请问能不能赐教下? ^^

  • 转到第
(Required)
(Required, not published)
如果留言未显示请不要重复留言,我将为你恢复!


生活点滴Enjoy Life is proudly powered by WordPress | admin| About Us | cosbeta| Bluehost| site map 0.500s & 36