Skip to content

Instantly share code, notes, and snippets.

@eQRoeil
Last active December 29, 2015 01:19
Show Gist options
  • Save eQRoeil/7592619 to your computer and use it in GitHub Desktop.
Save eQRoeil/7592619 to your computer and use it in GitHub Desktop.
scrollin-up.js adds .su--scrollinUp to body when (and only) scrolling up http://soqr.fr/scrollin-up Pascal Cauhépé @eQRoeil
/*! scrollin-up.js
* adds .su--scrollinUp to body
* when (and only) scrolling up
* http://soqr.fr/scrollin-up
* @author Pascal Cauhépé @eQRoeil
*/
//do not execute if addEventListener is not supported
if (window.addEventListener) {
//from underscore.js
var debounce = function (func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments,
later = function () {
timeout = null;
if (!immediate) {func.apply(context, args); }
},
callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {func.apply(context, args); }
};
},
b = document.body,
h = window.innerHeight,
origOffsetY = window.pageYOffset,
lastPos = origOffsetY;
var scrollinUpAddClass = function () {
var scrolY = window.pageYOffset;
if (scrolY >= origOffsetY && scrolY < lastPos && scrolY >= 0.5 * h) {
if (b.className.indexOf('su--ready') !== -1) {b.className = b.className.replace('su--ready', 'su--scrollinUp'); }
} else {
if (b.className.indexOf('su--scrollinUp') !== -1) {b.className = b.className.replace('su--scrollinUp', 'su--ready'); }
}
lastPos = scrolY;
};
var scrollinUp = function() {
if (b.className.indexOf('su--ready') === -1 && b.className.indexOf('su--scrollinUp') === -1 ) { b.className += ' su--ready'; }
h = window.innerHeight;
scrollinUpAddClass();
}
window.addEventListener('scroll', debounce(scrollinUp, 150, false), false);
window.addEventListener('resize', debounce(scrollinUp, 150, false), false);
}
/*! scrollin-up.js
* add class su--scrollinUp to body
* when (and only) scrolling up
* http://soqr.fr/scrollin-up
* @author Pascal Cauhépé @eQRoeil
*/
if(window.addEventListener){var debounce=function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)},h=c&&!d;clearTimeout(d),d=setTimeout(g,b),h&&a.apply(e,f)}},b=document.body,h=window.innerHeight,origOffsetY=window.pageYOffset,lastPos=origOffsetY,scrollinUpAddClass=function(){var a=window.pageYOffset;a>=origOffsetY&&lastPos>a&&a>=.5*h?-1!==b.className.indexOf("su--ready")&&(b.className=b.className.replace("su--ready","su--scrollinUp")):-1!==b.className.indexOf("su--scrollinUp")&&(b.className=b.className.replace("su--scrollinUp","su--ready")),lastPos=a},scrollinUp=function(){-1===b.className.indexOf("su--ready")&&-1===b.className.indexOf("su--scrollinUp")&&(b.className+=" su--ready"),h=window.innerHeight,scrollinUpAddClass()};window.addEventListener("scroll",debounce(scrollinUp,150,!1),!1),window.addEventListener("resize",debounce(scrollinUp,150,!1),!1)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment