私のスマホに問題があっただけでした…。
前提
ページ内リンクをクリックするとハンバーガーメニューを閉じるようにしたが…スマホのときは…閉じない
実現したいこと
スマホでも、ページ内リンクをクリックするとハンバーガーメニューを閉じるようにしたいです。
発生している問題・エラーメッセージ
PCのRWDでは、しっかり閉じるように出来ているのですが…スマホでは、うまく閉じないです。HOME,CONTACTは問題ないのですが…WORKS,ABOUT,SERVICE,PRICEのようなid移動だとうまく閉じません。
もしかしたら…私のスマホだけかもしれません…。
問題のサイトです。https://yamato11.com/
エラーメッセージ
該当のソースコード
HTML
1<div class="menu_btn"> 2 <div class="burger"> 3 <span class="top"></span> 4 <span class="middle"></span> 5 <span class="bottom"></span> 6 </div> 7 </div> 8 9 10 11 <div class="menu"> 12 <ul class="menu_item_open "> 13 <li class=""><a href="<?php bloginfo("url"); ?>">HOME</a></li> 14 <li class=""><a href="<?php bloginfo("url"); ?>/#popular_post_content">WORKS</a></li> 15 <li class=""><a href="<?php bloginfo("url"); ?>/#content">ABOUT</a></li> 16 <li class=""><a href="<?php bloginfo("url"); ?>/#front-service">SERVICE</a></li> 17 <li class=""><a href="<?php bloginfo("url"); ?>/#front-company">PRICE</a></li> 18 <li class=""><a href="<?php bloginfo("url"); ?>/contact">CONTACT</a></li> 19 </ul> 20 </div>
CSS
1@media (max-width: 960px){ 2 3 .right nav .menu_item { 4 display: none; 5 6 } 7/* バーガー */ 8.menu_btn { 9 display: inherit; 10 z-index: 20000; 11 position: fixed; 12 top: 0px; 13 right: 31px; 14} 15 16.burger { 17 width: 22px; 18 height: 22px; 19 display: block; 20 cursor: pointer; 21 position: absolute; 22} 23 24.burger .top { 25 transform: translateY(-7px); 26} 27.active .top { 28 transform: rotate(-495deg); 29 animation-name: rotate-top; 30} 31 32.active span.middle { 33 background: rgba(51,51,51,0); 34} 35 36.burger .bottom { 37 transform: translateY(7px); 38} 39 40.active .bottom { 41 transform: rotate(495deg); 42 animation-name: rotate-bottom; 43} 44 45.burger span { 46 width: 22px; 47 height: 2px; 48 display: block; 49 background: #fff; 50 position: absolute; 51 left: 25%; 52 top: 50%; 53 transition: transform 0.3s; 54 transform: rotate(0deg); 55} 56 57.right{ 58 text-align: center; 59 justify-content: center; 60} 61 62.burger{ 63 width: 40px; 64 height: 40px; 65 cursor: pointer; 66 border-radius: 4px; 67 position: relative; 68 z-index: 2; 69 margin-top: 10px; 70 margin-bottom: auto; 71} 72.burger:hover{ 73 background-color: #ddd; 74} 75 76.menu{ 77 color: white; 78 display: flex; 79 align-items: center; 80 justify-content: center; 81 width: 50vw; 82 margin-right: 0; 83 height: 100vh; 84 background-color: #000; 85 position: fixed; 86 top: 0; 87 left: 0; 88 z-index: 1; 89} 90 91.menu{ 92 transform: translateX(-100vw); 93 transition: all .3s linear; 94 95} 96 97.menu.is-active{ 98 transform: translateX(0); 99} 100 101.menu_item_open{ 102 text-align: left; 103 display: block; 104 width: 100%; 105 margin-top: -10px; 106} 107 108.menu_item_open li{ 109display: block; 110text-align: left; 111} 112 113nav .gmenu ul li a { 114 padding: 15px 0px 15px 25%; 115 color: #fff; 116 display: block; 117 width: 100%; 118 -o-box-sizing: border-box; 119 -ms-box-sizing: border-box; 120 box-sizing: border-box; 121}
jQuery
1$(function() { 2 $('.burger').click(function() { 3 $(this).toggleClass('active'); 4 5 if ($(this).hasClass('active')) { 6 $('.menu').addClass('is-active'); 7 } else { 8 $('.menu').removeClass('is-active'); 9 } 10 }); 11}); 12 13 14$(function() { 15 $('.menu a').click(function() { 16 $('.menu').removeClass('is-active'); 17 $('.burger').removeClass('active'); 18 }); 19});
試したこと
別パターンでの実装もしたのですが同じ結果でした…。
pcとスマホでは、サイトの読み込み方がことなるということだったので…$(window).on('load', function() {}を試しましたが…うまくいきませんでした…。
jQuery
$('.burger').click(function(){ $('.burger').toggleClass('active'); $('.menu').toggleClass('is-active'); }); $(window).on('load', function() { $('.menu li a').click(function(){ $('.burger').removeClass('active'); $('.menu').removeClass('is-active'); }); });
回答1件
あなたの回答
tips
プレビュー