Last active
October 9, 2018 06:13
-
-
Save k-ish/172e71d4187375ec5c9547478b18a52f to your computer and use it in GitHub Desktop.
scroll highlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- スクロール位置によって、ハイライトする項目が変化 --> | |
<header> | |
<h1>Highlight Menu</h1> | |
<nav> | |
<ul> | |
<li class="current"><a href="#">Section1</a></li> | |
<li><a href="#">Section2</a></li> | |
<li><a href="#">Section3</a></li> | |
<li><a href="#">Section4</a></li> | |
</ul> | |
</nav> | |
</header> | |
<h1>Section1</h1> | |
<h1>Section2</h1> | |
<h1>Section3</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
// ブラウザをスクロール | |
$(window).scroll(function(){ | |
// 各コンテンツ位置とスクロール量の関係を調べる | |
for(var i = 1; i <= 4; i++){ | |
if($("section:nth-child(" + i + ")").offset().top < $(window).scrollTop() + 300){ | |
// 現在のcurrentクラスを削除 | |
$("nav li").removeClass("current"); | |
// 新しくcurrentクラスを追加 | |
$("nav li:nth-child(" + i + ")").addClass("current"); | |
} | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* .current 変化時のスタイルを記述 */ | |
.current { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment