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 rectangle a contains rectangle b | |
// Each object (a and b) should have 2 properties to represent the | |
// top-left corner (x1, y1) and 2 for the bottom-right corner (x2, y2). | |
function contains(a, b) { | |
return !( | |
b.x1 < a.x1 || | |
b.y1 < a.y1 || | |
b.x2 > a.x2 || | |
b.y2 > a.y2 | |
); |
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 arr = { | |
max: function(array) { | |
return Math.max.apply(null, array); | |
}, | |
min: function(array) { | |
return Math.min.apply(null, array); | |
}, | |
range: function(array) { |
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
/** | |
* A "deeper" indented text effect with the :before and :after pseudo-elements. | |
*/ | |
html, body { | |
height: 100%; | |
} | |
body { | |
margin: 0; |
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 the text nodes which are descendants of the passed parent | |
function getTextNodes(parent){ | |
var all = []; | |
for (parent = parent.firstChild; parent; parent = parent.nextSibling) { | |
if (['SCRIPT','STYLE'].indexOf(parent.tagName) >= 0) continue; | |
if (parent.nodeType === Node.TEXT_NODE) all.push(parent); | |
else all = all.concat(getTextNodes(parent)); | |
} | |
return all; | |
} |
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
// Replace tag name but keep element contents | |
function changeTag(el, newTagName, keepAttributes) { | |
var newEl = document.createElement(newTagName); | |
// Copy the children | |
while (el.firstChild) { | |
newEl.appendChild(el.firstChild); // *Moves* the child | |
} | |
// Copy the attributes |
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
// probability that the event with the passed probability will NOT occur | |
function complement(p) { | |
return 1 - p; | |
} | |
// probability that a and b will happen when neither outcome | |
// is affected by the other (accepts 1 or more arguments) | |
function intersectionOfIndependentEvents(a, b) { | |
var ret = a; | |
for (var i = 1; i < arguments.length; 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
// get nearest parent element matching selector | |
var closest = (function() { | |
var el = HTMLElement.prototype; | |
var matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector; | |
return function closest(el, selector) { | |
return matches.call(el, selector) ? el : closest(el.parentElement, selector); | |
}; | |
})(); |
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
/* | |
* Pure CSS aspect ratio with no spacer images or js! :) | |
*/ | |
body { | |
width: 36%; | |
margin: 8px auto; | |
} | |
div.stretchy-wrapper { |
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
/** | |
* Pure CSS stopwatch | |
*/ | |
* { box-sizing: border-box; -moz-box-sizing: border-box } | |
html { background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAOklEQVQIWx3K2QkAMAgFwadEsBjF/ksTD0Kyn8uQme05BzOD3QW5+2YmVBWvP6oKIoLuBkXEF48zMy6Jfxr7Roi85AAAAABJRU5ErkJggg==) } | |
body { | |
text-align: center; |
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
// remove parent without removing childen | |
function unwrap(wrapper) { | |
// place childNodes in document fragment | |
var docFrag = document.createDocumentFragment(); | |
while (wrapper.firstChild) { | |
var child = wrapper.removeChild(wrapper.firstChild); | |
docFrag.appendChild(child); | |
} | |
// replace wrapper with document fragment |
NewerOlder