2013-03-20 3 views
1

클릭 할 때 켜고 끌 수있는 간단한 모바일 메뉴를 만들고 싶습니다. 또한 부모가 클릭 될 때까지 부모 고양이 만 표시하는 것이 좋습니다.내 jquery 토글을 방해하는 CSS 표시 속성

나는이 두 가지에 대해 조사한 결과 좋은 답변을 얻었습니다. 그러나, 결합하면 나는 원하는 결과를 얻지 못하고있다.

특히 내가 사용하고있는 jquery 토글은 navul을 "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; 
     } 

이 일은 내가하는 일을하는 멍청한 방법 일 뿐이며, 나는 더 단순한 대안을 열어두고있다.

+0

FWIW, 이것을 간단한 데모 페이지에 설정하는 동안, 최종 계획은 이것을 magento 점포에 구현하여 nav 메뉴를 동적으로 생성하는 것입니다. – skondris

+0

'$ ('# nav> li> ul'). show(). parent ('. level0'). find ('> ul'). 숨기기(); (this) .parent(). hasClass ("level0")) { $ (this)) 각각은 ($ { 'nav> li> .hide(); } 다른 { $ (이) .show(); } });' –

+0

그리고 나는 잠시 동안 그것에 대해 생각하고, 간단하게 할 수있다 :'$ ('#의 탐색> 리 .level0> ul '). hide();' –

답변

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); 
    } 
}); 
+0

좋아요! 고맙습니다! 이것은 완벽하게 작동합니다! 나는 더 간단한 것을 사용했다. – skondris

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로 간다 거기에 약간 쓸모없는 일을하지만 난 당신이 더 심각한 문제로이 문제를 해결할 수 있습니다 생각합니다.

+0

예, 나는 지나치게 복잡하게하는 경향이 있습니다. :) 참고 : 저는 이것을 magento 점포 내의 동적 메뉴에 구현할 것입니다. 따라서 Nav 컨테이너와 각 부모 Nav 요소에 대한 표시 속성을 설정하는 것과 같은 방법을 사용하는 것이 좋습니다. 다시 말하지만, 토글은 그 자체로 훌륭하게 작동합니다. 부모가 클릭하여 끈적 거리게 될 때까지 자식 요소를 숨기는 다른 방법을 시도해 보는 것입니다. – skondris