2012-04-20 86 views
3

这是我第一次在论坛发帖。在必要时指导我CSS菜单和jQuery

我试图设置基本网页编辑(使用Wordpress)的模板,并且菜单有很多麻烦。该网站是这样的:http://munchkingraphicdesign-uat.com/devsite/

我设法最终建立了一个CSS菜单(3级)似乎工作正常,但我没有控制浮动(子菜单总是垂直漂浮,无论我改变了我无法让他们水平漂浮)。在任何人问我为什么要这么做的时候 - 就是测试自己和代码,看看它是否设置正确。没有完全做到这一点。

然后我忽略了这一点,并按下看看我是否可以通过jQuery添加一些效果。我正确初始化了jQuery并添加了一些我认为会让我滑落的代码。

在Chrome中似乎没问题 - 但第三层不会滑动,因此我认为CSS会回落。在Internet Explorer 8中,当我移动到第二级下拉菜单并且第三级从零高度到满时上下跳动时,会出现闪烁。

我似乎甚至不能在我的本地站点上复制此内容。它根本不起作用。

任何一种灵魂都可以给我指点吗?也许我在走路之前就试图跑步,但是我担心我甚至没有通过CSS代码获得正确的基础知识。

道歉,但有一点注释掉的代码也正在努力学习,因为我去。那些_NULL后面有这个添加到让jquery工作,因为_NULL意味着html不再引用它们。

这段代码怎么会更好,我怎样才能得到良好的下拉使用jQuery三个级别?

非常感谢

下面是jQuery的,但CSS也是至关重要的,以得到正确的

<script type="text/javascript"> 
    jQuery(document).ready(function($) {/*normally loads in noconflickmode, so have to use this line to be able to use the $ sign*/ 
     ($("#menubar ul li").hover(function(){ 
      $(this).addClass("hover"); 
      $('ul:first',this).slideDown(400); 
     }, 
     function(){ 
      $(this).removeClass("hover"); 
      $('ul:first',this).slideUp(400); 
     }); 
     $("#menubar ul li ul li:has(ul)").find("a:first").append(" &raquo; "); 
    }); 
</script> 

所以CSS是

#menubar-outer { /*container box with no colour. Used to position inner div*/ 
    background-color: #D5CBA7; 
    position: relative;/*Needs to be explicitly set for z-index*/ 
    z-index: 50;/*Needed for IE7*/ 
    width: 100%; 
    margin: 0 0 0px 0;/*Use lower margin for lower line tab effect. Use a negative offset on left to align*/ 
    padding: 0; 
    text-align: left;/*CONTROLS overall position of menu. Right, left or center*/ 
    /*border-bottom: 1px solid #c0c0c0;*/ 
} 
#menubar { /*inner container box..Sets space and fonts*/ 
    /*background-color:blue;*/ 
    display: inline-block; 
    position: relative; 
    z-index: 50; 
    margin: 0 0 0px 0px;/*for space around the menu first row*/ 
    padding: 0; 
    font-size: 1em; 
    text-transform: uppercase; 
    vertical-align: bottom;/*needed to remove automatic inline-block additional 5px margin*/ /*border: 1px solid black;*/ 
} 
#menubar a {/*The element containing the words*/ 
    /*background-color: #dbdbdb; *//*color of main menu*/ 
    display: block; /*using block rather than inline makes entire block, not just the text clickable*/ 
    /*height: 15px;*//*CONTROLS height of menu item on first row together with "li li a" which controls for subsequent rows*/ 
    padding: 10px 15px; /*space around the menu items*/ 
    line-height: 15px;/*spacing between lines on same menu item*/ 
    text-align: left;/*CONTROLS centred menu items or left/right aligned. "a" element used rather than li which doesn't work in IE7. Affects drop downs so important*/ 
} 

