/ Published in: jQuery
After being tasked with creating inputs like those on the twitter homepage I came up with this inline label system that uses jquery to control the labels (in the vase of the example it fase and slides them).
The code is in three parts. First, the input are formatted a very special way, don’t worry it’s basic and easy to implement. Take a look at the first section below. The input is obvious, as is the label, the last div, with the class .inputerror.firstname is some specific code for inline displayed error messages, I may make a post about them if there is any interest. The label is moved over the input box. Because of this it’s important to have the label properly formatted with the for="" element pointing to the appropriate input field.
Second is the CSS. Obviously the code below has some styling for looks, which is fully changeable based on your individual implementation, the important stuff is the display, overflow, and the margin-top on the .inputbox label, as well as the previously mentioned background in the .inputbox input style. You’ll notice that the inputbox doesnt have a width or a height (except where limited by the actual input inside it). That because thats all set via the individual inputs so you can vary it from input to input depending on what will go in it (just like the example has set up).
The last bit of code is the scripting. The first bit checks if there is a value to the input, if not it will reanimate the label back in, with a simple fade in in this case, but you do have full control over how it does it. Then, if there is something in the input it will fade it out, in this case by pushing it right while fading out. Once a label is completely faded out it is hidden so it won’t interfere with normal operation of the input box (like selecting or double clicking). It also makes it visible again before fading it back in.
All three parts put together make a nice animated inline label like the ones on the example page located after the url (scroll to the bottom of the post for a direct link). It’s been tested in IE and on mobile devices and seems to work fine albeit with slow or no animations on mobile devices.
If you’ve found a great use for this, converted it to another library (or no a library independent form), or have an questions or would like some help with modifications or implementation on your specific site leave a comment below or on the blog at the link.
The code is in three parts. First, the input are formatted a very special way, don’t worry it’s basic and easy to implement. Take a look at the first section below. The input is obvious, as is the label, the last div, with the class .inputerror.firstname is some specific code for inline displayed error messages, I may make a post about them if there is any interest. The label is moved over the input box. Because of this it’s important to have the label properly formatted with the for="" element pointing to the appropriate input field.
Second is the CSS. Obviously the code below has some styling for looks, which is fully changeable based on your individual implementation, the important stuff is the display, overflow, and the margin-top on the .inputbox label, as well as the previously mentioned background in the .inputbox input style. You’ll notice that the inputbox doesnt have a width or a height (except where limited by the actual input inside it). That because thats all set via the individual inputs so you can vary it from input to input depending on what will go in it (just like the example has set up).
The last bit of code is the scripting. The first bit checks if there is a value to the input, if not it will reanimate the label back in, with a simple fade in in this case, but you do have full control over how it does it. Then, if there is something in the input it will fade it out, in this case by pushing it right while fading out. Once a label is completely faded out it is hidden so it won’t interfere with normal operation of the input box (like selecting or double clicking). It also makes it visible again before fading it back in.
All three parts put together make a nice animated inline label like the ones on the example page located after the url (scroll to the bottom of the post for a direct link). It’s been tested in IE and on mobile devices and seems to work fine albeit with slow or no animations on mobile devices.
If you’ve found a great use for this, converted it to another library (or no a library independent form), or have an questions or would like some help with modifications or implementation on your specific site leave a comment below or on the blog at the link.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Part one, HTML layout for the forms <div class="inputbox"> <input type="text" name="firstname" id="firstname"/> <label for="firstname">Firstname</label> <div class="inputerror firstname"></div> </div> // Part the, the CSS style base (along with some for looks) .inputbox{ border: 1px black solid; display: inline-block; border-radius:5px; box-shadow: inset 1px 1px 5px #CCCCCC; padding:1px; margin-top:8px; margin-right:5px; margin-left:0px; overflow:hidden; } .inputbox input{ border: none; background: transparent; margin-top:-1px; font-size:20px; } .inputbox label{ display:block; font-size: 20px; font-family:sans-serif; letter-spacing: 2px; padding-left: 5px; margin-top:-25px; color: #777777; } // The actual scripting for the animation $(document).ready(function(){ inlinelabel(); }); function inlinelabel(){ $('div.inputbox > input').keydown(keyupdown); $('div.inputbox > input').keyup(keyupdown); function keyupdown(){ if($(this).val()==''){ $(this).parent().children('label').css('visibility','visible'); $(this).parent().children('label').stop(); $(this).parent().children('label').css('margin-left','0px') $(this).parent().children('label').animate({ opacity: 1, },200); }else{ $(this).parent().children('label').stop(); $(this).parent().children('label').animate({ opacity: 0, marginLeft: '50%', },200,function(){ $(this).parent().children('label').css('visibility','hidden'); }); } }; }
URL: http://fatfolderdesign.com/248/css/inline-input-labels-with-animation