<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>スクロール アーカイブ - nyanblog ~にゃんぶろぐ~</title>
	<atom:link href="https://nyanblog2222.com/tag/%E3%82%B9%E3%82%AF%E3%83%AD%E3%83%BC%E3%83%AB/feed/" rel="self" type="application/rss+xml" />
	<link>https://nyanblog2222.com/tag/スクロール/</link>
	<description>ちょっとしたことを調べているよ</description>
	<lastBuildDate>Tue, 24 Aug 2021 23:43:42 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7</generator>

<image>
	<url>https://nyanblog2222.com/wp-content/uploads/2021/09/cropped-favicon-32x32.png</url>
	<title>スクロール アーカイブ - nyanblog ~にゃんぶろぐ~</title>
	<link>https://nyanblog2222.com/tag/スクロール/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>【JavaScript】スクロール中にヘッダーを固定する方法【簡単・コピペOK】</title>
		<link>https://nyanblog2222.com/programming/javascript/1630/</link>
					<comments>https://nyanblog2222.com/programming/javascript/1630/#respond</comments>
		
		<dc:creator><![CDATA[nyan2222]]></dc:creator>
		<pubDate>Sat, 04 Jul 2020 09:00:00 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[スクロール]]></category>
		<guid isPermaLink="false">https://nyanblog2222.com/?p=1630</guid>

					<description><![CDATA[<p><img src="https://nyanblog2222.com/wp-content/uploads/2022/11/73.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" fetchpriority="high" srcset="https://nyanblog2222.com/wp-content/uploads/2022/11/73.png 1280w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-300x157.png 300w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-1024x536.png 1024w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-768x402.png 768w" sizes="(max-width: 1280px) 100vw, 1280px" />こんにちは。ホームページを閲覧している中で、始めはヘッダーが固定されていなかったのに、途中から常に規定の位置で固定されているページをお見掛けしたことはありませんか？ 今回はスクロール中にヘッダーナビゲーションを固定させる [&#8230;]</p>
<p>投稿 <a href="https://nyanblog2222.com/programming/javascript/1630/">【JavaScript】スクロール中にヘッダーを固定する方法【簡単・コピペOK】</a> は <a href="https://nyanblog2222.com">nyanblog ~にゃんぶろぐ~</a> に最初に表示されました。</p>
]]></description>
										<content:encoded><![CDATA[<img src="https://nyanblog2222.com/wp-content/uploads/2022/11/73.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" srcset="https://nyanblog2222.com/wp-content/uploads/2022/11/73.png 1280w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-300x157.png 300w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-1024x536.png 1024w, https://nyanblog2222.com/wp-content/uploads/2022/11/73-768x402.png 768w" sizes="(max-width: 1280px) 100vw, 1280px" />
<p>こんにちは。ホームページを閲覧している中で、始めはヘッダーが固定されていなかったのに、途中から常に規定の位置で固定されているページをお見掛けしたことはありませんか？</p>



<p>今回はスクロール中にヘッダーナビゲーションを固定させる方法をご紹介していきたいと思います。</p>



<p>また、本記事のコードではJQueryを使用しています。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">HTML</a></li><li><a href="#toc2" tabindex="0">CSS</a></li><li><a href="#toc3" tabindex="0">JavaScript</a></li><li><a href="#toc4" tabindex="0">デモページ</a></li><li><a href="#toc5" tabindex="0">ひとこと</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">HTML</span></h2>



<p>HTMLでは、トップ画像とナビゲーション、サイトの長さを出すためにsection・footer要素を置いています。</p>



<pre class="wp-block-code html"><code>&lt;div id="top_visual">&lt;/div>
&lt;nav>
    &lt;div class="wrapper">
        &lt;ul>
            &lt;li class="item">&lt;a href="#news">News&lt;/a>&lt;/li>
            &lt;li class="item">&lt;a href="#about">About&lt;/a>&lt;/li>
            &lt;li class="item">&lt;a href="#blog">Blog&lt;/a>&lt;/li>
            &lt;li class="item">&lt;a href="#contact">Contact&lt;/a>&lt;/li>
        &lt;/ul>
    &lt;/div>
&lt;/nav>
&lt;section id="news">
    &lt;div class="wrapper">
        &lt;h1>News&lt;/h1>
        &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p>
    &lt;/div>
&lt;/section>
&lt;section id="about">
    &lt;div class="wrapper">
        &lt;h1>About&lt;/h1>
        &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p>
    &lt;/div>
&lt;/section>
&lt;section id="blog">
    &lt;div class="wrapper">
        &lt;h1>Blog&lt;/h1>
        &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p>
    &lt;/div>
&lt;/section>
&lt;section id="contact">
    &lt;div class="wrapper">
        &lt;h1>Contact&lt;/h1>
        &lt;p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p>
    &lt;/div>
&lt;/section>
&lt;footer>
    &lt;p>© 2020 nyanblog&lt;/p>
&lt;/footer></code></pre>



<h2 class="wp-block-heading"><span id="toc2">CSS</span></h2>



<p>CSSで見た目を整えていきます。<br>また、JavaScriptでつけ外しをする「nav.fixed」でヘッダーを固定しています。</p>



<pre class="wp-block-code css"><code>@charset "utf-8";

*{
    margin: 0;
}

#top_visual {
    background: url(../img/visual.jpg) center center / cover no-repeat;
    height: 480px;
}

nav {
    background: #fff;
    width: 100%;
}

nav.fix {
    position: fixed;
    right: 0;
    top: 0;
}

.wrapper {
    margin: 0 auto;
    max-width: 100%;
}

nav ul {
    justify-content: flex-end;
    display: flex;
    list-style: none;

}

.item {
    margin: 0 2em;
    padding: 17px 0;
}

.item a {
    color: #B78D4A;
    text-decoration: none;
}

.item a:hover{
    color: #D5BC94;
}

h1 {
    font-size: 1.25rem;
    padding: 2rem 0;
    color: #B78D4A;
    font-weight: normal;
}

p {
    line-height: 2;
    padding-bottom: 4rem;
    color: #555A60;
}

section{
    background-color: #F0F0F0;
    padding: 2em;

}

section .wrapper{
    max-width: 800px;
}

footer {
    background-color: #B78D4A;
    padding: 2em;
}

footer p {
    font-size: .8em;
    color: #fff;
    max-width: 800px;
    padding: 0;
}</code></pre>



<h2 class="wp-block-heading"><span id="toc3">JavaScript</span></h2>



<p>JavaScriptでスクロールイベントするたびに、<strong><span style="color:#b78d4a" class="has-inline-color">スクロールの位置とnav要素の位置と比較</span></strong>しています。<br>スクロールの位置を超えると、<strong><span style="color:#b78d4a" class="has-inline-color">nav要素に「fix」クラスを付与</span></strong>し、上部に固定させます。</p>



<pre class="wp-block-code javascript"><code>$(function() {
    var pos = $("nav").offset().top;
    var height = $("nav").outerHeight();
    $(window).scroll(function () {
        if ($(this).scrollTop() > pos) {
            $("nav").addClass("fix");
            $("body").css("padding-top", height);
        } else {
            $("nav").removeClass("fix");
            $("body").css("padding-top", 0);
        }
    });
});</code></pre>



<h2 class="wp-block-heading"><span id="toc4">デモページ</span></h2>



<p><strong><a class="link" href="https://nyanblog2222.com/demo/scroll_event/" target="_blank" aria-label="undefined (opens in a new tab)">デモページはこちら</a></strong>です。<br>実際にスクロールで動かしてヘッダーが固定されることを確認してみてください。</p>



<h2 class="wp-block-heading"><span id="toc5">ひとこと</span></h2>



<p>いかがでしたでしょうか。<br>今回はスクロールイベントを利用して、ヘッダーナビゲーションを固定させる方法をご紹介しました。</p>



<p>簡単にコピペで実装することができますので、良かったら試してみてください。<br>本記事をご覧くださり、ありがとうございました！</p>
<p>投稿 <a href="https://nyanblog2222.com/programming/javascript/1630/">【JavaScript】スクロール中にヘッダーを固定する方法【簡単・コピペOK】</a> は <a href="https://nyanblog2222.com">nyanblog ~にゃんぶろぐ~</a> に最初に表示されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nyanblog2222.com/programming/javascript/1630/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
