Skip to content

Instantly share code, notes, and snippets.

@nikolaswise
Last active August 22, 2018 20:25
Show Gist options
  • Select an option

  • Save nikolaswise/9625987d0f79f4bf03caa0cdb1310449 to your computer and use it in GitHub Desktop.

Select an option

Save nikolaswise/9625987d0f79f4bf03caa0cdb1310449 to your computer and use it in GitHub Desktop.
export const $ = (selector, context = document) => Array(...context.querySelectorAll(selector))
export const isEven = n => n === 0 || !!(n && !(n%2));
export const isInViewport = (el)=> {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
export const closest = (className, context) => {
var current;
for (current = context; current; current = current.parentNode) {
if (current.nodeType === 1 && current.classList.contains(className)) {
break;
}
}
return current;
}
export const getOffsetTop = (elem) => {
let offsetTop = 0;
do {
if ( !isNaN( elem.offsetTop ) ){
offsetTop += elem.offsetTop;
}
} while( elem = elem.offsetParent );
return offsetTop;
}
export const boolify = string => string == 'true'
export const delay = (ms) => new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms);
})
// delay('2s').then(() => {
// console.log('wow')
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment