実現したいこと
コーディング初心者です。恐れ入ります。ご鞭撻のほど、賜れれば幸いです。
ページが読み込まれた際に「タブメニューが全て閉じている」というのが現状です。
これを「ページが読み込まれた際に『最初のタブメニューは開いた状態」」にしたいです。
発生している問題・分からないこと
jQueryで「.on('load'」時にaddclassがされるように書いたつもりですが・・・・機能してないようで。
解決方法が分かり兼ねております。
恐れ入ります。私の見解誤り・確認不足だったようで、「.on('load'」は問題なく機能しておりました。
タブメニューの前にdivが入ると、ページが読み込まれた際に「タブメニューが全て閉じてしまっている」というのが現状でした。これを「ページが読み込まれた際に『最初のタブメニューは開いた状態』」にしたいです。
※下記の該当のソースコードを「『nekora様が保管くださったコード』に当該div部分(<div class="price-title-container">)を加えたコード」に変更させて頂いております。
[文字数の関係で、影響がないとわかったスライダー・ハンバーガーメニューのjsは割愛致しております。]
該当のソースコード
HTML
1<!DOCTYPE html> 2<html lang="jp"> 3 <head> 4 <title>タブメニュー初期表示時オープン</title> 5 <script src="https://kit.fontawesome.com/26c6772bd8.js" crossorigin="anonymous"></script> 6 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> 7 <!-- スクロールしたら文字色が変わる --> 8 <script src="js/midnight.jquery.js"></script> 9 <!-- タブメニュー --> 10 <script src="js/tab-menu.js"></script> 11 <!-- スクロールしたら文字色が変わる --> 12 <script> 13 $(document).on('ready', function() { 14 $('.change-color').midnight(); 15 }); 16 </script> 17 18 <style> 19 .price { 20 height: 100vh; 21 width: 100%; 22 } 23 .price .price-title-container { 24 writing-mode: vertical-rl; 25 display: flex; 26 margin: 0 auto; 27 padding-top: 76px; 28 } 29 .price .price-title-container .main-title { 30 font-size: 1.5rem; 31 font-weight: bold; 32 letter-spacing: 0.3rem; 33 margin: 0; 34 color: rgba(255, 255, 255, 0.7); 35 text-shadow: 0px 0px 8px #000, 0px 0px 8px #000; 36 } 37 .price .price-title-container .sub-title { 38 font-size: 1.5rem; 39 text-combine-upright: all; 40 margin: 0; 41 padding-top: 0.5rem; 42 color: rgba(255, 255, 255, 0.7); 43 text-shadow: 0px 0px 8px #000, 0px 0px 8px #000; 44 } 45 46 /* ーーータブメニュー ーーーー*/ 47 /*tabの形状*/ 48 .tab{ 49 display: flex; 50 flex-wrap: wrap; 51 font-family: "shinryu", sans-serif; 52 font-weight: 400; 53 font-style: normal; 54 margin-left: 40px; 55 } 56 .tab li a{ 57 display: block; 58 background:#ddd; 59 margin:0 2px; 60 padding:10px 20px; 61 } 62 /*liにactiveクラスがついた時の形状*/ 63 .tab li.active a{ 64 background:#fff; 65 } 66 /*エリアの表示非表示と形状*/ 67 .area { 68 display: none; 69 opacity: 0; 70 background: #fff; 71 margin-left: 40px; 72 padding:50px 20px; 73 font-family: "shinryu", sans-serif; 74 font-weight: 400; 75 font-style: normal; 76 } 77 /*areaにis-activeというクラスがついた時の形状*/ 78 .area.is-active { 79 display: block; 80 animation-name: displayAnime; 81 animation-duration: 2s; 82 animation-fill-mode: forwards; 83 } 84 85 @keyframes displayAnime{ 86 from { 87 opacity: 0; 88 } 89 to { 90 opacity: 1; 91 } 92 } 93 /* ーーータブメニュー終了ーーーーーー*/ 94 </style> 95 </head> 96 97 <body> 98 <section class="price scroll-area" data-midnight="shadow"> 99 <div class="price-title-container"> 100 <p class="main-title">料金</p> 101 <p class="sub-title">price</p> 102 </div> 103 <ul class="tab"> 104 <li><a href="#menber2">2名様</a></li> 105 <li><a href="#menber3">3名様</a></li> 106 <li><a href="#menber4">4名様</a></li> 107 <li><a href="#menber5">5名様</a></li> 108 <li><a href="#menber6">6名様</a></li> 109 </ul> 110 <div id="menber2" class="area"> 111 <table> 112 <tr> 113 <th>紅葉</th> 114 <th>水面</th> 115 <th>音色</th> 116 <th>紅葉茶</th> 117 <th>滝の音</th> 118 <th>雪花</th> 119 <th>囲炉裏</th> 120 <th>太平</th> 121 </tr> 122 <tr> 123 <th>38,500円</th> 124 <th>41,800円</th> 125 <th>38,500円</th> 126 <th>33,000円</th> 127 <th>41,800円</th> 128 <th>41,800円</th> 129 <th>36,300円</th> 130 <th>36,300円</th> 131 </tr> 132 </table> 133 </div> 134 <div id="menber3" class="area"> 135 <table> 136 <tr> 137 <th>紅葉</th> 138 <th>水面</th> 139 <th>音色</th> 140 <th>紅葉茶</th> 141 <th>滝の音</th> 142 <th>雪花</th> 143 <th>囲炉裏</th> 144 <th>太平</th> 145 </tr> 146 <tr> 147 <th>36,300円</th> 148 <th>39,300円</th> 149 <th>36,300円</th> 150 <th>30,800円</th> 151 <th>39,600円</th> 152 <th>39,600円</th> 153 <th>34,100円</th> 154 <th>34,100円</th> 155 </tr> 156 </table> 157 </div> 158 <div id="menber4" class="area"> 159 <table> 160 <tr> 161 <th>紅葉</th> 162 <th>水面</th> 163 <th>音色</th> 164 <th>紅葉茶</th> 165 <th>滝の音</th> 166 <th>雪花</th> 167 <th>囲炉裏</th> 168 <th>太平</th> 169 </tr> 170 <tr> 171 <th>34,100円</th> 172 <th>37,400円</th> 173 <th>34,100円</th> 174 <th>28,600円</th> 175 <th>37,400円</th> 176 <th>37,400円</th> 177 <th>31,900円</th> 178 <th>31,900円</th> 179 </tr> 180 </table> 181 </div> 182 <div id="menber5" class="area"> 183 <table> 184 <tr> 185 <th>紅葉</th> 186 <th>水面</th> 187 <th>音色</th> 188 <th>紅葉茶</th> 189 <th>滝の音</th> 190 <th>雪花</th> 191 <th>囲炉裏</th> 192 <th>太平</th> 193 </tr> 194 <tr> 195 <th>31,900円</th> 196 <th>35,200円</th> 197 <th>31,900円</th> 198 <th>-</th> 199 <th>35,200円</th> 200 <th>35,200円</th> 201 <th>-</th> 202 <th>-</th> 203 </tr> 204 </table> 205 </div> 206 <div id="menber6" class="area"> 207 <table> 208 <tr> 209 <th>紅葉</th> 210 <th>水面</th> 211 <th>音色</th> 212 <th>紅葉茶</th> 213 <th>滝の音</th> 214 <th>雪花</th> 215 <th>囲炉裏</th> 216 <th>太平</th> 217 </tr> 218 <tr> 219 <th>-</th> 220 <th>33,000円</th> 221 <th>-</th> 222 <th>-</th> 223 <th>33,000円</th> 224 <th>33,000円</th> 225 <th>-</th> 226 <th>-</th> 227 </tr> 228 </table> 229 </div> 230 </section> 231 232 <script> 233 //任意のタブにURLからリンクするための設定 234 function GethashID (hashIDName){ 235 if(hashIDName){ 236 $('.tab li').find('a').each(function() { 237 var idName = $(this).attr('href'); 238 if(idName == hashIDName){ 239 var parentElm = $(this).parent(); 240 $('.tab li').removeClass("active"); 241 $(parentElm).addClass("active"); 242 //表示させるエリア設定 243 $(".area").removeClass("is-active"); 244 $(hashIDName).addClass("is-active"); 245 } 246 }); 247 } 248 } 249 250 //タブをクリックしたら 251 $('.tab a').on('click', function() { 252 var idName = $(this).attr('href'); 253 GethashID (idName); 254 return false; 255 }); 256 257 // 上記の動きをページが読み込まれたらすぐに動かす 258 $(window).on('load', function () { 259 $('.tab li:first-of-type').addClass("active"); 260 $('.area:first-of-type').addClass("is-active"); 261 var hashName = location.hash; 262 GethashID (hashName); 263 }); 264 </script> 265 </body> 266</html>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
teratailで「タブメニュー」や「タブメニュー 開いたまま」や「タブメニュー ロード」で検索しましたが、同様事例を見つけられませんでした。
補足
特になし

回答2件
あなたの回答
tips
プレビュー