実現したいこと
添付画像のようにナビゲーションの「在宅クリーニング」にカーソルが乗ったときに、詳細なナビゲーションリスト(背景青色で囲われている要素たち)を表示させてカーソルが離れた時には非表示にされるようにしたい。
発生している問題・分からないこと
「在宅クリーニング」にカーソルが乗っていても、離れていてもずっと表示されたままになってしまう。
該当のソースコード
HTML
1<header class="header"> 2 <!-- ハンバーガーメニュー・SP用 --> 3 <button id="bt-hamburger" onclick="document.getElementById('nav-sp').style.display = 'block'"> 4 <img src="assets/img/bt-hamburger.png" alt="ナビゲーションを開く"> 5 </button> 6 7 <!-- ナビゲーション・SP用 --> 8 <div id="nav-sp"> 9 <button id="bt-close" onclick="document.getElementById('nav-sp').style.display = 'none'"> 10 <img src="assets/img/bt-close.png" alt="ナビゲーションを閉じる"> 11 </button> 12 <!--メニューリスト・SP用 --> 13 <nav class="nav-sp-list"> 14 <ul> 15 <li><a class="menu-sp" href="index.html" onclick="document.getElementById('nav-sp').style.display = 'none'">ホーム</a></li> 16 <li><a class="menu-sp" href="vacant-room.html" onclick="document.getElementById('nav-sp').style.display = 'none'">空室クリーニング</a></li> 17 <li><a class="menu-sp" href="at-home.html" onclick="document.getElementById('nav-sp').style.display = 'none'">在宅クリーニング</a></li> 18 <li><a class="menu-sp" href="about.html" onclick="document.getElementById('nav-sp').style.display = 'none'">店舗概要</a></li> 19 <li class="menu-sp-bottom"><a class="menu-sp" href="contact.html" onclick="document.getElementById('nav-sp').style.display = 'none'">問い合わせ</a></li> 20 </ul> 21 </nav> 22 </div> 23 24 25 <!-- 店名ロゴ --> 26 <div class="shop-logo-layout"> 27 <a href="#"> 28 <img class="shop-logo-header" src="assets/img/top/shop-logo-header-02.png" alt=""> 29 </a> 30 </div> 31 <!-- PC用ナビゲーション --> 32 <nav> 33 <ul> 34 <li><a href="index.html">ホーム</a></li> 35 <li><a href="vacant-room.html">空室クリーニング</a></li> 36 <li><a href="at-home.html">在宅クリーニング</a></li> 37 <li><a href="about.html">店舗概要</a></li> 38 <li><a href="contact.html">問い合わせ</a></li> 39 </ul> 40 </nav> 41 42 <section class="nav-more active"> 43 <div class="list-top"> 44 <div class="list-01"> 45 <div class="list-01-first"> 46 <h3>> 空室クリーニング</h3> 47 <a href="vacant-room.html">ー 空室クリーニング</a> 48 </div> 49 <div class="list-01-second"> 50 <h3>> エアコンのお掃除</h3> 51 <a href="at-home-01.html">ー エアコンクリーニング</a> 52 </div> 53 </div> 54 55 <div class="list-02"> 56 <h3>> 外回りのお掃除</h3> 57 <a href="at-home-10.html">ー 玄関クリーニング</a> 58 <a href="at-home-11.html">ー ベランダクリーニング</a> 59 <a href="at-home-09.html">ー 窓・サッシクリーニング</a> 60 </div> 61 62 <div class="list-03"> 63 <h3>> インターネット設定</h3> 64 <a href="at-home-12.html">ー インターネット設定代行</a> 65 </div> 66 </div> 67 68 <div class="list-bottom"> 69 <div class="list-03"> 70 <div class="list-03-first"> 71 <h3>> 水まわりのお掃除</h3> 72 <a href="at-home-02.html">ー 浴室クリーニング</a> 73 <a href="at-home-08.html">ー 洗面所クリーニング</a> 74 <a href="at-home-07.html">ー トイレクリーニング</a> 75 </div> 76 77 <div class="list-03-second"> 78 <h3>> キッチンのお掃除</h3> 79 <a href="at-home-06.html">ー キッチンクリーニング</a> 80 <a href="at-home-05.html">ー ファンタイプ・レンジフードクリーニング</a> 81 <a href="at-home-04.html">ー プロペラタイプ・レンジフードクリーニング</a> 82 </div> 83 </div> 84 85 <div class="list-04"> 86 <h3>> お得なセットプラン</h3> 87 <a href="set-plan.html#set-01">ー 2点セット(浴室、レンジフード)</a> 88 <a href="set-plan.html#set-02">ー 2点セット(キッチン、レンジフード)</a> 89 <a href="set-plan.html#set-03">ー 3点セット(浴室、キッチン、レンジフード)</a> 90 <a href="set-plan.html#set-04">ー 4点セット(浴室、キッチン、レンジフード、洗面所)</a> 91 <a href="set-plan.html#set-05">ー 5点セット(浴室、キッチン、レンジフード、洗面所、トイレ)</a> 92 93 </div> 94 </div> 95 </section> 96</header> 97
scss
1.header { 2 margin-left: 15%; // padding-left ではなく margin に 3 color: #333333; 4 display: flex; 5 align-items: center; 6 justify-content: space-between; /* 追加 */ 7 padding-right: 9.3%; 8 // スマホレイアウト 9 @media screen and (max-width: 768px){ 10 all: unset; /* すべてのプロパティをリセット */ 11 } 12 13 // ナビゲーション 14 > nav { 15 flex-grow: 1; 16 // スマホレイアウトの際には非表示にする 17 @media screen and (max-width: 768px){ 18 display: none; 19 } 20 21 > ul { 22 display: flex; 23 justify-content: flex-end; 24 list-style: none; 25 padding: 0; 26 27 > li { 28 margin-right: 20px; 29 } 30 } 31 } 32} 33// ナビゲーション・リンクホバー時 34/* 方法① シンプルに下線を表示させる場合 */ 35.header nav ul li a:hover { 36 text-decoration: underline; 37} 38 39// 店名ロゴ・スマホレイアウト 40.shop-logo-layout{ 41 @media screen and (max-width: 768px){ 42 display: flex; 43 justify-content: center; /* 横方向に中央揃え */ 44 flex: 1; /* スペースを均等に確保(必要に応じて) */ 45 } 46} 47.shop-logo-header { 48 @media screen and (max-width: 768px){ 49 display: block; 50 margin: 0 auto; 51 } 52} 53 54// ハンバーガーメニューボタン 55#bt-hamburger{ 56 display: none; 57 @media screen and (max-width: 768px){ 58 display: block; 59 position: fixed; 60 top: 15px; 61 left: 4.7%; 62 z-index: 90; 63 } 64} 65 66// ナビゲーション閉じるボタン 67#bt-close{ 68 display: none; 69 @media screen and (max-width: 768px){ 70 display: block; 71 position: fixed; 72 top: 15px; 73 left: 4.7%; 74 75 } 76} 77 78// ナビゲーション・SP用 79#nav-sp{ 80 display: none; 81 @media screen and (max-width: 768px){ 82 position: fixed; 83 top: 0; 84 left: 0; 85 background-color: #fff; 86 width: 100vw; /* 画面幅いっぱい */ 87 height: 100vh; 88 display: none; 89 z-index: 99; 90 } 91} 92 93// ナビゲーションリスト・SP用 94.nav-sp-list{ 95 display: none; 96 @media screen and (max-width: 768px){ 97 display: block; 98 padding-left: 4.7%; 99 padding-right: 4.7%; 100 padding-top: 150px; 101 102 >ul >li{ 103 padding: 20px 35.6% 20px 7.1%; 104 font-family: "Axiforma"; 105 font-size: 20px; 106 border-top-width: 1px; 107 border-top-color: #0047AB; 108 border-top-style: solid; 109 110 a:hover{ 111 text-decoration: underline; 112 } 113 } 114 } 115} 116 117.menu-sp-bottom{ 118 @media screen and (max-width: 768px){ 119 border-bottom-width: 1px; 120 border-bottom-color: #0047AB; 121 border-bottom-style: solid; 122 } 123} 124 125// 「在宅クリーニング」ホバー時に出現するメニューリスト 126.nav-more.active{ 127 display: block; 128 position: absolute; 129 top: 65px; 130 width: 85%; 131 height: 100vh; 132 background-color: #0047AB; 133 color: #FFFAFA; 134 z-index: 99; 135 border-radius: 10px; 136 padding: 50px; 137} 138 139.nav-more{ 140 display: none; 141} 142 143 144.list-top, .list-bottom{ 145 display: flex; 146 margin-bottom: 50px; 147 gap: 10%; 148} 149 150// >空室クリーニング 151.list-01-first, .list-03-first{ 152 margin-bottom: 50px; 153 >h3{ 154 color: #FFFAFA; 155 } 156 >a{ 157 display: block; 158 margin: 4px 0; /* 上下の間隔を少し空ける(お好みで) */ 159 } 160} 161 162// >エアコンのお掃除 163.list-01-second, .list-02, .list-03, .list-03-second, .list-04{ 164 display: block; 165 >h3{ 166 color: #FFFAFA; 167 } 168 >a{ 169 display: block; 170 margin: 4px 0; /* 上下の間隔を少し空ける(お好みで) */ 171 } 172} 173 174 175// ヘッダーここまで 176
jQuery
1$(document).ready(function(){ 2 3 // ヘッダーの読み込み 4 $(function(){ 5 $("#header-placeholder").load("../includes/header.html"); 6 }); 7 8 // フッターの読み込み 9 $(function(){ 10 $("#footer-placeholder").load("../includes/footer.html"); 11 }); 12 13 // トップ画像のカルーセル設定 14 $('.top-photo-carousel').slick({ 15 autoplay: true, 16 dots: true, 17 arrows: true, 18 infinite: true, 19 slidesToShow: 1, 20 slidesToScroll: 1, 21 speed: 700, 22 autoplaySpeed: 3000, 23 }); 24 25 // トップ・サービス一覧の切り替え表示 26 $('.bt-switching').click(function () { 27 // ボタンのアクティブ切り替え 28 $('.bt-switching').removeClass('active'); 29 $(this).addClass('active'); 30 31 // クリックされたボタンが何番目かを取得 32 const index = $('.bt-switching').index(this); 33 34 // service-listを切り替え 35 $('.service-list').removeClass('show').eq(index).addClass('show'); 36 }); 37 38 // トップ・「在宅クリーニング」ホバー時に切り替え表示させるメニューリスト 39 40 // 「在宅クリーニング」リンクと.nav-moreを取得 41 const $atHomeLink = $('nav ul li a[href="at-home.html"]'); 42 const $navMore = $('.nav-more'); 43 44 // ホバー時に.activeを追加 45 $atHomeLink.hover( 46 function () { 47 $navMore.addClass('active'); 48 }, 49 function () { 50 // 少し時間を置いて、.nav-more内にマウスがあるかを確認 51 setTimeout(function () { 52 if (!$navMore.is(':hover')) { 53 $navMore.removeClass('active'); 54 } 55 }, 100); 56 } 57 ); 58 59 // nav-more自体にもホバー判定を追加(ホバー中は表示を保つ) 60 $navMore.hover( 61 function () { 62 $navMore.addClass('active'); 63 }, 64 function () { 65 $navMore.removeClass('active'); 66 } 67 ); 68});
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
何も変化しなかった。
補足
特になし
