Skip to content

Instantly share code, notes, and snippets.

@jackmakesthings
Last active August 29, 2015 14:01
Show Gist options
  • Save jackmakesthings/dcb444d6f4469e85bbe5 to your computer and use it in GitHub Desktop.
Save jackmakesthings/dcb444d6f4469e85bbe5 to your computer and use it in GitHub Desktop.
Utils and modules
Things that might come in handy again later.
/**
* Add a class when the page is scrolling.
* Typical use: disable hover effects during scroll for better performance.
* Uses the scroll event, so it's fired often and thus isn't ideal; should rewrite later.
*/
function adjustForScroll() {
var $body = document.querySelector('body'),
bufferTime = 300,
scrollClass = 'scrolling',
timer;
window.addEventListener('scroll', function() {
clearTimeout(timer);
if(!$body.classList.contains(scrollClass) {{
$body.classList.add(scrollClass);
}
timer = setTimeout( function(){
$body.classList.remove(scrollClass);
}, bufferTime);
}, false);
}
/**
* Usage: animate(docElem, "scrollTop", "", currscrl, newscrl, 500, true) = scroll from currscrl to newscrl in 500s)
*/
function animate(elem,style,unit,from,to,time,prop) {
if( !elem) return;
var start = new Date().getTime(),
timer = setInterval(function() {
var step = Math.min(1,(new Date().getTime()-start)/time);
if (prop) {
elem[style] = (from+step*(to-from))+unit;
} else {
elem.style[style] = (from+step*(to-from))+unit;
}
if( step == 1) clearInterval(timer);
},25);
elem.style[style] = from+unit;
}
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
}
return a;
}
function getOffset( el ) {
var offsetTop = 0,
offsetLeft = 0;
do {
if ( !isNaN( el.offsetTop ) ) {
offsetTop += el.offsetTop;
}
if ( !isNaN( el.offsetLeft ) ) {
offsetLeft += el.offsetLeft;
}
} while( el = el.offsetParent )
return {
top : offsetTop,
left : offsetLeft
}
}
function getScrollPosition() {
if (document.documentElement.scrollTop === 0) {
return document.body.scrollTop;
} else {
return document.documentElement.scrollTop;
}
}
var docElem = window.document.documentElement;
function getViewportH() {
var client = docElem['clientHeight'],
inner = window['innerHeight'];
if( client < inner )
return inner;
else
return client;
}
function inViewport( el, h ) {
var elH = el.offsetHeight,
scrolled = scrollY(),
viewed = scrolled + getViewportH(),
elTop = getOffset(el).top,
elBottom = elTop + elH,
// if 0, the element is considered in the viewport as soon as it enters.
// if 1, the element is considered in the viewport only when it's fully inside
// value in percentage (1 >= h >= 0)
h = h || 1;
return (elTop + elH * h) <= viewed && (elBottom - elH * h) >= scrolled;
}
var $toTop = $('.scroll-to-top');
$toTop.on('click', function(e) {
e.preventDefault();
$("body, html").animate({
scrollTop:0
}, 700);
});
var $toTop = document.getElementByID('scroll-to-top');
function scrollToTop() {
/* coming soon */
}
function scrollY() {
return window.pageYOffset || docElem.scrollTop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment