Skip to content

Instantly share code, notes, and snippets.

@cladley
cladley / Event Delegation
Created October 3, 2018 11:07
Event attached to a parent element and you are interested when a child is clicked
// Some event attached to a parent element
this.parent.addEventListener('click', this.handleItemsClick.bind(this), false);
function handleItemsClick(e) {
// e.currentTarget is the element that the event was attached to. this.parent here
for (var target = e.target; target && target !== e.currentTarget; target = target = target.parentNode) {
if (target.matches('.child-selector')) {
// Do whatever
@cladley
cladley / Aspect ratio video player.
Created May 17, 2018 10:21
Uses @media (min-aspect-ratio: 16 / 9) to swap height and width
$video-total-margin: 96px;
$overlay-bg-color: #000;
$close-button-bg-color: #333;
.video-overlay {
background-color: $overlay-bg-color;
display: flex;
height: 100%;
left: 0;
opacity: 0;
/**
* Timeout promise that can cancel a long running promise
* using Promise.race
*/
function timeoutPromise(delay) {
return new Promise(function(resolve, reject) {
setTimeout(function(){
reject("Timeout!");
}, delay);
function timeoutfunc(fn,delay) {
var intv = setTimeout(function() {
intv = null;
fn(new Error("Timeout"));
}, delay);
return function() {
// timeout hasn't happened yet?
if(intv) {
clearTimeout(intv);
@cladley
cladley / Full screen background-image or image
Created June 10, 2016 12:49
snippet to create a full screen image on any viewport size
<div class="fullscreen" data-img-width="1500" data-img-height="900"> <---- because its a background image, we need the width and height
// whatever here. If it was a normal image tag, we could just get it off
</div> the image
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
.fullscreen {
width: 100%;
height: 100%;
background-image:url('http://someimage.jpg');
var docElem = window.document.documentElement;
function getViewportH() {
var client = docElem['clientHeight'],
inner = window['innerHeight'];
if( client < inner )
return inner;
else
return client;
var attach = require('attach.js');
var utils = require('./helpers/utils');
var constants = require('./helpers/const');
/**
* Create a new accordion object
* @param {HTMLElement} el
*/
function Accordion(el) {
function SocialShare(el) {
this.POPUP_WIDTH = 550;
this.POPUP_HEIGHT = 420;
this.$element = $(el);
this.$element.on('click', this.onClick.bind(this));
}
SocialShare.prototype = {
@cladley
cladley / endevents.js
Last active September 15, 2015 10:40
transition end , animation end event names
function whichAnimationEndEvent(){
var el = document.createElement('fakeelement');
var animationsEvents = {
'WebkitAnimation' : 'webkitAnimationEnd',
'OAnimation' : 'oAnimationEnd',
'msAnimation' : 'MSAnimationEnd',
'animation' : 'animationend'
};
for(var t in animationsEvents){
var latestKnownScrollY = 0,
ticking = false;
function onScroll(){
latestKnownScrollY = window.scrollY;
requestTick();
}