【jQuery】グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法

今回は、「グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法」について紹介します。
コピペして使い回すことが出来るので、是非参考にしてください。
【jQuery】グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法
完成形はこちらです。
See the Pen
Untitled by webis (@webis-co)
on CodePen.
ページ内リンクをクリックしたらメニューが閉じられて、対象のセクションまで自動スクロールするのが分かるでしょう。
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!-- ハンバーガーボタン --> <div id="nav-toggle"> <div> <span></span> <span></span> <span></span> </div> </div> <!--グローバルナビ--> <nav id="gloval-nav"> <ul> <li><a href="#section1">セクション1</a></li> <li><a href="#section2">セクション2</a></li> <li><a href="#section3">セクション3</a></li> <li><a href="#section4">セクション4</a></li> </ul> </nav> <!-- コンテンツ --> <div class="container"> <section class="section" id="section1"> <span>セクション1</span> <p>テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。</p> </section> <section class="section" id="section2"> <span>セクション2</span> <p>テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。</p> </section> <section class="section" id="section3"> <span>セクション3</span> <p>テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。</p> </section> <section class="section" id="section4"> <span>セクション4</span> <p>テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。テキストが入ります。</p> </section> </div>は |
ハンバーガーメニューとグローバルナビ、そしてコンテンツを用意します。
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
// ハンバーガーメニュー #nav-toggle { position: fixed; top: 20px; right: 20px; height: 20px; width: 20px; cursor: pointer; > div { position: relative; width: 20px; } span { width: 100%; height: 1px; left: 0; display: block; background: black; position: absolute; transition: transform .6s ease-in-out, top .5s ease; &:nth-child(1) { top: 0; } &:nth-child(2) { top: 7px; } &:nth-child(3) { top: 14px; } } } .open { #nav-toggle span { background: #fff; &:nth-child(1) { top: 7px; transform: rotate(45deg); } &:nth-child(2) { top: 14px; width: 0; left: 50%; } &:nth-child(3) { top: 7px; transform: rotate(-45deg); } } } #gloval-nav { background-color: rgba(0, 0, 0, 0.7); position: fixed; top: 0; left: 0; right: 0; bottom: 0; text-align: center; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 20px; transform: translateX(-100%); transition: opacity .6s ease, visibility .6s ease; } #gloval-nav { a { display: block; color: #fff; padding: 20px 0; transition: color .6s ease; } ul { li { opacity: 0; transform: translateX(-200px); transition: transform .6s ease, opacity .2s ease; &:nth-child(2) { transition-delay: .15s; } &:nth-child(3) { transition-delay: .3s; } &:nth-child(4) { transition-delay: .45s; } } } } /* open */ .open { overflow: hidden; #gloval-nav { visibility: visible; transform: translateX(0); transition: transform .6s; } #gloval-nav li { opacity: 1; transform: translateX(0); transition: transform 1s ease, opacity .9s ease; } } // z-index #nav-toggle { z-index: 100; } #gloval-nav { z-index: 10; } // コンテンツ .section { padding: 70px 20px; height: 100vh; text-align: center; span { font-weight: bold; font-size: 30px; } p { font-size: 18px; line-height: 1.8; } &:first-child { background-color: red; } &:nth-child(2) { background-color: greenyellow; } &:nth-child(3) { background-color: hotpink; } &:nth-child(4) { background-color: lightcoral; } } |
jQuery
jQueryを使うために、まずはbodyタグの直前に以下のコードを記述してください。
1 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
そしてその他のjQueryコードはこちら。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// ハンバーガーメニュ (function ($) { $(function () { $('#nav-toggle').on('click', function () { $('body').toggleClass('open'); }); }); })(jQuery); // スクロール $('a[href^="#"]').click(function () { var speed = 400; var href = $(this).attr("href"); var target = $(href == "#" || href == "" ? 'html' : href); var position = target.offset().top; $('body,html').animate({ scrollTop: position }, speed, 'swing'); return false; }); // グローバルナビメニューのリンクをクリックしたらページを閉じる $(function () { $("#gloval-nav ul li a").on("click", function () { $("#gloval-nav ul").toggleClass(); $("body").removeClass("open"); }); }); |
まとめ
以上が、「グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法」でした。