ある程度スクロールする必要がある縦長ページでは現在地を示すようにナジゲーションボタンにカレント表示をした方が良いでしょう。
各要素の「.offset().top」の位置を取得し、if文を使い、各要素の開始位置以上スクロールしたら、その要素にaddClassします。
参考サイト
www.fbywellfit.com
「offset().top」と「scrollTop()」を理解する
offset().topとは特定のHTML要素が配置されている座標を取得するのに使われるメソッドで、scrollTop()はスクロールした量を取得するメソッド
scrollTop
「scrollTop()」を使用した場合、要素の最上部から縦スクロールした現在位置までのピクセル数を取得します。
{ scrollTop: 0 }であればtopから0ピクセルの位置で停止、{ scrollTop:100}であればtopから100pxの位置で停止します。
$(window).scroll(function() { console.log($(this).scrollTop()); })
offsetメソッド
document内に配置した要素の位置を座標で取得できるメソッドになります。「top(Y座標)」と「left(X座標)」が取得出来ます
var Pos=$('#box').offset().top; console.log(Pos);
index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>シングルページでのカレント設定</title> <link rel="stylesheet" href="css/style.css"> </head> <body id="top"> <header> <h1>シングルページでのカレント設定</h1> </header> <nav id="g-nav"> <ul> <li class="current" id="list-1"><a href="#top">Top</a></li> <li id="list-2"><a href="#concept">Concept</a></li> <li id="list-3"><a href="#product">Product</a></li> <li id="list-4"><a href="#news">News</a></li> <li id="list-5"><a href="#store">Store</a></li> </ul> </nav> <div class="box" id="concept"><h2>CONCEPT</h2></div> <div class="box" id="product"><h2>PRODUCT</h2></div> <div class="box" id="news"><h2>NEWS</h2></div> <div class="box" id="store"><h2>STORE</h2></div> <footer></footer> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js'></script> <script> $(function(){ $(window).on('load resize',function(){ //まず指定した要素の位置を取得する let pos1 = $('#concept').offset().top; let pos2 = $('#product').offset().top; let pos3 = $('#news').offset().top; let pos4 = $('#store').offset().top; console.log(pos4); $(window).scroll(function(){ //スクロール量の取得 let y = $(window).scrollTop(); console.log(y); if(y > 0 && y < pos1){ //yが0以上かつpos1未満の時 $('#list-1').addClass('current').siblings('li').removeClass('current'); }else if(y >= pos1 && y < pos2){ //yがpos1以上かつpos2未満の時 $('#list-2').addClass('current').siblings('li').removeClass('current'); }else if(y >= pos2 && y < pos3){ //yがpos2以上かつpos3未満の時 $('#list-3').addClass('current').siblings('li').removeClass('current'); }else if(y >= pos3 && y < pos4){ $('#list-4').addClass('current').siblings('li').removeClass('current'); }else if(y >= pos4){ $('#list-5').addClass('current').siblings('li').removeClass('current'); } }); }); }); </script> </body> </html>
style.css
@charset "utf-8"; html{box-sizing:border-box;-webkit-text-size-adjust:100%}*,:after,:before{background-repeat:no-repeat;box-sizing:inherit}:after,:before{text-decoration:inherit;vertical-align:inherit}*{padding:0;margin:0;box-sizing:border-box;}audio:not([controls]){display:none;height:0}hr{overflow:visible}article,aside,details,figcaption,figure,footer,header,main,menu,nav,section,summary{display:block}summary{display:list-item}small{font-size:80%}[hidden],template{display:none}abbr[title]{border-bottom:1px dotted;text-decoration:none}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}code,kbd,pre,samp{font-family:monospace,monospace}b,strong{font-weight:bolder}dfn{font-style:italic}mark{background-color:#ff0;color:#000}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}input{border-radius:0}[role=button],[type=button],[type=reset],[type=submit],button{cursor:pointer}[disabled]{cursor:default}[type=number]{width:auto}[type=search][type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}textarea{overflow:auto;resize:vertical}button,input,optgroup,select,textarea{font:inherit}optgroup{font-weight:700}button{overflow:visible}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:0;padding:0}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button:-moz-focusring{outline:1px dotted}[type=reset],[type=submit],button,html [type=button]button,select{text-transform:none}button,input,select,textarea{background-color:transparent;border-style:none;color:inherit}select::-ms-expand{display:none}select::-ms-value{color:currentColor}legend{border:0;color:inherit;display:table;max-width:100%;white-space:normal}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}[type=search]img{border-style:none;vertical-align: bottom}progress{vertical-align:baseline}svg:not(:root){overflow:hidden}audio,canvas,progress,video{display:inline-block}@media screen{[hidden~=screen]{display:inherit}[hidden~=screen]:not(:active):not(:focus):not(:target){position:absolute!important;clip:rect(0 0 0 0)!important}}[aria-busy=true]{cursor:progress}[aria-controls]{cursor:pointer}[aria-disabled]{cursor:default}::-moz-selection{background-color:#b3d4fc;color:#000;text-shadow:none}::selection{background-color:#b3d4fc;color:#000;text-shadow:none}ul,ol{list-style:none;}a{text-decoration:none;}img{max-width: 100%;vertical-align: bottom;} html{ scroll-behavior: smooth; } header{ width: 100%; height: 100vh; background-color: #4b6072; padding-top: 200px; } h1{ text-align: center; font-size: 40px; color: #FFF; } .box{ width: 100%; height: 100vh; padding-top: 100px; background: url(../img/01.webp) no-repeat center center/cover; } .box:nth-of-type(2){ background: url(../img/02.webp) no-repeat center center/cover; } .box:nth-of-type(3){ background: url(../img/03.webp) no-repeat center center/cover; } .box:nth-of-type(4){ background: url(../img/04.webp) no-repeat center center/cover; } footer{ width: 100%; height: 40vh; background-color: #514f50; } #g-nav{ background-color: #ffffffdd; position: fixed; right: 0; bottom: 50px; padding: 20px; } #g-nav li{ margin-bottom: 10px; } #g-nav a{ display: block; padding: 6px; color: #222; } #g-nav a::before{ display: inline-block; content: ""; width: 12px; height: 12px; border-radius: 50%; background-color: #058381; margin-right: 10px; opacity: 0; transition: 0.2s; } #g-nav a:hover::before,#g-nav li.current>a::before{ opacity: 1; } .box>h2{ text-align: center; font-size: 28px; background-color: #FFF; width: fit-content;/* 幅を要素の中身に合わせる */ margin: 0 auto; padding: 10px 16px; }