2017-10-19 11 views
13

私の垂直ナビゲーションに5つのリンクがあります。画面のサイズを変更すると、垂直ナビゲーションは水平ナビゲーションになり、残りの2つのリンクを表示するmenuという別のリンクが3つ表示されます。サブメニューが画面のサイズ変更で正しくレンダリングされない(および予期しない動作)

何らかの理由で、画面のサイズ変更時にmenuが表示された場合、リストの内容はすでに表示されています。メニューをクリックすると、ホバーオーバーしていなくてもホバープロパティが残ります。ここではビジュアルです:画面のリサイズで

1.、次のように表示されます:

enter image description here

です:

enter image description here

2.は、メニューの上にマウスを移動するときOK。メニューリンクがクリックされたときにリンクが表示されるようにするだけです。しかし、私はなぜmenu 2つのliスペースを話すのか分からない。

3.

メニューをクリック:

enter image description here

これはOKです。ただし、Menu liのサイズがどのようになっているかに注意してください。

4. menuをクリックして、離れたリンクから、マウスを移動した後:前述のように enter image description here

、私はこれらの問題を引き起こしているのか分かりません。

$(document).ready(function() { 
 
    $(".show").click(function() { 
 
    $(".subMenu").toggleClass("active"); 
 
    return false; 
 
    }); 
 
});
.site-wrapper { 
 
    height: 100%; 
 
    min-height: 100%; 
 
    display: flex; 
 
} 
 

 

 
/* make divs appear below each other on screen resize */ 
 

 
@media screen and (max-width: 540px) { 
 
    .site-wrapper { 
 
    flex-direction: column; 
 
    } 
 
} 
 

 
ul.subMenu { 
 
    display: none; 
 
} 
 

 
.subMenu.active { 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
li.show { 
 
    display: none; 
 
} 
 

 
.nav-container { 
 
    border-right: 1px solid #E4E2E2; 
 
    height: 100%; 
 
    width: 200px; 
 
    background-color: #f4f3f3; 
 
} 
 

 
.logo-holder { 
 
    text-align: center; 
 
} 
 

 
.nav { 
 
    text-align: justify; 
 
} 
 

 
nav:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
} 
 

 
.nav-link { 
 
    display: block; 
 
    text-align: left; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 0px; 
 
    padding-left: 15px; 
 
} 
 

 
.nav-link:hover { 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
.nav ul { 
 
    width: 100%; 
 
    /* make div span div */ 
 
    padding: 0px; 
 
} 
 

 
.nav ul li { 
 
    list-style-type: none; 
 
} 
 

 
.nav li:hover a { 
 
    color: #f4f3f3; 
 
} 
 

 
.active { 
 
    text-align: left; 
 
    padding-left: 15px; 
 
    text-decoration: none; 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
@media screen and (max-width: 540px) { 
 
    .nav-container { 
 
    width: 100%; 
 
    height: 100px; 
 
    background-color: #f4f3f3; 
 
    border-bottom: 0.5px solid #f4f3f3; 
 
    } 
 
    .nav-link { 
 
    padding: 10px; 
 
    } 
 
    .logo-holder { 
 
    overflow: hidden; 
 
    display: block; 
 
    margin: auto; 
 
    width: 40%; 
 
    } 
 
    .nav-container nav ul { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
    } 
 
    .logo-holder { 
 
    text-align: left; 
 
    } 
 
    #navigation-div { 
 
    background-color: #f4f3f3; 
 
    margin-top: 0; 
 
    } 
 
    .socials { 
 
    display: none; 
 
    } 
 
    .hide { 
 
    display: none; 
 
    } 
 
    .show { 
 
    display: inline-block !important; 
 
    } 
 
    .nav ul li {} 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="site-wrapper"> 
 
    <div class="nav-container"> 
 
    <div class="logo-holder"> 
 
     <img class="user-select-none" src="images/temp-logo.jpeg" alt="temp" /> 
 
    </div> 
 
    <div id="navigation-div"> 
 
     <nav class="nav"> 
 
     <ul> 
 
      <li><a class="nav-link active" href="">Test 1</a></li> 
 
      <li><a class="nav-link " href="">Test 2</a></li> 
 
      <li><a class="nav-link" href="">Test 3</a></li> 
 
      <li class="hide"><a class="nav-link hide" href="">Test 4</a></li> 
 
      <li class="hide"><a class="nav-link hide" href="">Test 5</a></li> 
 
      <li class="show"><a class="nav-link" href="">Menu</a> 
 
      <ul class="subMenu"> 
 
       <li><a class="nav-link" href="">Test 4</a></li> 
 
       <li><a class="nav-link" href="">Test 5</a></li> 
 
      </ul> 
 
      </li> 
 
     </ul> 
 
     </nav> 
 
    </div> 
 
    </div> 
 
</div>

+0

だから、テスト4とテスト5のようなものは、メニューのクリックだけに表示されます。右? –

答えて

2

説明:

あなたが台無しに非常に多くのものを持っている。ここ

は私の現在のアプローチです。注意すべき最も重要な点は、サブ要素はメイン要素から継承していることです。たとえば、我々は次のようなHTMLがある場合:

<ul id="main_menu"> 
    <li>list item 1</li> 
    <li>list item 2</li> 
    <li> 
     <ul id="sub_menu"> 
      <li>sub list item 1</li> 
     </ul> 
    </li> 
</ul> 

そして、このCSS:だから

#main_menu li {/* This styling will also be applied to sub_menu!! */ 
    color: red; 
} 

を使用すると、メインメニューの下にのみダイレクトリーに適用したい場合は、唯一の直接意味>を使用問題を修正

#main_menu > li {/* This styling will be applied only to direct li, sub_menu li will not take this styling */ 
    color: red; 
} 

:そうのような要素を、

1-追加!

ul.subMenu { 
    display: none !important; 
} 

2 - コメントや、この行を削除します:サブメニューに重要な

.nav li:hover a { 
    /* color: #f4f3f3; */ 
} 

3 - あなたのサブメニューは、メインメニューからいくつかの不要なスタイルを継承しています。これらを追加してください:

.subMenu a.nav-link { 
    background-color: #f4f3f3; 
    color: #333; 
} 
.subMenu a.nav-link:hover { 
    background-color: #333; 
    color: #f4f3f3; 
} 
.subMenu.active { 
    display: block !important; 
} 

ここにはdemoがあります。何らかの理由で

0

、画面上のサイズを変更するメニューが表示されたら、リストの内容は、すでにあなたがサイズを変更するとものを行うためにjQueryのサイズ変更機能を使用することができます

が表示されます。

しかし、なぜメニューが2つの隙間を話すのか分かりません。

メニューには2つの隙間があります。ulには2つのスペースがあり、幅が潰れています。 アンカータグに幅を追加すると、liから幅を継承しないため、アンカータグの外観が少し良くなります。 a.nav-link {width:50px;}

メニューをクリックしてリンクからマウスを離した後、

jQueryを使用してサブメニューの親にクラスを追加すると、スタイリングを制御できます。

ここにコードがあります。

jQueryの

$(document).ready(function() { 
    $(".subMenu").addClass('hide'); 
    $(".show").click(function() { 
     $(".show").toggleClass('active-parent'); 
    $(".subMenu").toggleClass("active"); 
    $(".subMenu").toggleClass("hide"); 
    return false; 
    }); 
}); 
$(window).resize(function(){ 
    $(".subMenu").addClass('hide'); 
}); 

HTML