#menubar li li a { /*Second and third level rows. */ 
    /*height: 30px;*/ /*Needed for styling different height on first row*/ 
    /*padding: 7px 6px;*//*Needed for styling different padding than first row*/ 
    /*background-color: #D5CBA7;*//*CONTROLS filled background color for dropdowns. Important because above active page highlights will override otherwise*/ 
} 
#menubar li:first-child { 
    border-left: none;/*accompanies divider&box line aboves. Use "none" if you are using seperators and add in e.g. "1px solid #c0c0c0"" if borders*/ 
} 
#menubar li { /* fixes dimensions of all level menu elements. Turns menu into horizontal*/ 
    float: left;/*makes the menu horizontal. Needed above in "a" for ie6 but causes compatibility probs*/ 
    position: relative;/*need this to ensure that the following div can be absolutely positioned*/ /*width: 130px;*/ /*CONTROLS first row menu item spacing. Width needed for li rather than "a" to work in ie7*/ 
    margin: 0 0px 0 0;/*THIS creates gaps between items*/ 
    /*border-top: 1px solid #c0c0c0; 
    border-right: 1px solid #c0c0c0; 
    border-bottom: 1px solid #c0c0c0;/*Use border top right and bottom only if you want surrounding boxes*/ 
    border-left: 2px solid #ffffff;/*Use by itself if you want dividers, together with above if seperated boxes or"none" for joined boxes (each box uses the folowing box's right border except the first child)*/ 
    /*border-radius: 4px 4px 0px 0px; 
    -moz-border-radius: 4px 4px 0px 0px; 
    -khtml-border-radius: 4px 4px 0px 0px; 
    -webkit-border-radius: 4px 4px 0px 0px;*/ 
} 
#menubar li:last-child { 
    border-right: 2px solid #ffffff;/*accompanies divider&box line aboves. Use "none" if you are using seperators and add in e.g. "1px solid #c0c0c0"" if borders*/ 
} 
#menubar ul ul li:last-child { 
    border-right: 2px solid #ffffff;/*accompanies divider&box line aboves. Use "none" if you are using seperators and add in e.g. "1px solid #c0c0c0"" if borders*/ 
} 
#menubar ul ul li:first-child { 
    border-top: 2px solid #ffffff;/*If boxes used on dropdown. Used to override border-top none below "li li"*/ 
    border-left: 2px solid #ffffff;/*If boxes used on dropdown. Use to override "li:first-child" on the previous level dropdown*/ 
} 
#menubar ul ul li {/*Dropdown items*/ 
    background-color: #D5CBA7;/*KEEP as background color needed fir IE6 to select over another div*/ 
    left: -2px;/*CONTROLS for border. 0 if no border. -1px if border is 1px. Adjust together with "li" above, "li:first-child" above, "border" below on this ID and "top" and "left" on "ul ul li:hover ul"*/ 
    width: 130px;/*needed to make the whole line selectable*/ 
    margin: 0;/*Needed to reset dropdowns if margin used on first level*/ 
    border-top: none; 
    border-right: 2px solid #ffffff; 
    border-bottom: 2px solid #ffffff; 
    border-left: 2px solid #ffffff;/*Use border right, bottom and left only if you want surrounding boxes. Also adjust "left" in this same ID (border top is "none")*/ /*border-left: none;/*Use "border-left:none" only if you want dividers and for them to be removed on every sublevel*/ 
    /*border-radius: 0px 0px 0px 0px; 
    -moz-border-radius: 0px 0px 0px 0px; 
    -khtml-border-radius: 0px 0px 0px 0px; 
    -webkit-border-radius: 0px 0px 0px 0px;*/ 
} 
#menubar ul ul, #menubar ul li:hover ul ul { /* Hide all the dropdowns (submenus) */ 
    display: none; 
    /*visibility:hidden;*/ 
    position: absolute;/*absolute means that it is absolutely positioned to the 'relative' div before it*/ 
    left: 0px;/*needed for IE7*/ 
    z-index: 60;/*for hovering over itself*/ 
    width: 130px; /*CONTROLS width of colored background in dropdown levels. Needed to float left*/ 
} 
#menubar ul li:hover ul_NULL {/*Second level submenus. Container box that the li's sit within and on top of. Displays when li are hovered on*/ 
    /*background-color:red;*/ 
    display: block; 
} 
#menubar ul ul li:hover ul {/*third level submenus. Container box that the li's sit within and on top of. Displays when li are hovered on*/ 
    /*background-color:red;*/ 
    display:block; 
    position: absolute;/*absolute means that it is absolutely positioned to the 'relative' div before it*/ 
    top: -2px; /*CONTROLS for border. 0 positions at the top with no border or -1 for border*/ 
    left: 132px;/*CONTROLS width. Match to "li" if width given + BORDER + 1 BORDER. 100% positions 1 col over to right, incl margin.*/ 
    z-index: 60;/*for hovering over itself*/ 
    width: 130px; /*CONTROLS width of colored background in dropdown levels. Needed to float left*/ 
} 
#menubar ul ul li:hover ul_NULL {/*third level submenus. Container box that the li's sit within and on top of. Displays when li are hovered on*/ 
    /*background-color:red;*/ 
    display: block; 
} 

终于HTML

