実現したいこと
HTML、CSS初心者ですが、グローバルナビゲーションをレスポンシブ対応のドロップダウン形式にしたいです。
発生している問題・分からないこと
JavaScriptやCSSの設定がよくわかりません
ドロップダウンが表示されません
該当のソースコード
HTML <!-- グローバルナビゲーション --> <div class="navigation"> <input id="navi-checkbox" type="checkbox"> <nav class="navi-menu"> <label class="hamburger-btn" for="navi-checkbox" aria-label="open navigation"> <span class="hamburger-bar"></span> </label> <ul class="navi-list"> <li class="navi-item"> <a class="navi-item-anchor" href="<%url>">HOME</a> </li> <li class="navi-item"> <a class="navi-item-anchor" href="<%url>archives.html">INDEX</a> </li> <li class="navi-item"> <a class="navi-item-anchor" href="<%server_url>control.php">LOG IN</a> </li> <li class="navi-item navi-item-modal"> <label class="navi-item-anchor search_modal-trigger-label" for="search_modal-trigger"><svg class="svg-fill" role="img" aria-hidden="true"><use xlink:href="#svg-search-fill"></use></svg></label> </li> </ul> </nav> </div> CSS /***========================================== global navigation ==========================================***/ .navigation { position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; background: var(--navi-bgcolor); color: var(--navi-color); font-size: var(--navi-font_size); transition: .7s ease-in-out; } .searchBarfound .navigation { top: 27px; } .navi-menu { display: flex; flex-flow: column; align-items: center; height: var(--navi-height); margin: auto; overflow: hidden; transition: height .6s ease-in-out; } .navi-list { flex: 1; width: 100%; min-height: 0; overflow-y: auto; text-align: center; } .navi-list::-webkit-scrollbar { display: none; } .navi-item { position: relative; margin: 0 0 10px; } .navi-item:first-of-type { margin-top: 30px; } .navi-item-anchor { display: block; padding: 1em 0; color: var(--navi-color); cursor: pointer; } /* hamburger */ .hamburger-btn { flex-shrink: 0; display: flex; flex-flow: column; align-items: center; justify-content: center; width: var(--navi-height); height: var(--navi-height); margin: 0 auto; color: currentColor; cursor: pointer; } .hamburger-bar { display: block; height: 2px; width: 28px; margin: 5px; background: currentColor; transition: background .6s; } .hamburger-btn::before, .hamburger-btn::after { content: ""; display: block; width: 28px; height: 2px; background: currentColor; transition: transform .6s; } #navi-checkbox { display: none; } #navi-checkbox:checked + .navi-menu .hamburger-btn .hamburger-bar { background: transparent; } #navi-checkbox:checked + .navi-menu .hamburger-btn::before { transform: translateY(7px) rotate(225deg); } #navi-checkbox:checked + .navi-menu .hamburger-btn::after { transform: translateY(-7px) rotate(-225deg); } #navi-checkbox:checked ~ .navi-menu { height: var(--navi-accordion-height); overflow: visible; } @media screen and (min-width: 768px) { .hamburger-btn { display: none; } #navi-checkbox:checked + .navi-menu { height: initial; } .navigation { position: static; width: initial; background: transparent; } .navi-menu { margin: 0 auto; } .navi-list { flex: initial; display: flex; justify-content: flex-end; align-items: center; height: var(--navi-height); overflow: hidden; background: transparent; } .navi-item { height: 100%; margin: 0 0 0 10px; } .navi-item:first-of-type { margin: 0; } .navi-item-anchor { display: flex; justify-content: center; align-items: center; height: 100%; padding: 0 1.8em; } } .sticky_active { box-shadow: 0 2px 4px -1px rgba(0,0,0,.06), 0 4px 5px 0 rgba(0,0,0,.06), 0 1px 10px 0 rgba(0,0,0,.08); background: var(--navi-bgcolor); } @media screen and (min-width: 768px) { .sticky_active { display: flex; justify-content: flex-end; position: fixed; top: 0; left: 0; width: 100%; } .sticky_active .navi-menu { width: var(--width-whole_pct); max-width: var(--width-whole); } } /***========================================== header + navigation ==========================================***/ .margin-block { width: 100%; height: calc(var(--navi-height) + var(--header-margin-vertical-sp)); } .header-banner { width: var(--width-whole_pct); max-width: var(--width-whole); margin: 0 auto; overflow: hidden; } @media screen and (min-width: 768px) { .margin-block { height: var(--header-margin-vertical-tb); } .header-banner { display: flex; justify-content: space-between; align-items: center; } } @media screen and (min-width: 1100px) { .margin-block { height: var(--header-margin-vertical-pc); } } .blog-title { flex: 1; min-width: 0; font-weight: normal; font-size: clamp(var(--blog_title-font_size-min), 2.5vw, var(--blog_title-font_size-max)); line-height: var(--line_height-title); font-family: var(--font_family-latin); } .blog-title-anchor { color: var(--color-base); } .site-description, .breadcrumbs-prewrap { width: var(--width-whole_pct); max-width: var(--width-whole); margin: 15px auto 0; font-size: var(--sub_title-font_size); }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ChatGPTでHTML、CSSを変更、JavaScriptを追加しましたが、マウスホバーしてもドロップダウンが表示されませんでした
補足
FC2ブログのSwollenというテンプレートを使用しています。
ChatGPTでのCSSの変更内容、JavaScriptを記載します。
文字数がオーバーするのでHTMLは省きました。
CSS
/==========================================
global navigation
==========================================/
/* ナビゲーションを右寄せ */
.navigation {
display: flex;
justify-content: flex-end;
align-items: center;
}
/* ナビゲーションのメニュー */
.navi-menu {
display: flex;
align-items: center;
}
/* ナビゲーションのリスト */
.navi-list {
display: flex;
list-style: none;
padding: 0;
margin: 0;
}
/* 各ナビゲーション項目 */
.navi-item {
position: relative;
padding: 10px 15px;
min-width: 100px;
}
/* ナビゲーションのリンク */
.navi-item-anchor {
font-size: 14px;
text-decoration: none;
display: block;
text-align: center;
padding: 5px 10px;
}
/* 画像のサイズ調整 */
.navi-item img {
width: 40px;
height: auto;
vertical-align: middle;
}
/* チェックボックス(四角いボックス)を非表示 */
#navi-checkbox {
display: none;
}
/* ======= Sitemap ドロップダウン対応 ======= */
/* Sitemapの親要素 */
.navi-dropdown {
position: relative;
}
/* Sitemapのリンク */
.navi-dropdown .dropdown-toggle {
cursor: pointer;
display: block;
padding: 5px 10px;
}
/* ドロップダウンメニュー */
.navi-dropdown-menu {
opacity: 0;
visibility: hidden;
position: absolute;
right: 0;
top: 100%;
background: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
list-style: none;
padding: 5px 0;
width: 150px;
z-index: 1000;
transition: opacity 0.3s ease, visibility 0.3s ease;
}
/* マウスホバー時にドロップダウンを表示 */
.navi-dropdown:hover .navi-dropdown-menu {
opacity: 1;
visibility: visible;
}
/* ドロップダウン内の各メニュー */
.navi-dropdown-menu li {
padding: 5px 10px;
}
.navi-dropdown-menu li a {
font-size: 14px;
text-decoration: none;
display: block;
text-align: left;
padding: 8px 12px;
white-space: nowrap;
}
/* ホバー時の背景色 */
.navi-dropdown-menu li a:hover {
background: lightgray;
}
/==========================================
header + navigation
==========================================/
の部分のCSSは変更していません。
JavaScript
<script> document.addEventListener("DOMContentLoaded", function () { let dropdown = document.querySelector(".navi-dropdown"); let dropdownMenu = document.querySelector(".navi-dropdown-menu"); dropdown.addEventListener("mouseenter", function () { dropdownMenu.style.opacity = "1"; dropdownMenu.style.visibility = "visible"; }); dropdown.addEventListener("mouseleave", function () { dropdownMenu.style.opacity = "0"; dropdownMenu.style.visibility = "hidden"; }); }); </script>
回答2件
あなたの回答
tips
プレビュー