http://jsfiddle.net/Calum/5dqwJ/
CSS:
textarea {
height: 20px;
margin: 20px;
width: 585px;
resize: none;
overflow: hidden;
vertical-align: top;
transition: height 3s;
-webkit-transition: height 3s;
-moz-transition: height 3s;
-o-transition: height 3s;
}
Javascript:
$(document).ready(function () {
$("textarea").focus(function (event) {
if ($(this).val().length === 0) {
$(this).height(100);
$(this).css('resize', 'vertical');
$(this).attr('placeholder', null);
}
});
$("textarea").blur(function (event) {
if ($(this).val().length === 0) {
$(this).height(20);
$(this).attr('placeholder', "Enter comment");
$(this).css('resize', 'none');
}
});
});
HTML:
<textarea placeholder='Enter comment'></textarea>
In Chrome, IE and Opera, when you focus on the textarea it expands and contracts with the CSS3 transition.
When I do the same with FireFox, it doesn't animate anything.
Any ideas why?
Thanks!
see This page for transitions
https://developer.mozilla.org/en-US/docs/CSS/Tutorials/Using_CSS_transitions
and you can do it completely by css no need to use js
remove $(this).height(20); and $(this).height(100); From JS and ADD this To css
textarea:focus{
height: 100px;
}
If you add the required attribute on your textarea, you could use the :invalid pseudo-class to target an empty textarea:
textarea {
height: 100px;
margin: 20px;
width: 585px;
resize: vertical;
overflow: hidden;
vertical-align: top;
-webkit-transition: height .3s;
-moz-transition: height .3s;
-o-transition: height .3s;
transition: height .3s;
}
textarea:invalid{ /* single line*/
height: 20px;
resize: none;
}
textarea:focus{
height: 100px;
resize: vertical;
}
demo
(add box-shadow: none; if you don't want the red border around it)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With