<div id="menubar-outer"> 
    <span id="menubar" class="linkclass-mainmenu"> 
     <!--span needs to be used for ie7 to be able to use text-align on variable width--> 
     <div class="menu"> 
      <ul> 
       <li class="page_item page-item-21"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/adventures-of-huckleberry-finn/"> 
         Adventures of Huckleberry Finn 
        </a> 
        <ul class='children'> 
         <li class="page_item page-item-23"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/adventures-of-huckleberry-finn/conflict-between-civilization-and-natural-life/"> 
           Conflict between civilization and natural life 
          </a> 
          <ul class='children'> 
           <li class="page_item page-item-22"> 
            <a href="http://munchkingraphicdesign-uat.com/devsite/adventures-of-huckleberry-finn/conflict-between-civilization-and-natural-life/the-adventures-of-tom-sawyer/"> 
             The Adventures of Tom Sawyer 
            </a> 
           </li> 
          </ul> 
         </li> 
         <li class="page_item page-item-24"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/adventures-of-huckleberry-finn/intellectual-and-moral-education/"> 
           Intellectual and Moral Education 
          </a> 
         </li> 
        </ul> 
       </li> 
       <li class="page_item page-item-31"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/contact-us/"> 
         Contact Us 
        </a> 
       </li> 
       <li class="page_item page-item-9"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/duke-and-the-king/"> 
         Duke and the King 
        </a> 
        <ul class='children'><li class="page_item page-item-10"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/duke-and-the-king/conflict-between-civilization-and-natural-life/"> 
         Conflict between civilization and natural life 
        </a> 
        </li> 
         <li class="page_item page-item-12"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/duke-and-the-king/parodies-of-popular-romance-novels/"> 
           Parodies of Popular Romance Novels 
          </a> 
         </li> 
         <li class="page_item page-item-11"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/duke-and-the-king/the-hypocrisy-of-civilized-society/"> 
           The Hypocrisy of Civilized Society 
          </a> 
         </li> 
        </ul> 
       </li> 
       <li class="page_item page-item-17"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/jims-escape/"> 
         Jim&#8217;s escape 
        </a> 
        <ul class='children'> 
         <li class="page_item page-item-18"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/jims-escape/childhood/"> 
           Childhood 
          </a> 
         </li> 
         <li class="page_item page-item-19"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/jims-escape/conflict-between-civilization-and-natural-life/"> 
           Conflict between civilization and natural life 
          </a> 
         </li> 
         <li class="page_item page-item-20"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/jims-escape/reception/"> 
           Reception 
          </a> 
         </li> 
        </ul> 
       </li> 
       <li class="page_item page-item-5"> 
        <a href="http://munchkingraphicdesign-uat.com/devsite/mockery-of-religion/"> 
         Mockery of Religion 
        </a> 
        <ul class='children'> 
         <li class="page_item page-item-8"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/mockery-of-religion/duke-and-the-king/"> 
           Duke and the King 
          </a> 
         </li> 
         <li class="page_item page-item-6"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/mockery-of-religion/jims-escape/"> 
           Jim&#8217;s escape 
          </a> 
         </li> 
         <li class="page_item page-item-7"> 
          <a href="http://munchkingraphicdesign-uat.com/devsite/mockery-of-religion/life-in-st-petersburg/"> 
           Life in St. Petersburg 
          </a> 
         </li> 
        </ul> 
       </li> 
      </ul> 
     </div> 
    </span><!-- end #menubar --> 
    <div class="clearfix"> 

    </div><!--Used when floating. Clearfix comes before closing div of non-floated element--> 
</div><!--end #menubar-outer--> 

jsFiddle

+0

#menubar UL李UL李 - 意味着你已经指定了两个级别...这就是为什么它不能在第三个层次的工作。做一些类似于#menubar ul li ul.subUL的地方,其中subUL是属于第一个和内部孩子的一个子类的ul类......确保所有内在的uls都有class =“subUL” – robert 2012-04-20 02:04:59

+0

感谢Robert!这有助于我认为的最后一行 - 但这只是在菜单项上添加符号以显示还有更多的子菜单。 那第一节我不是很理解。我是否需要将此代码翻倍并将其更改为下一级别的帐户?试图在jsfiddle中搞乱它(刚发现它),但可能会在周末。 – Peter 2012-04-20 19:16:19

回答

1

这是您的菜单的modified jsFiddle - 在CSS中使用3级下拉菜单。

我已经在Chrome,Firefox & IE中测试过它,它工作正常。

希望它可以帮助你。下面的代码是...

CSS

