Created
August 31, 2011 11:43
-
-
Save addyosmani/1183358 to your computer and use it in GitHub Desktop.
Random snippets
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
/*This list of snippets doesn't really have any purpose. Just looking through ideas.*/ | |
@mathias: classList based on work by boaz https://gist.github.com/478061 | |
/* | |
Usages: | |
$(selector).classList() // returns an array of classnames | |
$(selector).classList('newclass') // replaces the current element’s classes | |
$(selector).classList(['new', 'class', 'names']) // replaces the current element’s classes | |
*/ | |
jQuery.fn.extend({ | |
classList: function(value) { | |
if (value) { | |
if (jQuery.isArray(value)) { | |
this.attr('class', value.join(' ')); | |
return this; | |
} else if (typeof value === 'string') { | |
return this.attr('class', value); | |
}; | |
}; | |
return this.attr('class').split(/\s+/); | |
} | |
}); | |
/*single and double click on same element*/ | |
https://gist.github.com/399624 | |
/*random list re-ordering with a callback*/ | |
https://gist.github.com/236696 (could be done better) | |
/*reverse variations*/ | |
jQuery.fn.reverse = function() { | |
return this.pushStack(this.get().reverse(), arguments); | |
}; | |
/*sort variations*/ | |
jQuery.fn.sort = function() { | |
return this.pushStack( jQuery.makeArray( [].sort.apply( this, arguments )) ); | |
}; | |
jQuery.fn.sort = function() { | |
return this.pushStack( [].sort.apply( this, arguments ), []); | |
}; | |
http://james.padolsey.com/javascript/sorting-elements-with-jquery/ | |
http://www.wrichards.com/blog/2009/02/jquery-sorting-elements/ | |
/*wait*/ | |
by @nlogax/ultror http://docs.jquery.com/Cookbook/wait | |
$.fn.wait = function(time, type) { | |
time = time || 1000; | |
type = type || "fx"; | |
return this.queue(type, function() { | |
var self = this; | |
setTimeout(function() { | |
$(self).dequeue(); | |
}, time); | |
}); | |
}; | |
/*more sort stuff*/ | |
var listitems = $(‘ul li’); | |
listitems.sort(function(a, b) { | |
var compA = $(a).text().toUpperCase(); | |
var compB = $(b).text().toUpperCase(); | |
return (compA compB) ? 1 : 0; | |
}); | |
/*Julian's pub/sub for $.Callbacks in 1.7*/ | |
https://github.com/jquery/jquery/commit/96b0089c9ce5c65f9d002966256d90e778345a2a | |
/*Faster getText*/ | |
http://forum.jquery.com/topic/sizzle-gettext-open-pull-request-for-ticket-6863#14737000002377958 | |
/*addPrior by @cowboy*/ | |
https://gist.github.com/1076570 | |
jQuery.fn.addPrior = function(selector) { | |
var prior = this.prevObject; | |
return this.add(selector == null ? prior : prior.filter(selector)); | |
}; | |
/*nextorfirst,prevorlast-http://www.mattvanandel.com/999/jquery-nextorfirst-function-guarantees-a-selection/*/ | |
$.fn.nextOrFirst = function(selector){ | |
var next = this.next(selector); | |
return (next.length) ? next : this.prevAll(selector).last(); | |
}; | |
$.fn.prevOrLast = function(selector){ | |
var prev = this.prev(selector); | |
return (prev.length) ? prev : this.nextAll(selector).last(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment