Sunday, January 23, 2011

How does jQuery implement hide() and show()

By display: none
Not visibility: hidden


JQuery Source code:

hide: function( speed, easing, callback ) {
if ( speed || speed === 0 ) {
return this.animate( genFx("hide", 3), speed, easing, callback);

} else {
for ( var i = 0, j = this.length; i < j; i++ ) {
var display = jQuery.css( this[i], "display" );

if ( display !== "none" ) {
jQuery.data( this[i], "olddisplay", display );
}
}

// Set the display of the elements in a second loop
// to avoid the constant reflow
for ( i = 0; i < j; i++ ) {
this[i].style.display = "none";
}

return this;
}
}