#menubar-outer { 
    background-color: #D5CBA7; 
    width: 100%; 
    margin: 0; 
    padding: 0; 
    text-align: left; 
} 
#menubar { 
    display: inline-block; 
    position: relative; 
    z-index: 50; 
    margin: 0; 
    padding: 0; 
    font-size: 1em; 
    text-transform: uppercase; 
    vertical-align: bottom; 
} 
#nav { 
    margin: 0 auto; 
    padding: 7px 6px 0; 
    line-height: 100%; 
} 
#nav li { 
    margin: 0 5px; 
    padding: 0 0 8px; 
    float: left; 
    position: relative; 
    list-style: none; 
} 

/* main level link */ 
#nav a { 
    font-weight: bold; 
    color: #000; 
    text-decoration: none; 
    display: block; 
    padding: 8px 20px; 
    margin: 0; 
} 

/* main level link hover */ 
#nav .current a, #nav li:hover > a { 
    background: #D5CBA7; 
    color: #000; 
} 

/* sub levels link hover */ 
#nav li a { 
    background: #D5CBA7; 
    border: none; 
} 

#nav li a:hover { 
    color: #fff; 
    background: #AE953C; 
} 

/* dropdown */ 
#nav li:hover > ul { 
    display: block; 
} 

/* level 2 list */ 
#nav ul { 
    display: none; 
    margin: 0; 
    padding: 0; 
    width: 185px; 
    position: absolute; 
} 
#nav ul li { 
    float: none; 
    margin: 0; 
    padding: 0; 
} 

#nav ul a { 
    font-weight: normal; 
    font-color: #000; 
} 

/* level 3+ list */ 
#nav ul ul { 
    left: 181px; 
    top: -3px; 
} 

#nav { 
    display: inline-block; 
} 

HTML

<div id="menubar-outer"> 
    <span id="menubar" class="linkclass-mainmenu"> 
     <!--span needs to be used for ie7 to be able to use text-align on variable width--> 
     <div class="menu"> 
      <ul id="nav"> 
      <li class="current"><a href="http://www.webdesignerwall.com">Home</a></li> 
      <li><a href="http://www.ndesign-studio.com">My Projects</a> 
       <ul> 
        <li><a href="http://www.ndesign-studio.com">N.Design Studio</a> 
         <ul> 
          <li><a href="http://www.ndesign-studio.com/portfolio">Portfolio</a></li> 
          <li><a href="http://www.ndesign-studio.com/wp-themes">WordPress Themes</a></li> 
          <li><a href="http://www.ndesign-studio.com/wallpapers">Wallpapers</a></li> 
          <li><a href="http://www.ndesign-studio.com/tutorials">Illustrator Tutorials</a></li> 
         </ul> 
        </li> 
        <li><a href="http://www.webdesignerwall.com">Web Designer Wall</a> 
         <ul> 
          <li><a href="http://jobs.webdesignerwall.com">Design Job Wall</a></li> 
         </ul> 
        </li> 
        <li><a href="http://icondock.com">IconDock</a></li> 
        <li><a href="http://bestwebgallery.com">Best Web Gallery</a></li> 
       </ul> 
      </li> 
      <li><a href="#">Multi-Levels</a> 
       <ul> 
        <li><a href="#">Team</a> 
         <ul> 
          <li><a href="#">Sub-Level Item</a></li> 
          <li><a href="#">Sub-Level Item</a> 
           <ul> 
            <li><a href="#">Sub-Level Item</a></li> 
            <li><a href="#">Sub-Level Item</a></li> 
            <li><a href="#">Sub-Level Item</a></li> 
           </ul> 
          </li> 

          <li><a href="#">Sub-Level Item</a></li> 
         </ul> 
        </li> 
        <li><a href="#">Sales</a></li> 
        <li><a href="#">Another Link</a></li> 
        <li><a href="#">Department</a> 
         <ul> 
          <li><a href="#">Sub-Level Item</a></li> 
          <li><a href="#">Sub-Level Item</a></li> 
          <li><a href="#">Sub-Level Item</a></li> 
         </ul> 
        </li> 
       </ul> 
      </li> 
      <li><a href="#">About</a></li> 
      <li><a href="#">Contact Us</a></li> 
      </ul> 
     </div> 
    </span><!-- end #menubar --> 
    <div class="clearfix"> 

    </div><!--Used when floating. Clearfix comes before closing div of non-floated element--> 
</div><!--end #menubar-outer--> 
+0

谢谢。看起来比我的更干净。会过去。不是100%确定我可以在Wordpress中获得所有课程(他们被自动分配并认为尝试更改它们会变得更加复杂),但学习将帮助我。 – Peter 2012-04-20 19:23:00