Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@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
@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 {
/* 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 / 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
@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 / getScrollPosition.js
Created January 8, 2017 11:36
getScrollPosition.js
/**
* Get scroll position
* @param {Object} rootElement
* @return {Object}
*/
export function getScrollPosition (rootElement) {
return {
y: rootElement ? rootElement.scrollTop : window.pageYOffset,
x: rootElement ? rootElement.scrollLeft : window.pageXOffset
}
@vincentorback
vincentorback / duplicate-posts.php
Last active January 18, 2017 19:30
Adds and action in the WordPress admin to duplicate pages and posts
<?php
/**
* Function creates post duplicate as a draft and redirects then to the edit post screen
*/
function duplicate_post_as_draft() {
global $wpdb;
if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'duplicate_post_as_draft' == $_REQUEST['action']))) {
wp_die('No post to duplicate has been supplied!');
@vincentorback
vincentorback / object-fit-cover-polyfill.js
Last active February 21, 2017 14:17
Replace object-fit elements with a div using background-size: cover
/*
Demo:
http://codepen.io/vincentorback/pen/ZLLJbB
*/
export function objectFit (el) {
let objectElReplacement = `<div
class="${el.classList}"
style="
background-image:url(${el.getAttribute('src')});
@vincentorback
vincentorback / hideIdleCursor.js
Last active March 4, 2017 12:49
hideIdleCursor.js
const targetEl = document.querySelector('.js-target')
const hideMouse = false
const idleTime = 3000
let idleMouseTimer
targetEl.addEventListener('mousemove', (e) => {
if (!hideMouse) {
targetEl.style.cursor = ''
clearTimeout(idleMouseTimer)
export function randomHex() {
return '#' + (Math.random() * 0xFFFFFF << 0).toString(16)
}