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
// simple equal column heights | |
// sets the height of each matched div to that of the tallest element in the collection | |
// Usage: $("#div1, #div2, #div3").equalHeights(); | |
$.fn.equalHeights = function(){ | |
var heights = [], i = this.length; | |
while(i--){ heights[i] = this.eq(i).height(); } | |
return this.height(Math.max.apply(Math, heights)); | |
} |
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
// determine if an element is COMPLETELY inside the current viewport. | |
// surely, there must be an easier way | |
$.expr[':']['in-viewport'] = function(element){ | |
var $win = $(window), $el = $(element), | |
// element calculations | |
offset = $el.offset(), | |
outerWidth = $el.outerWidth(), | |
outerHeight = $el.outerHeight(), |
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
// extremely basic templating with replace(). | |
var | |
// template | |
template = '<div id="#{id}">#{text}</div>', | |
// object literal of replacement values | |
params = { id:10, text:'hi there' }, |
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
// Skeleton jQuery Plugin (OO) | |
(function($){ | |
$.fn.myPlugin = function( options ){ | |
options = $.extend( {}, $.fn.myPlugin.defaults, options ); | |
return this.each(function(){ | |
// create a new object & store it in the element's data for easy access | |
$(this).data('myPlugin', new MyPlugin(this, options)); |
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
// accessing all OTHER instances of a widget, from within your widget. | |
// method one: use your own data store | |
var instances = []; | |
$.widget("ui.dialog", { | |
_create: function(){ | |
instances.push(this.element); | |
}, |
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
(function($){ | |
$.widget("ui.mywidget", { | |
options: { | |
autoOpen: true | |
}, | |
_create: function(){ | |
// by default, consider this thing closed. |
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
// recursive setTimeout, better than setInterval. @paul_irish | |
(function f(){ if(condition){} else{ setTimeout(f, 1000); } })(). |
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 | |
$this.find("li") | |
.wrapInner('<a href="#" style="display:block" />') | |
.delegate("a", "click", function(){ | |
$this.dialog("close"); | |
return false; | |
}); | |
// B |
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
// old and busted | |
var foo = $(".bar"); | |
if( foo.length ){ | |
foo.addClass("LOL"); | |
} | |
// new hotness |
OlderNewer