<div class="site-wrapper"> 
    <div class="nav-container"> 
    <div class="logo-holder"> 
     <img class="user-select-none" src="images/temp-logo.jpeg" alt="temp" /> 
    </div> 
    <div id="navigation-div"> 
     <nav class="nav"> 
     <ul> 
      <li><a class="nav-link active" href="">Test 1</a></li> 
      <li><a class="nav-link " href="">Test 2</a></li> 
      <li><a class="nav-link" href="">Test 3</a></li> 
      <li class="hide"><a class="nav-link hide" href="">Test 4</a></li> 
      <li class="hide"><a class="nav-link hide" href="">Test 5</a></li> 
      <li class="show"><a class="nav-link" href="">Menu</a> 
      <ul class="subMenu"> 
       <li><a class="nav-link" href="">Test 4</a></li> 
       <li><a class="nav-link" href="">Test 5</a></li> 
      </ul> 
      </li> 
     </ul> 
     </nav> 
    </div> 
    </div> 
</div> 

CSS

.site-wrapper { 
    height: 100%; 
    min-height: 100%; 
    display: flex; 
} 


/* make divs appear below each other on screen resize */ 

@media screen and (max-width: 540px) { 
    .site-wrapper { 
    flex-direction: column; 
    } 
} 

ul.subMenu { 
    display: none; 
} 

.subMenu.active { 
    display: flex; 
    flex-direction: column; 
} 

li.show { 
    display: none; 
} 

.nav-container { 
    border-right: 1px solid #E4E2E2; 
    height: 100%; 
    width: 200px; 
    background-color: #f4f3f3; 
} 

.logo-holder { 
    text-align: center; 
} 

.nav { 
    text-align: justify; 
} 

nav:after { 
    content: ""; 
    display: table; 
    clear: both; 
} 

.nav-link { 
    display: block; 
    text-align: left; 
    color: #333; 
    text-decoration: none; 
    margin-left: 0px; 
    padding-left: 15px; 
} 

.nav-link:hover { 
    background-color: #333; 
    color: #f4f3f3; 
} 

.nav ul { 
    width: 100%; 
    /* make div span div */ 
    padding: 0px; 
} 

.nav ul li { 
    list-style-type: none; 
} 

.nav li:hover a { 
    color: #f4f3f3; 
} 

.active { 
    text-align: left; 
    padding-left: 15px; 
    text-decoration: none; 
    background-color: #333; 
    color: #f4f3f3; 
} 

@media screen and (max-width: 540px) { 
    .nav-container { 
    width: 100%; 
    height: 100px; 
    background-color: #f4f3f3; 
    border-bottom: 0.5px solid #f4f3f3; 
    } 
    .nav-link { 
    padding: 10px; 
    } 
    .logo-holder { 
    overflow: hidden; 
    display: block; 
    margin: auto; 
    width: 40%; 
    } 
    .nav-container nav ul { 
    display: flex; 
    flex-wrap: wrap; 
    justify-content: center; 
    } 
    .logo-holder { 
    text-align: left; 
    } 
    #navigation-div { 
    background-color: #f4f3f3; 
    margin-top: 0; 
    } 
    .socials { 
    display: none; 
    } 
    .hide { 
    display: none; 
    } 
    .nav-container nav .hide{ 
     display: none; 
    } 
    .show { 
    display: inline-block !important; 
    } 
    a.nav-link { 
    width: 50px; 
    } 
    .active-parent a.nav-link { 
    color: #ffffff; 
    background: #333; 
    } 
    .nav ul li {} 
} 
0

ません2.

あなたは、のようにフレックス表示そのようにそれを変更するには、両方のulを設定していますw私もいくつかのクラスを追加しました

.selected a, 
.active a { 
    background-color: #333; 
    color: #f4f3f3; 
} 

ユーザーメニューが開いているときに簡単にスタイリングするためにメニューをクリックしたときに病気にのみ2番目のレベルのリンクの親ulだけ

.nav-container nav>ul { 
    display: flex; 
    flex-wrap: wrap; 
    justify-content: center; 
    } 

化粧品に適用

