ハンバーガーメニューを設置しており、一部ページ内リンクとスムーススクロールを設定しています。
ハンバーガーメニュー内のリンクをクリックするとハンバーガーメニューが閉じるようにしたいと考えています。
ハンバーガーメニュー内のリンクをクリックすると該当箇所まで移動はしますが、ハンバーガーメニューが閉じずに開いたままです。
以下、CSSのみ一部抜粋して記載しています。
HTML
1<div id="nav"> 2<ul> 3<li><a href="#info">INFORMATION</a></li> 4<li><a href="URL"> BOOKSHELF</a></li> 5<li><a href="#bkm"">BOOKMARK</a></li> 6<li><a href="#mail">CONTACT</a></li><li> 7<a href="URL">HOME</a></li> 8</ul></div> 9<a class="menu" href="#"><span></span><span></span><span></span></a> 10<header><h1>タイトル</h1></header><div id="all"><article><section id="info"><h2>タイトル</h2>中身</section><section id="news"><h2>タイトル</h2>中身</section><section id="bkm"><h2>タイトル</h2>中身</section> 11<section id="mail"><h2>タイトル</h2><br> 12中身</section><div class="footer">中身</div><p class="pagetop"><a href="#"><i class="fas fa-angle-double-up"></i></a></p></div></article></div>
CSS
1 2.menu, 3.menu span { 4display: inline-block; 5-webkit-transition:all .4s; 6transition: all .4s; 7box-sizing: border-box; 8} 9 10.menu { 11position: fixed; 12top: 10px; 13right: 12px; 14width: 50px; 15height: 40px; 16margin:6px auto; 17padding:6px auto; 18z-index: 9999; 19} 20 21.menu span { 22position: absolute; 23left: 12px; 24width: 70%; 25height: 1px; 26background-color: #000; 27z-index: 9999; 28} 29 30.menu span:nth-of-type(1) { 31top: 0px; 32} 33 34.menu span:nth-of-type(2) { 35top: 18px; 36} 37 38.menu span:nth-of-type(3) { 39bottom: 0px; 40} 41 42.menu.active span:nth-of-type(1) { 43-webkit-transform: translateY(20px) rotate(-45deg); 44transform: translateY(20px) rotate(-45deg); 45} 46 47.menu.active span:nth-of-type(2) { 48opacity: 0; 49} 50 51.menu.active span:nth-of-type(3) { 52-webkit-transform: translateY(-20px) rotate(45deg); 53transform: translateY(-20px) rotate(45deg); 54} 55 56#nav { 57position: fixed; 58top: 0; 59right: 0; 60z-index: 9999; 61width: 80vw; 62height: 100vh; 63background: rgba(240,255,240,1); 64opacity: 0; 65-webkit-transition: .5s linear; 66-moz-transition: .5s linear; 67transition: .5s linear; 68-moz-transform: translateX(100vw); 69-webkit-transform: translateX(100vw); 70transform: translateX(100vw); 71} 72 73#nav.active{ 74right: 0; 75opacity: 1; 76-moz-transform: translateX(0); 77-webkit-transform: translateX(0); 78transform: translateX(0); 79} 80 81#nav ul{ 82margin:20px 0; 83padding:20px 0; 84} 85 86#nav ul li{ 87list-style-type: none; 88} 89 90#nav ul li a{ 91font-family:'Nunito',"メイリオ", sans-serif; 92display: block; 93text-align: center; 94text-decoration: none; 95color: #003853; 96font-size: 18px; 97font-weight:700; 98margin:20px 0; 99padding:10px 0; 100} 101 102body{ 103margin:0; 104padding:0; 105background:url(); 106background-position:center; 107background-repeat:no-repeat; 108background-attachment:fixed; 109background-size:cover; 110letter-spacing:2.4px; 111line-height:1.5; 112} 113 114header{ 115width:100%; 116height:60px; 117top:0; 118padding:20px 0; 119text-align:center; 120position:fixed; 121z-index:1; 122background-color:rgba(248,254,243,1); 123} 124 125section{ 126background:rgba(255,255,255, .8); 127display:block; 128font-family:'Nunito',"メイリオ", sans-serif; 129font-size:14px; 130color: #003853; 131text-align:center; 132margin:10px auto; 133padding:10px auto; 134} 135 136.pagetop { 137display: none; 138position: fixed; 139bottom: 30px; 140right: 50px; 141} 142 143.pagetop a { 144display: block; 145width: 50px; 146height: 50px; 147background-color: #333; 148border-radius: 50px; 149text-align: center; 150color: #fff; 151font-size: 24px; 152text-decoration: none; 153line-height: 50px; 154}
jQuery
1<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 2<script type="text/javascript"> 3 $(function(){ 4 $('.menu').on('click', function() { 5 $(this).toggleClass('active'); 6 $("#nav").toggleClass('active'); 7 }) 8 }); 9 10$(function() { 11 var pagetop = $('.pagetop'); 12 $(window).scroll(function () { 13 if ($(this).scrollTop() > 80) { 14 pagetop.fadeIn(); 15 } else { 16 pagetop.fadeOut(); 17 } 18 }); 19 pagetop.click(function () { 20 $('body, html').animate({ scrollTop: 0 }, 600); 21 return false; 22 }); 23}); 24 25$(function(){ 26 $('a[href^="#"]').click(function() { 27 var speed = 600; 28 var href= $(this).attr("href"); 29 var target = $(href == "#" || href == "" ? 'html' : href); 30 var position = target.offset().top; 31 $('body,html').animate({scrollTop:position}, speed, 'swing'); 32 return false; 33 }); 34}); 35</script>
似たような質問を見たり調べたりして、様々なコードを試してみましたが、ハンバーガーメニュー自体が動かなくなったり、メニューは開けても三本線のアイコンが消えたりしました。
初心者の為、すみませんが教えて下さい。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/26 12:07