Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@vincentorback
vincentorback / randomBetween.js
Last active January 8, 2017 11:30
randomBetween.js
/**
* Get a random int between two values
* @param {Int} min
* @param {Int} max
* @return {Int}
*/
export function randomBetween(min = 0, max = 0) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
@vincentorback
vincentorback / getMousePosition.js
Last active January 8, 2017 11:33
getMousePosition.js
export function getMousePosition (e) {
let posX = 0
let posY = 0
if (!e) {
e = window.event
}
if (e.pageX || e.pageY) {
posX = e.pageX
/* If js doesnt load or just before it does: hide element for 2 seconds. */
.no-js .u-showIfNoJs {
animation: showAfterLong 2000ms;
}
/* Hide when js loads and remove animation */
.js .u-showIfNoJs {
animation: none;
display: none;
}
@vincentorback
vincentorback / setFaviconColor.js
Last active July 25, 2019 10:43
setFaviconColor.js
/* global define, Image */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function () {
return factory()
})
} else if (typeof exports === 'object') {
module.exports = factory()
} else {
@vincentorback
vincentorback / getViewportOffset.js
Last active January 8, 2017 13:31
Get element offset within viewport
/**
* Get element offset
* @param {Element} obj
* @return {Boolean}
*/
export function getElementViewportOffset (element) {
let left = element.offsetLeft,
top = element.offsetTop,
elementParent = element.parentNode
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
@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,
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 / 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 -
@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) => {