http://thecodeplayer.com/walkthrough/animating-float-labels-jquery-css3
Float labels are cool for small screens and can be awesomely animated by breaking down the labels into individual characters and then playing around with it.
In this walkthrough, when the user focuses on an input field individual letters are animated into an HTML fieldset like feel using jQuery.
//breakdown the labels into single character spans
$(".flp label").each(function() {
var sop = '<span class="ch">'; //span opening
var scl = '</span>'; //span closing
//split the label into single letters and inject span tags around them
//$(this).html(sop + $(this).html().split("").join(scl+sop) + scl);
console.log($(this).html(), sop + $(this).html() + scl);
$(this).html(sop + $(this).html() + scl);
//to prevent space-only spans from collapsing
$(".ch:contains(' ')").html(" ");
})
var d;
//animation time
$(".flp input").focus(function() {
//calculate movement for .ch = half of input height
var tm = $(this).outerHeight()/2 *-1 + "px";
//label = next sibling of input
//to prevent multiple animation trigger by mistake we will use .stop() before animating any character and clear any animation queued by .delay()
$(this).next().addClass("focussed").children().stop(true).each(function(i) {
d = i*50;//delay
$(this).delay(d).animate({top: tm}, 200, 'easeOutBack');
})
})
$(".flp input").blur(function(){
//animate the label down if content of the input is empty
if($(this).val() == "") {
$(this).next().removeClass("focussed").children().stop(true).each(function(i) {
d = i*50;
$(this).delay(d).animate({top: 0}, 500, 'easeInOutBack');
})
}
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.