css显示属性干扰我的jquery切换

时间:2013-03-20 23:43:08

标签: jquery css mobile menu toggle

我想创建一个简单的移动菜单,点击后可以打开和关闭。我还希望它只显示父母而不是儿童猫,直到点击父母。

我研究过,并找到了这两个方面的好答案。但是,当合并时,我没有得到理想的结果。

具体来说,我正在使用的jquery切换依赖于nav ul设置为“display:none”作为它们的起始状态,但这是以某种方式干扰我的其他jquery函数来隐藏其父级是顶级ul的元素类。

除了在初始加载菜单时没有显示任何子项的部分外,一切都按预期工作。但是,一旦我删除了最初完全隐藏我的nav ul的css行,那么孩子们就会被隐藏(但当然,我的导航会在加载时展开,当我想要隐藏它时。

页面我正在尝试解决这个问题:[http://saks-jewelers.com/mobile-nav/mobile_nav.html#]

这是我的html代码和jquery:

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Responsive Header Nav</title>
    <meta name="description" content="Responsive Header Nav">
    <meta name="author" content="Treehouse">
    <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#menu-icon").click(function(){
        $("nav ul").toggle(200);
      });

        $('#nav>li>ul').each(function(){
            if($(this).parent().hasClass("level0")) 
            { 
                $(this).hide();
            } else {
                $(this).show(); 
            }
        });
        $('#nav>li').click(function(){
            // check that the menu is not currently animated
            if ($('#nav ul:animated').size() == 0) {
                // create a reference to the active element (this)
                // so we don't have to keep creating a jQuery object
                $heading = $(this);
                // create a reference to visible sibling elements
                // so we don't have to keep creating a jQuery object
                $expandedSiblings = $heading.siblings().find('ul:visible');
                if ($expandedSiblings.size() > 0) {
                    $expandedSiblings.slideUp(200, function(){
                        $heading.find('ul').slideDown(200);
                    });
                }
                else {
                    $heading.find('ul').slideDown(200);
                }
            }
        });
    });
    </script>
    <body>
    <header>
    <a href="#" id="logo"></a>
    <nav>
        <a href="#" id="menu-icon"></a>
    <ul id="nav">
            <li class="level0 nav-1 first level-top parent">
            <a href="#" class="level-top"><span>Rings</span></a>
            <ul class="level0">
                <li class="level1 nav-1-1 first"><a href="#" class=""><span>CZ Fashion</span></a></li>
                <li class="level1 nav-1-2"><a href="#" class=""><span>Gemstone Fashion</span></a></li>
                <li class="level1 nav-1-3"><a href="#" class=""><span>Silver &amp; Gold Fashion</span></a></li>
                <li class="level1 nav-1-4"><a href="#" class=""><span>Pearl</span></a></li>
                <li class="level1 nav-1-5"><a href="#" class=""><span>Moissanite™</span></a></li>
                <li class="level1 nav-1-6"><a href="#" class=""><span>Religious &amp; Symbolic</span></a></li>
                <li class="level1 nav-1-7"><a href="#" class=""><span>Youth</span></a></li>
                <li class="level1 nav-1-8"><a href="#" class=""><span>Promise Rings</span></a></li>
                <li class="level1 nav-1-9 last"><a href="#" class=""><span>Wedding</span></a></li>
            </ul>
        </li>
        <li class="level0 nav-2 level-top parent"><a href="#" class="level-top"><span>Earrings</span></a>
            <ul class="level0">
                <li class="level1 nav-2-1 first"><a href="#" class=""><span>CZ Fashion</span></a></li>
                <li class="level1 nav-2-2"><a href="#" class=""><span>Gemstone Fashion</span></a></li>
                <li class="level1 nav-2-3"><a href="#" class=""><span>Silver &amp; Gold Fashion</span></a></li>
                <li class="level1 nav-2-4"><a href="#" class=""><span>Pearl</span></a></li>
                <li class="level1 nav-2-5"><a href="#" class=""><span>Moissanite™</span></a></li>
                <li class="level1 nav-2-6"><a href="#" class=""><span>Religious &amp; Symbolic</span></a></li>
                <li class="level1 nav-2-7 last"><a href="#" class=""><span>Youth</span></a></li>
            </ul>
        </li>
    </ul>
    </nav>
    </header>
    <section>
    <img src="featured.png" alt="Respond" />
    <h1>Respond to Your Surroundings</h1>
    <p>...Filler Text</p>
    <p>...Filler Text</p>
    <p>Filler Text</p>
    </section>
    </body>
    </html>

