Skip to content

Instantly share code, notes, and snippets.

View catdad's full-sized avatar
🍍
What's happening?

Kiril Vatev catdad

🍍
What's happening?
View GitHub Profile
@catdad
catdad / cancelEvent.js
Created March 31, 2014 14:38
Cancel event for modern browsers and old IE
//IE8 compatible cancel of event
ev.stopPropagation ? ev.stopPropagation() : ev.cancelBubble = true;
ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
.transparent-background{
/* IE8 transparent background fix -- #aaRRGGBB */
-ms-filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#a6000000', endColorstr='#a6000000');
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr='#a6000000', endColorstr='#a6000000');
background: rgba(0,0,0,.65);
}
@catdad
catdad / getStyle.js
Last active August 29, 2015 14:00
Gets the CSS style of a specific query selector and returns it in an object
function getStyle(className) {
var parseRuleText = function(rule){
var text = rule.split('{').pop().split('}').shift();
var cssRules = {};
var lines = text.split(';');
forEach(lines, function(line){
var rule = line.split(':');
if (rule[0].trim()) cssRules[rule[0].trim()] = rule[1].trim();
@catdad
catdad / forEach.js
Last active August 29, 2015 14:00
A simple, annotated forEach function
var forEach = function(obj, cb, context){
// check for a native forEach function
var native = [].forEach,
hasProp = Object.prototype.hasOwnProperty;
// if there is a native function, use it
if (native && obj.forEach === native) {
//don't bother if there is no function
cb && obj.forEach(cb, context);
}
@catdad
catdad / weirdMath.js
Created May 15, 2014 16:33
Handling some common IEEE Floating Point math issues
var isZero = function(val){ return Math.abs(val) < 1.0e-14; }
var isNegative = function(val){ return (val < 0) ? true : val === 0 && (1/val)===-Infinity; }
@catdad
catdad / isInsideViewableArea.js
Created May 15, 2014 20:03
Checks if an element is visible inside another element.
//this code will check if there is any overlap between two elements
//it was made to be used to check if a portion of a scrollably div is visible inside its wrapper
function isInView(containerBB, elBB) {
return (!(
elBB.top >= containerBB.bottom ||
elBB.left >= containerBB.right ||
elBB.bottom <= containerBB.top ||
elBB.right <= containerBB.left
));
}
/**
* support for `Element.getBoudingClientRect()` goes back to IE5, FF3, and Chrome 1
* it's great for getting the placement of a DOM element
* it is supposed to include `top`, `bottom`, `left`, and `right` in browser coordinates
* newer browsers also return `width` and `height`
* this code patches that, allowing all browsers access to the `widht` and `height`
*/
function standardizeBoundingBox(bb) {
//IE8 does not have width and height in bounding boxes
return {
//a constructor
function Thing(a, b, c, d){
//a private constructor
function Thing(){
//we don't care about d
this.d = d;
}
//a is read-only
Thing.prototype.getA = function(){ return a; };
@catdad
catdad / waterfall.js
Last active August 29, 2015 14:02
Based on the original sync gist -- https://gist.github.com/catdad/4598289 -- this code will execute asynchronous functions synchronously, passing the result of one function as the seed data of the next.
//simple sync function
function sync(/* funcsArray, done, seedDataArgs */) {
var args = Array.prototype.slice.call(arguments, 0);
//functions array to sync is first
var funcs = args.shift();
//done callback is second
var done = args.shift();
//all remaining arguments are the seed data
var counter = funcs.length;
@catdad
catdad / pad.js
Last active August 29, 2015 14:02
/**
* Pad a number, in string format, with leading zeros.
* @param {number|string} num The number to pad
* @param {number} [size] The target length
* @returns {string} The padded number.
*/
function pad(num, size) {
size = size || 2;
var s = num + "";
while (s.length < size) s = "0" + s;