Skip to content

Instantly share code, notes, and snippets.

@adoc
Created February 5, 2015 10:14
Show Gist options
  • Save adoc/fa817fd9a232859b82cd to your computer and use it in GitHub Desktop.
Save adoc/fa817fd9a232859b82cd to your computer and use it in GitHub Desktop.
Fancy Scroller Start.
// TODO: Encaps this and fancyScroll in function.
var currentObj = null;
var fancyScrolling = false;
function fancyScroll(ev, down) {
var scrollToOffset = 60,
scrollDuration = 500;
// Do nothing if we're already animating.
if (!$('body').is(':animated')) {
var windowHeight = $(window).height(),
bottomThreshold = 0, // Same as navbar height.
objToScroll = null,
lastBottom = down && -1 || 9999999;
// Check all the sections and determine if we're scrolling in to one.
$("section, header").each(function () {
var elTop = this.getBoundingClientRect().top,
elBottom = this.getBoundingClientRect().bottom;
if (windowHeight - elTop > 0 && elBottom > bottomThreshold) {
if ((down && elBottom > lastBottom) ||
(!down && elBottom < lastBottom) {
objToScroll = this.id;
lastBottom = elBottom;
}
}
});
if (objToScroll !== currentObj) {
// Do Fancy Scrolling
fancyScrolling = true;
$('html, body').stop();
$('body').animate({
scrollTop: $("#" + objToScroll).offset().top + scrollToOffset
}, {
duration: scrollDuration,
complete: function () {
fancyScrolling = false;
},
easing: 'easeInOutExpo'
});
currentObj = objToScroll;
}
}
}
$(window).bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta /120 > 0) {
$('body').stop();
}
else{
$('body').stop();
}
});
$(window).keydown(function (ev) {
if (ev.keyCode === 38 || // Up
ev.keyCode === 40 || // Down
ev.keyCode === 33 || // Page-Up
ev.keyCode === 34) { // Page-Down
$('body').stop();
}
});
var lastScrollTop = 0;
$(window).scroll(function (ev) {
// contrib: http://stackoverflow.com/a/4326907
var st = $(this).scrollTop();
if (st > lastScrollTop){
checkNavbar();
fancyScroll(ev, true);
} else {
checkNavbar();
fancyScroll(ev, false);
}
lastScrollTop = st;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment