{"id":10571,"date":"2022-12-26T15:10:00","date_gmt":"2022-12-26T06:10:00","guid":{"rendered":"https:\/\/hanami-web.tokyo.jp\/blog\/?p=10571"},"modified":"2022-11-29T08:11:16","modified_gmt":"2022-11-28T23:11:16","slug":"fix-header-samplecode","status":"publish","type":"post","link":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/","title":{"rendered":"Sample code for making the header fixed when scrolling"},"content":{"rendered":"<p>A memo on how to implement a fixed header using html\/css\/js<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">table of contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%ae%e4%bd%9c%e3%82%8b%e6%96%b9%e6%a6%82%e8%a6%81\" >How to create a fixed header<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%aehtml%e3%82%b3%e3%83%bc%e3%83%89\" >Header HTML Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%ab%e3%81%99%e3%82%8bjquery%e3%82%b3%e3%83%bc%e3%83%89\" >jQuery code for fixed header<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#css%e3%81%ae%e8%a8%ad%e5%ae%9a%e6%96%b9%e6%b3%95\" >How to set up CSS<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%95%e3%82%8c%e3%81%a6%e3%81%84%e3%81%aa%e3%81%84%e6%99%82%e3%81%aehtml\" >HTML when not scrolled<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%95%e3%82%8c%e3%81%9f%e6%99%82%e3%81%aehtml\" >HTML when scrolled<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bccss%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e4%be%8b\" >Fixed Header CSS Code Example<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#html%e3%81%a8%e3%81%ae%e7%b4%90%e3%81%a5%e3%81%91%e6%96%b9\" >How to link with HTML<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/hanami-web.tokyo.jp\/blog\/en\/coding\/fix-header-samplecode\/#%e3%81%be%e3%81%a8%e3%82%81\" >summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%ae%e4%bd%9c%e3%82%8b%e6%96%b9%e6%a6%82%e8%a6%81\"><\/span>How to create a fixed header<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Give the part you want to fix an ID (Class is also OK)<\/li>\n\n\n\n<li>Add a specific class when scrolling with jQuery<\/li>\n\n\n\n<li>Fix the specific class that was added and adjust the design<\/li>\n<\/ol>\n\n\n\n<p>It can be done in this way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%aehtml%e3%82%b3%e3%83%bc%e3%83%89\"><\/span>Header HTML Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For example, give the DIV that encloses the global menu an ID of [header] like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=&quot;header&quot;&gt;\n  &lt;div class=&quot;global-nav&quot;&gt;\n    &lt;nav class=&quot;header-nav&quot;&gt;\n      &lt;ul class=&quot;nav&quot;&gt;\n        &lt;li&gt;&lt;a href=&quot;\/blog\/en\/index.html\/&quot;&gt;HOME&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/news.html\/&quot;&gt;news&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/guidance\/&quot;&gt;Message from the President&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/facility\/&quot;&gt;Facility Information&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/access\/&quot;&gt;MAP &amp;amp; ACCESS&lt;\/a&gt;&lt;\/li&gt;\n      &lt;\/ul&gt;\n      &lt;\/nav&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bc%e3%81%ab%e3%81%99%e3%82%8bjquery%e3%82%b3%e3%83%bc%e3%83%89\"><\/span>jQuery code for fixed header<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Use jQuery to add the header ID [header]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When scrolled 50px from the TOP<\/li>\n\n\n\n<li>Add the [.is-fixed] class<\/li>\n\n\n\n<li>When the page is not scrolled 50px from the top<\/li>\n\n\n\n<li>[is-fixed] class deleted<\/li>\n<\/ul>\n\n\n\n<p>The following instructions are given.<\/p>\n\n\n\n<p>Save it in a separate file, such as common.js.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(window).bind(&#039;scroll&#039;, function(){ if($(this).scrollTop() &gt; 50) { $(&#039;#header&#039;).addClass(&#039;is-fixed&#039;); }else{ $(&#039;#header&#039;).removeClass(&#039;is-fixed&#039;); } });<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"css%e3%81%ae%e8%a8%ad%e5%ae%9a%e6%96%b9%e6%b3%95\"><\/span>How to set up CSS<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will demonstrate how the header class changes when it is scrolled and not scrolled.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%95%e3%82%8c%e3%81%a6%e3%81%84%e3%81%aa%e3%81%84%e6%99%82%e3%81%aehtml\"><\/span>HTML when not scrolled<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=&quot;header&quot;&gt;\n  &lt;div class=&quot;global-nav&quot;&gt;\n    &lt;nav class=&quot;header-nav&quot;&gt;\n      &lt;ul class=&quot;nav&quot;&gt;\n        &lt;li&gt;&lt;a href=&quot;\/blog\/en\/index.html\/&quot;&gt;HOME&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/news.html\/&quot;&gt;news&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/guidance\/&quot;&gt;Message from the President&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/facility\/&quot;&gt;Facility Information&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/access\/&quot;&gt;MAP &amp;amp; ACCESS&lt;\/a&gt;&lt;\/li&gt;\n      &lt;\/ul&gt;\n      &lt;\/nav&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%95%e3%82%8c%e3%81%9f%e6%99%82%e3%81%aehtml\"><\/span>HTML when scrolled<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>[header] The [is-fixed] class will be added to the DIV with the ID<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=&quot;header&quot; class=&quot;is-fixed&quot;&gt;\n  &lt;div class=&quot;global-nav&quot;&gt;\n    &lt;nav class=&quot;header-nav&quot;&gt;\n      &lt;ul class=&quot;nav&quot;&gt;\n        &lt;li&gt;&lt;a href=&quot;\/blog\/en\/index.html\/&quot;&gt;HOME&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/news.html\/&quot;&gt;news&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/guidance\/&quot;&gt;Message from the President&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/facility\/&quot;&gt;Facility Information&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=&quot;\/blog\/en\/access\/&quot;&gt;MAP &amp;amp; ACCESS&lt;\/a&gt;&lt;\/li&gt;\n      &lt;\/ul&gt;\n      &lt;\/nav&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e5%9b%ba%e5%ae%9a%e3%83%98%e3%83%83%e3%83%80%e3%83%bccss%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e4%be%8b\"><\/span>Fixed Header CSS Code Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can add a design to the CSS class \u3010is-fixed\u3011. If you want a fixed header, add \u3010position:fixed\u3011 to make it fixed.<\/p>\n\n\n\n<p>You can fix the global menu from the beginning with [position:fixed] and then make small changes to the design, such as changing the background color only when scrolling, to make it appear fixed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.is-fixed .global-nav { position: fixed; top: 0; left: 0; background: #ffffff; padding: 0 20px 5px 5%; }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"html%e3%81%a8%e3%81%ae%e7%b4%90%e3%81%a5%e3%81%91%e6%96%b9\"><\/span>How to link with HTML<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>index.html - HTML file with header code<\/li>\n\n\n\n<li>style.css - CSS file containing the CSS code<\/li>\n\n\n\n<li>common.js - JS file<\/li>\n<\/ul>\n\n\n\n<p>Let&#039;s say you have these two files: index.html Add the following inside:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>External style.css loading code<\/li>\n\n\n\n<li>jQuery library loading code<\/li>\n\n\n\n<li>Loading code for the external file common.js<\/li>\n<\/ul>\n\n\n\n<p>is.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"style.css\"&gt;\n&lt;script type=\"text\/javascript\" src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.0.2\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script type=\"text\/javascript\" src=\"common.js\"&gt;&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%e3%81%be%e3%81%a8%e3%82%81\"><\/span>summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can add specific code when scrolling with some simple JS code.<\/p>\n\n\n\n<p>When using WordPress, many themes have a fixed header from the beginning.<strong>myStickymenu<\/strong>] allows you to create a fixed header without any coding!<\/p>\n\n\n\n<p>WordPress x no-code construction is great!<\/p>","protected":false},"excerpt":{"rendered":"<p>\u30b9\u30af\u30ed\u30fc\u30eb\u3059\u308b\u3068\u30d8\u30c3\u30c0\u30fc\u304c\u56fa\u5b9a\u3055\u308c\u308b\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u306ehtml\/css\/js\u3092\u4f7f\u3063\u305f\u5b9f\u88c5\u65b9\u6cd5\u5099\u5fd8\u9332 \u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u306e\u4f5c\u308b\u65b9\u6982\u8981 \u3053\u306e\u6d41\u308c\u3067\u51fa\u6765\u307e\u3059\u3002 \u30d8\u30c3\u30c0\u30fc\u306eHTML\u30b3\u30fc\u30c9 \u4f8b\u3048\u3070\u3053\u306e\u3088\u3046\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u30e1\u30cb\u30e5\u30fc\u3092\u56f2\u3046DIV\u306bI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10613,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1522],"tags":[],"class_list":["post-10571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding"],"blocksy_meta":[],"aioseo_notices":[],"uagb_featured_image_src":{"full":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",720,540,false],"thumbnail":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1-150x150.png",150,150,true],"medium":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1-300x225.png",300,225,true],"medium_large":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",720,540,false],"large":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",720,540,false],"1536x1536":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",720,540,false],"2048x2048":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",720,540,false],"trp-custom-language-flag":["https:\/\/hanami-web.tokyo.jp\/blog\/wp-content\/uploads\/2022\/11\/thumnail-5-1.png",16,12,false]},"uagb_author_info":{"display_name":"\u682a\u5f0f\u4f1a\u793eHanamiWEB \u4ee3\u8868\u53d6\u7de0\u5f79 \u677e\u6d66\u307f\u3055","author_link":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/author\/hanami\/"},"uagb_comment_info":0,"uagb_excerpt":"\u30b9\u30af\u30ed\u30fc\u30eb\u3059\u308b\u3068\u30d8\u30c3\u30c0\u30fc\u304c\u56fa\u5b9a\u3055\u308c\u308b\u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u306ehtml\/css\/js\u3092\u4f7f\u3063\u305f\u5b9f\u88c5\u65b9\u6cd5\u5099\u5fd8\u9332 \u56fa\u5b9a\u30d8\u30c3\u30c0\u30fc\u306e&hellip;","brizy_media":[],"_links":{"self":[{"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/posts\/10571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/comments?post=10571"}],"version-history":[{"count":0,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/posts\/10571\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/media\/10613"}],"wp:attachment":[{"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/media?parent=10571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/categories?post=10571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hanami-web.tokyo.jp\/blog\/en\/wp-json\/wp\/v2\/tags?post=10571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}