Skip to content

Instantly share code, notes, and snippets.

@romeovs
Created November 24, 2013 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romeovs/7631533 to your computer and use it in GitHub Desktop.
Save romeovs/7631533 to your computer and use it in GitHub Desktop.
example where `.activating` links while scrolling is unpleasant. only triggering the waypoint when ":animated" is not on html element.
// HTML:
// <nav>
// <ul>
// <li> <a href="/#learn">learn</a> </li>
// <li> <a href="/#about-me">about me</a> </li>
// <li> <a href="/#signup">sign up</a> </li>
// </ul>
// </nav>
// <section id="#learn"> .. </section>
// <section id="#about-me"> .. </section>
// <section id="#signup"> .. </section>
// when move trough a certain section, add the
// .activate class to corresponding the navbar link
$("#learn").waypoint(function(direction){
$("nav a").removeClass("active");
if ( direction === "down" ) {
$("nav a[href='/#learn']").addClass("active");
}
}, {offset: navh, continuous: false});
$("#about-me").waypoint(function(direction){
//if ( !$("html,body").is(":animated")) {
// this fixes the problem but in a different way
$("nav a").removeClass("active");
if ( direction === "down" ) {
$("nav a[href='/#about-me']").addClass("active");
} else {
$("nav a[href='/#learn']").addClass("active");
}
//}
}, {offset: navh, continuous: false});
$("#signup").waypoint(function(direction){
//if ( !$("html,body").is(":animated")) {
// this fixes the problem but in a different way
$("nav a").removeClass("active");
if ( direction === "down" ) {
$("nav a[href='/#signup']").addClass("active");
} else {
$("nav a[href='/#about-me']").addClass("active");
}
// }
}, {offset: navh, continuous: false});
// scroll to anchor on nav link click
// disable waypoints on the way
// so sections we move trough don't
// activate nav links unnecesarily.
var scrollToAnchor = function() {
$.waypoints('disable');
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
$(this).parent().parent().find(".active").removeClass("active");
$("nav a[href='/" + this.hash + "']").addClass("active");
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
setTimeout(function(){
$('html,body').animate({
scrollTop: target.offset().top - navh + 1
}, 800);}, 10);
}
}
$.waypoints('disable');
return false;
}
$('a[href*=#]:not([href=#])').click(scrollToAnchor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment