【jQuery】グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法
今回は、「グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法」について紹介します。
コピペして使い回すことが出来るので、是非参考にしてください。
目次
【jQuery】グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法
完成形はこちらです。
See the Pen
Untitled by webis (@webis-co)
on CodePen.
ページ内リンクをクリックしたらメニューが閉じられて、対象のセクションまで自動スクロールするのが分かるでしょう。
HTML
<!-- ハンバーガーボタン -->
<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
// ハンバーガーメニュー
#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タグの直前に以下のコードを記述してください。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
そしてその他のjQueryコードはこちら。
// ハンバーガーメニュ
(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");
});
});
まとめ
以上が、「グローバルメニューのページ内リンクをクリックしたらメニューを閉じて対象の箇所まで自動スクロールさせる方法」でした。