$(this).toggleClass('selected'); 

(function($) { 
 
    $(document).ready(function() { 
 
    $(".show").click(function() { 
 
     $(this).toggleClass('selected'); 
 
     $(".subMenu").toggleClass("active"); 
 
     return false; 
 
    }); 
 
    }); 
 
})(jQuery);
.site-wrapper { 
 
    height: 100%; 
 
    min-height: 100%; 
 
    display: flex; 
 
} 
 

 

 
/* make divs appear below each other on screen resize */ 
 

 
@media screen and (max-width: 540px) { 
 
    .site-wrapper { 
 
    flex-direction: column; 
 
    } 
 
} 
 

 
ul.subMenu { 
 
    display: none; 
 
} 
 

 
.subMenu.active { 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
li.show { 
 
    display: none; 
 
} 
 

 
.nav-container { 
 
    border-right: 1px solid #E4E2E2; 
 
    height: 100%; 
 
    width: 200px; 
 
    background-color: #f4f3f3; 
 
} 
 

 
.logo-holder { 
 
    text-align: center; 
 
} 
 

 
.nav { 
 
    text-align: justify; 
 
} 
 

 
nav:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
} 
 

 
.nav-link { 
 
    display: block; 
 
    text-align: left; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 0px; 
 
    padding-left: 15px; 
 
} 
 

 
.nav-link:hover { 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
.nav ul { 
 
    width: 100%; 
 
    /* make div span div */ 
 
    padding: 0px; 
 
} 
 

 
.nav ul li { 
 
    list-style-type: none; 
 
} 
 

 
.nav>li:hover a { 
 
    color: #f4f3f3; 
 
} 
 

 
.active { 
 
    text-align: left; 
 
    padding-left: 15px; 
 
    text-decoration: none; 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
.selected a, 
 
.active a { 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
@media screen and (max-width: 540px) { 
 
    .nav-container { 
 
    width: 100%; 
 
    height: 100px; 
 
    background-color: #f4f3f3; 
 
    border-bottom: 0.5px solid #f4f3f3; 
 
    } 
 
    .nav-link { 
 
    padding: 10px; 
 
    } 
 
    .logo-holder { 
 
    overflow: hidden; 
 
    display: block; 
 
    margin: auto; 
 
    width: 40%; 
 
    } 
 
    .nav-container nav>ul { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
    } 
 
    .logo-holder { 
 
    text-align: left; 
 
    } 
 
    #navigation-div { 
 
    background-color: #f4f3f3; 
 
    margin-top: 0; 
 
    } 
 
    .socials { 
 
    display: none; 
 
    } 
 
    .hide { 
 
    display: none; 
 
    } 
 
    .show { 
 
    display: inline-block !important; 
 
    } 
 
    .nav ul li {} 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="site-wrapper"> 
 
    <div class="nav-container"> 
 
    <div class="logo-holder"> 
 
     <img class="user-select-none" src="images/temp-logo.jpeg" alt="temp" /> 
 
    </div> 
 
    <div id="navigation-div"> 
 
     <nav class="nav"> 
 
     <ul> 
 
      <li><a class="nav-link active" href="">Test 1</a></li> 
 
      <li><a class="nav-link " href="">Test 2</a></li> 
 
      <li><a class="nav-link" href="">Test 3</a></li> 
 
      <li class="hide"><a class="nav-link hide" href="">Test 4</a></li> 
 
      <li class="hide"><a class="nav-link hide" href="">Test 5</a></li> 
 
      <li class="show"><a class="nav-link" href="">Menu</a> 
 
      <ul class="subMenu"> 
 
       <li><a class="nav-link" href="">Test 4</a></li> 
 
       <li><a class="nav-link" href="">Test 5</a></li> 
 
      </ul> 
 
      </li> 
 
     </ul> 
 
     </nav> 
 
    </div> 
 
    </div> 
 
</div>

関連する問題