Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@vincentorback
vincentorback / elementInViewport.js
Last active June 30, 2019 19:04
Check if element is in viewport
/**
* 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;
@vincentorback
vincentorback / getClosest.js
Last active June 8, 2016 11:57
Get closest parent element matching selector
/**
* 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
@vincentorback
vincentorback / breakpoints.js
Last active February 21, 2017 14:18
Sync resizing event and breakpoints
/**
* Dependencies
*/
const breakpoints = {
xs: 300,
sm: 600,
md: 800,
lg: 1200,
xl: 1600
};
@vincentorback
vincentorback / nodesToArray.js
Created March 20, 2016 12:00
Converting a NodeList to an Array
/**
* 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);
@vincentorback
vincentorback / urlParameters.js
Last active June 1, 2021 11:22
Set and get URL parameters
/**
* 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 ? "&" : "?"
@vincentorback
vincentorback / index.js
Last active April 17, 2020 10:53
List z-index values
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) => {
@vincentorback
vincentorback / slugify.js
Last active October 28, 2017 03:28
Slugify a string - javascript and php
/**
* 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 -
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);
@vincentorback
vincentorback / onDomChange.js
Last active January 8, 2017 13:34
Listen to modifications to the DOM
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,
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