Skip to content

Instantly share code, notes, and snippets.

@umbrae
Created April 14, 2011 21:57
Show Gist options
  • Select an option

  • Save umbrae/920660 to your computer and use it in GitHub Desktop.

Select an option

Save umbrae/920660 to your computer and use it in GitHub Desktop.
readability.article.smoothScrolling = {
reversePageScroll : false, // If they hold shift and hit space, scroll up
/**
* How long it takes to scroll the page, in ms.
**/
scrollSpeed: 500,
/**
* The scrollable element, which changes depending on browser. Initialized via initScrollable.
**/
scrollable: null,
/**
* initScrollable - detect which element to use for scrolling - html or body. Sets the scrollable attribute.
*
* Derived from getScrollable, borrowed from jQuery Smooth Scroll by Karl Swedberg:
* https://github.com/kswedberg/jquery-smooth-scroll/blob/master/jquery.smooth-scroll.js
*
* @param els an array of elements to try to scroll upon - probably ['html','body']
* @return null
**/
initScrollable: function(els) {
var i = 0, scrolled = false;
for(i=0, il=els.length; i<il; i++) {
var tagName = els[i],
el = $(tagName);
if (el.scrollTop() > 0) {
readability.article.smoothScrolling.scrollable = el.get(0);
break;
}
el.scrollTop(1);
scrolled = el.scrollTop() > 0;
el.scrollTop(0);
if (scrolled) {
readability.article.smoothScrolling.scrollable = el.get(0);
break;
}
}
// If we weren't able to initialize the scrollable at all, fall back to 'html, body'.
if(readability.article.smoothScrolling.scrollable === null) {
readability.article.smoothScrolling.scrollable = $('html, body');
}
},
/**
* Initialize the smooth scrolling functionality.
* @return null
**/
init: function() {
readability.article.smoothScrolling.initScrollable(['html','body']);
$(document).keydown( function(e) {
var code = (window.event) ? event.keyCode : e.keyCode,
viewportHeight = $(window).height(),
offset = $(window).scrollTop(),
direction = 1,
delta = viewportHeight - 50;
if (code === 16) {
readability.article.smoothScrolling.reversePageScroll = true;
return;
}
if (code === 32) {
direction = (readability.article.smoothScrolling.reversePageScroll) ? -1 : 1;
if(direction === 1) {
$('#scroll-bullet').css({'top': offset + (delta * direction), 'left': $('#rdb-article-content').position().left - 25}).show();
}
$(readability.article.smoothScrolling.scrollable).animate({scrollTop: offset + (delta * direction)}, readability.article.smoothScrolling.scrollSpeed, function () {
$('#scroll-bullet').delay(500).fadeOut('fast');
});
return false;
}
});
$(document).keyup( function(e) {
var code = (window.event) ? event.keyCode : e.keyCode;
if(code === 16) {
readability.article.smoothScrolling.reversePageScroll = false;
return;
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment