JSFiddle - React, Tailwind, and code Playground
by codebaker
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<div ng-app="labelApp">
<h1>AngularJS floating labels</h1>
<p>Inspired by <a href="http://dribbble.com/shots/1254439--GIF-Mobile-Form-Interaction?list=users">Matt D. Smith</a>
</p>
<form role="form">
<div class="row">
<div class="form-group col-xs-12">
<input class="form-control" type="text" name="title" placeholder="Title" inline-label pos="top" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<input class="form-control" type="text" name="price" placeholder="Price" inline-label pos="bottom" />
</div>
</div>
</form>
</div>
CSS
.floating-label-form-group input, .floating-label-form-group textarea {
position: relative;
}
.floating-label-form-group label {
display: block;
position: absolute;
top: 2em;
opacity: 0;
z-index: 0;
font-size: 0.85em;
line-height: 1.764705882em;
vertical-align: middle;
vertical-align: baseline;
margin: 0;
-webkit-transition: top 0.5s ease, opacity 0.5s ease;
-moz-transition: top 0.5s ease, opacity 0.5s ease;
-ms-transition: top 0.5s ease, opacity 0.5s ease;
transition: top 0.5s ease, opacity 0.5s ease;
}
.floating-label-form-group-with-value label {
opacity: 1;
position: absolute;
padding-left: 12px;
}
.floating-label-form-group-with-value label.left {
top: 0;
left: -25px;
}
.floating-label-form-group-with-value label.right {
top: 0;
right: -25px;
}
.floating-label-form-group-with-value label.top {
top: -25px;
}
.floating-label-form-group-with-value label.bottom {
bottom: -25px;
top: auto;
}
/* DEMO STYLES */
form{
margin-top: 25px;
}
JavaScript
var labelApp = angular.module('labelApp', []);
labelApp.directive('inlineLabel', function () {
return {
restrict: 'EA',
replace: true,
transclude: false,
scope: {
label: '@placeholder',
pos: '@pos'
},
link: function (scope, element, attrs) {
//default
if (!attrs.pos) {
scope.pos = 'top'
}
$(element).parents('.form-group').addClass('floating-label-form-group');
$(element).before('<label class="' + scope.pos + '">' + scope.label + '</label>');
$('body')
.on("input propertychange", ".floating-label-form-group", function (e) {
$(this).toggleClass("floating-label-form-group-with-value", !! $(e.target).val());
});
$(element)
.on("focus", function () {
$(this).addClass("floating-label-form-group-with-focus");
});
$(element)
.on("blur", function () {
$(this).removeClass("floating-label-form-group-with-focus");
});
}
};
});