Last active
August 29, 2015 14:01
-
-
Save jackmakesthings/dcb444d6f4469e85bbe5 to your computer and use it in GitHub Desktop.
Utils and modules
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
Things that might come in handy again later. |
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
/** | |
* 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); | |
} |
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
/** | |
* 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; | |
} |
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
function extend( a, b ) { | |
for( var key in b ) { | |
if( b.hasOwnProperty( key ) ) { | |
a[key] = b[key]; | |
} | |
} | |
return a; | |
} |
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
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 | |
} | |
} |
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
function getScrollPosition() { | |
if (document.documentElement.scrollTop === 0) { | |
return document.body.scrollTop; | |
} else { | |
return document.documentElement.scrollTop; | |
} | |
} |
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
var docElem = window.document.documentElement; | |
function getViewportH() { | |
var client = docElem['clientHeight'], | |
inner = window['innerHeight']; | |
if( client < inner ) | |
return inner; | |
else | |
return client; | |
} |
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
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; | |
} |
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
var $toTop = $('.scroll-to-top'); | |
$toTop.on('click', function(e) { | |
e.preventDefault(); | |
$("body, html").animate({ | |
scrollTop:0 | |
}, 700); | |
}); |
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
var $toTop = document.getElementByID('scroll-to-top'); | |
function scrollToTop() { | |
/* coming soon */ | |
} |
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
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