Last active
December 12, 2015 08:49
-
-
Save jonesmac/4747144 to your computer and use it in GitHub Desktop.
Javascript: Single Page Site Waypoint Script
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
<script type="text/javascript"> | |
$(document).ready(function() { | |
// The same for all waypoints | |
$('body').delegate('section > div.page-section', 'waypoint.reached', function(event, direction) { | |
var $active = $(this); | |
if (direction === "up") { | |
$active = $active.prev(); | |
} | |
if (!$active.length) $active = $active.end(); | |
$('.section-active').removeClass('section-active'); | |
$active.addClass('section-active'); | |
$('.link-active').removeClass('link-active'); | |
$('a[href=#'+$active.attr('id')+']').addClass('link-active'); | |
}); | |
// Register each section as a waypoint. | |
$('section > div.page-section').waypoint({ offset: '50%' }); | |
// Wicked credit to | |
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html | |
var scrollElement = 'html, body'; | |
$('html, body').each(function () { | |
var initScrollTop = $(this).attr('scrollTop'); | |
$(this).attr('scrollTop', initScrollTop + 1); | |
if ($(this).attr('scrollTop') == initScrollTop + 1) { | |
scrollElement = this.nodeName.toLowerCase(); | |
$(this).attr('scrollTop', initScrollTop); | |
return false; | |
} | |
}); | |
// Smooth scrolling for internal links | |
$("a[href^='#']").click(function(event) { | |
event.preventDefault(); | |
var $this = $(this), | |
target = this.hash, | |
$target = $(target); | |
// Line below is IOS fix 1 of 2 - http://stackoverflow.com/questions/7826868/fixed-position-navbar-only-clickable-once-in-mobile-safari-on-ios5#10030251 | |
$('#device').css('height', '200px'); | |
$(scrollElement).stop().animate({ | |
'scrollTop': $target.offset().top | |
}, 500, 'swing', function() { | |
window.location.hash = target; | |
// Line below is IOS fix 2 of 2 - http://stackoverflow.com/questions/7826868/fixed-position-navbar-only-clickable-once-in-mobile-safari-on-ios5#10030251 | |
$('#device').css('height', '0px'); | |
}); | |
}); | |
}); | |
</script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$('.top').addClass('hidden'); | |
$.waypoints.settings.scrollThrottle = 30; | |
$('#page-wrapper').waypoint(function(event, direction) { | |
$('.top').toggleClass('hidden', direction === "up"); | |
}, { | |
offset: '-100%' | |
}).find('#page-menu-wrapper').waypoint(function(event, direction) { | |
$(this).parent().toggleClass('sticky', direction === "down"); | |
event.stopPropagation(); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment