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
/** | |
* Check if element is in viewport | |
* @param {Node} el | |
* @return {Object} | |
*/ | |
export function elementInViewport(el) { | |
let top = el.offsetTop; | |
let left = el.offsetLeft; | |
let width = el.offsetWidth; | |
let height = el.offsetHeight; |
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
/** | |
* Get closest DOM element up the tree that contains a class, ID, or data attribute | |
* @param {Node} elem The base element | |
* @param {String} selector The class, id, data attribute, or tag to look for | |
* @return {Node} Element or Null if no match | |
*/ | |
export function getClosest (elem, selector) { | |
let firstChar = selector.charAt(0); | |
// Get closest match |
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
/** | |
* Dependencies | |
*/ | |
const breakpoints = { | |
xs: 300, | |
sm: 600, | |
md: 800, | |
lg: 1200, | |
xl: 1600 | |
}; |
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
/** | |
* Turns a list of nodes into an array | |
* @param { NodeList } nodeList | |
* @return { Array } | |
*/ | |
export function nodesToArray(nodeList) { | |
return [...nodeList]; | |
return Array.from(nodeList); | |
return [].slice.call(nodeList); | |
return Array.prototype.slice.call(nodeList); |
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
/** | |
* Set URL Parameter | |
* @param { String } key | |
* @param { String } value | |
* @param { String } url | |
*/ | |
export function setUrlParameter(key, value, url = window.location.href) { | |
let reg = new RegExp("([?&])" + key + "=.*?(&|$)", "i") | |
let separator = url.indexOf('?') !== -1 ? "&" : "?" |
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
const postcss = require('postcss') | |
module.exports = postcss.plugin('postcss-zindex', (options) => { | |
return (css) => { | |
let nodes = [] | |
// TODO: Some options? | |
// Get all z-index declarations | |
css.walkDecls('z-index', (decl, i) => { |
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
/** | |
* Slugify a string | |
* @param {String} text | |
* @return {String} | |
*/ | |
export function slugify(text) { | |
return text.toString().toLowerCase() | |
.replace(/([å,ä])/g, 'a') // Replace å and ä with aa | |
.replace(/(ö)/g, 'o') // Replace ö with o | |
.replace(/\s+/g, '-') // Replace spaces with - |
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 orientationEls = doc.querySelectorAll('.js-orientation'); | |
window.addEventListener('deviceorientation', function(eventData) { | |
var yTilt = Math.round((-eventData.beta + 90) * (40/180) - 40); | |
var xTilt = Math.round(-eventData.gamma * (20/180) - 20); | |
if (xTilt > 0) { | |
xTilt = -xTilt; | |
} else if (xTilt < -40) { | |
xTilt = -(xTilt + 80); |
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
const observer = new MutationObserver(function (mutations) { | |
// Do the things | |
}); | |
let targetEl = document.documentElement; // Can be any element | |
observer.observe(targetEl, { | |
childList: true, | |
subtree: true, | |
attributes: true, |
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 onVisibilityChange (callbackHidden, callbackVisible) { | |
var browserPrefixes = ['moz', 'ms', 'o', 'webkit'] | |
var isVisible = true // internal flag, defaults to true | |
// get the correct attribute name | |
function getHiddenPropertyName (prefix) { | |
return (prefix ? prefix + 'Hidden' : 'hidden') | |
} | |
// get the correct event name |