Created
September 18, 2014 19:33
-
-
Save solepixel/59dcc84dc25d68741ed0 to your computer and use it in GitHub Desktop.
Fade Labels for jQuery
This file contains hidden or 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
/** | |
* Fade Labels | |
* | |
* @version 1.0.0 | |
* @author Brian DiChiara | |
* @website http://briandichiara.com | |
*/ | |
(function ( $ ) { | |
$.fn.fadeLabel = function( options ){ | |
var settings = $.extend({ | |
label : false | |
}, options ); | |
return this.each(function(i, el){ | |
var $field = $(el); | |
var $label = false; | |
if( settings.label ){ | |
$label = $field.closest( settings.label ); | |
} | |
if( ! $label.length ){ | |
$label = $field.next('label'); | |
} | |
if( ! $label.length ){ | |
$label = $field.siblings('label:first'); | |
} | |
if( ! $label.length ){ | |
$label = $field.parents('label').find('.label'); | |
} | |
if( ! $label.length ){ | |
$label = $field.parents('li:first').find('label'); | |
} | |
if( ! $label.length){ | |
$label = $field.parents('p:first').find('label'); | |
} | |
if( ! $label.length){ | |
$label = $field.parents('form:first').find('label'); | |
} | |
if( ! $label.length ){ | |
console.log( 'fadeLabel could not find a label.' ); | |
return; | |
} | |
if( $field.val() ){ | |
$label.hide(); | |
} | |
$field.focus(function(e){ | |
if( ! $field.val() ){ | |
$label.fadeTo( 'normal', .2 ); | |
} else { | |
if( $label.css('opacity') > 0 ){ | |
$label.fadeTo('fast', 0); | |
} | |
} | |
}).blur(function(e){ | |
if( ! $field.val() ){ | |
$label.fadeTo( 'normal', 1 ); | |
} else { | |
if( $label.css('opacity') > 0 ){ | |
$label.hide(); | |
} | |
} | |
}).keyup(function(e){ | |
if( $field.val() ){ | |
if( $label.css('opacity') > 0 ){ | |
$label.fadeTo( 'fast', 0 ); | |
} | |
} else { | |
$label.fadeTo( 'normal', .2 ); | |
} | |
}); | |
}); | |
}; | |
}( jQuery )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment