Created
August 26, 2014 10:15
-
-
Save crimx/0f3f28cff92a0e898332 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script> | |
<meta charset="utf-8"> | |
<title>Test by Jesse</title> | |
</head> | |
<body> | |
<nav id = 'sidenav'> | |
<ul> | |
<li><a href="#1">First</a></li> | |
<li><a href="#2">Second</a></li> | |
<li><a href="#3">Third</a></li> | |
<li><a href="#4">Fourth</a></li> | |
<li><a href="#5">Fifth</a></li> | |
</ul> | |
</nav> | |
<div class="sections"> | |
<section id="1"><h1>First</h1></section> | |
<section id="2"><h1>Second</h1></section> | |
<section id="3"><h1>Third</h1></section> | |
<section id="4"><h1>Fourth</h1></section> | |
<section id="5"><h1>Fifth</h1></section> | |
</div> | |
</body> | |
</html> |
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
$(window).on('scroll', function () { | |
var scrollTop = $(document).scrollTop(), | |
activeLi = $('#sidenav li.active').first(); | |
if (activeLi.length <= 0) { | |
$('#sidenav li').first().addClass('active'); | |
} else { | |
var activeSec = $(activeLi.find('a').attr('href')); | |
if (activeSec.offset().top + activeSec.outerHeight() <= scrollTop) { | |
activeLi.removeClass('active'); | |
activeLi.next().addClass('active'); | |
} else { | |
var preActiveLi = activeLi.prev(); | |
if (preActiveLi.length > 0) { | |
var preActiveSec = $(preActiveLi.find('a').attr('href')); | |
if (preActiveSec.offset().top - scrollTop >= 0) { | |
activeLi.removeClass('active'); | |
preActiveLi.addClass('active'); | |
} | |
} | |
} | |
} | |
}); |
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
* { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
} | |
nav { | |
position: fixed; | |
height: 200px; | |
margin: auto; | |
top: 0; | |
bottom: 0; | |
right: 0; | |
background: #1ABC9C; | |
} | |
nav ul { | |
margin: 0 auto; | |
list-style: none; | |
text-align: center; | |
} | |
nav ul li { | |
display: block; | |
padding: 10px 20px; | |
} | |
nav ul li a { | |
padding: 10px 0; | |
color: #fff; | |
font-size: 1rem; | |
text-decoration: none; | |
font-weight: bold; | |
transition: all 0.2s ease; | |
} | |
li:hover > a { | |
color: #34495E; | |
} | |
li.active { | |
background-color: white; | |
} | |
li.active > a { | |
color: #1ABC9C; | |
} | |
section { | |
height: 900px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment