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
(function($) {
$.fn.hasScrollbar = function() {
return this.get(0).scrollHeight > this.height();
}
})(jQuery);
@catdad
catdad / sync.js
Last active December 11, 2015 12:08
This function can be used to sync many asynchronous functions. You only have to maintain the list and order once, and they will execute one after the other, as well as a callback after all the functions are done.
function firstFunction(next){
console.log('first function');
setTimeout(next, 1000);
}
function secondFunction(next){
console.log('second function');
setTimeout(next, 1000);
}
@catdad
catdad / parseQuery.js
Last active December 17, 2015 05:59
parse the query string of a URL and return an object
function parseQuery(){
var query = {};
var temp = window.location.search.substring(1).split('&');
for (var i = temp.length; i--;) {
var q = temp[i].split('=');
query[q.shift()] = q.join('=');
}
return query;
}
@catdad
catdad / viewportDisplay.js
Last active December 18, 2015 05:09
Displays the browser viewport for dev purposes
(function viewport(){
//create element
var el = document.createElement("div");
//style as you wish
el.id = 'screenSize';
el.style.position = "fixed";
el.style.width = "100%";
el.style.left = "0px";
el.style.bottom = "5px";
@catdad
catdad / scrollTo.js
Last active December 20, 2015 00:58
Scroll to element
//scroll to element
function scrollTo(el, offset){
el.offsetParent && window.scroll(0, (function(){
var position = 0;
do { position += el.offsetTop; }
while (el = el.offsetParent);
return position - (+offset || 0);
})());
}
@catdad
catdad / fontsInPage.js
Last active December 22, 2015 05:28
fonts in page
function styleInPage(css, verbose){
if(typeof getComputedStyle== "undefined")
getComputedStyle= function(elem){
return elem.currentStyle;
}
var who, hoo, values= [], val,
nodes= document.body.getElementsByTagName('*'),
L= nodes.length;
for(var i= 0; i<L; i++){
who= nodes[i];
@catdad
catdad / deferred.js
Created September 9, 2013 18:48
jQuery deferred sample
//make sure you have jQuery :)
//<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
(function(jQuery){
console.log("adding stuff to jQuery");
jQuery.fn.myExtension = function(){
console.log("I'm asynchronous");
var defer = $.Deferred();
function generator(low, high, post){
return function(override){
if (!post || typeof post !== "function") post = function(arg){return arg};
return post( Math.random() * (high - low) + low );
}
}
//example
var rand = generator(0,10); // random doubles between 0 and 10
var rand = generator(1,100,Math.round); // random ints between 1 and 100
@catdad
catdad / ready.js
Created March 6, 2014 20:47
A document.ready event
!function(){
var ready = function(){
//do stuff now!
};
var readyCheck = function(){ (document.readyState === 'complete') && ready(); };
if (document.addEventListener){
document.addEventListener('readystatechange', readyCheck, false);
}
else {
@catdad
catdad / sayNumbers.js
Last active August 29, 2015 13:57
This is a rough draft of code that will type out in words the number that you give it. For example, `say(123)` will return `one hundred twenty three`.
var oneDigit = {
'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five',
'6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'
};
var teens = {
'10': 'ten', '11': 'eleven', '12': 'twelve', '13': 'thirteen',
'14': 'fourteen', '15': 'fifteen', '16': 'sixteen',
'17': 'seventeen', '18': 'eighteen', '19': 'nineteen'
};
var twoDigit = {