这是我的css:

    /*RESET*/
    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {margin:0; padding:0; border:0; outline:0; font-size:100%; vertical-align:baseline; background:transparent;} body {line-height: 1;}ol, ul{list-style:none;} blockquote, q{quotes:none;} blockquote:before, blockquote:after, q:before, q:after{content:'';content:none;} :focus{outline:0;} ins{text-decoration:none;} del{text-decoration:line-through;} table{border-collapse:collapse; border-spacing:0;}

    /*MAIN*/
    body { 
        font-size: 1.05em;
        line-height: 1.25em;
        font-family: Helvetica Neue, Helvetica, Arial;
        background: #f9f9f9;
        color: #555;
    }
    a {
        color: #4C9CF1;
        text-decoration: none;
        font-weight: bold;
    }
    a:hover { color: #444;  }
    img { width: 100%;  }
    header {
        background: #fff;
        width: 100%;
        height: 76px;
        position: fixed;
        top: 0;
        left: 0;
        border-bottom: 4px solid #4C9CF1;
        z-index: 100;
            }
    #logo{
        margin: 20px;
        float: left;
        width: 200px;
        height: 40px;
        background: url(nav-logo.png) no-repeat center;
        display: block;
            }
    nav {   float: right;   padding: 20px;  }
    #menu-icon {
        display: hidden;
        width: 40px;
        height: 40px;
        background: #4C8FEC url(nav-menu-icon.png) center;
    }
    a:hover#menu-icon {background-color: #444; border-radius: 4px 4px 0 0;   }
    ul { list-style: none; }
    li {
        display: inline-block;
        float: left;
            padding: 10px
    }
    .current {color: #2262AD;}
    section {
        margin: 80px auto 40px;
        max-width: 980px;
        position: relative;
        padding: 20px
    }
    h1 {
        font-size: 2em;
        color: #2262AD;
        line-height: 1.15em;
        margin: 20px 0 ;
    }
    p {line-height: 1.45em; margin-bottom: 20px;}

    /*MEDIA QUERY*/
    @media only screen and (max-width : 640px) {

        header {position: absolute; }
        #menu-icon { display:inline-block; }
        nav ul, nav:active ul { 
            display: none;
            position: absolute;
            padding: 20px;
            background: #fff;
            right: 20px;
            top: 60px;
            width: 50%;
        }
        nav li {
            text-align: left;
            width: 100%;
            padding: 10px 0;
            margin: 0;
        }

如果这只是一种愚蠢的方式来做我想做的事情,我会接受更简单的替代方案。

2 个答案:

答案 0 :(得分:1)

我认为你的整个事情让你复杂化了。我会这样做的方式:

<div class="rings" style="display:none">
    <ul>
      <li> ring 1 .... </li>
      <li> ring 2 .... </li>
      ....
    </ul>
</div>

<div class="earrings" style="display:none">
    <ul>
      <li> ring 1 .... </li>
      <li> ring 2 .... </li>
      ....
    </ul>
</div>

接下来是jQuery:

$('.menuAnchor').on('click', function(){
   var $this = $(this); //get the clicked element again

   //Get the text in the a tag and pass it a class selector
   //Therefor <a class="menuAnchor">earrings</a> on click with toggle the earrings div
   $('.'+$this.html()).slideToggle(500); 
});

就css而言,那里有一些无用的东西,但我认为你可以解决这个问题而没有重大问题。

答案 1 :(得分:1)

你可能能够改进这一点:(如果我明白你的意图,这应该有效)

var subs = $('#nav>li.level0>ul');
$("#menu-icon").click(function () {
      $("nav> ul").toggle(200);
      if ($("nav ul").is(':hidden')) {
          subs.hide();
      }
  });
  subs.hide();
  $('#nav>li').click(function () {
       if (!$('#nav ul:animated').length) {
          $heading = $(this);
          $expandedSiblings = $heading.siblings().find('ul:visible');
          if ($expandedSiblings.length) {
              $expandedSiblings.slideUp(200, function () {
                  $heading.find('ul').slideDown(200);
              });
          } else {
              $heading.find('ul').slideDown(200);
          }
      }
  });

这似乎也有效,而且更简单:

 var subs = $('#nav>li.level0>ul');
 $("#menu-icon").click(function () {
     $("nav> ul").toggle(200);
     if ($("nav ul").is(':hidden')) {
         subs.hide();
     }
 });
 subs.hide();
 $('#nav>li').click(function () {
     if (!$('#nav ul:animated').length) {
         $(this).siblings().find('ul:visible').slideUp(200, function () {
             $(this).find('ul').slideDown(200);
         });
        $(this).find('ul').slideDown(200);
     }
 });