Textarea Dynamic Height & Angular Material Effect
by Venkatesh Dematti
HTML
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6">
<form action="/action_page.php">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="border_animation" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="border_animation" id="pwd">
</div>
<div class="form-group">
<label for="my__text"></label>
<textarea id="my__text" class="border_animation border_animation" name="webinar_interest" placeholder="Please add your comments"></textarea>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
CSS
.border_animation {
display: block;
width: 100%;
border: 0;
background: white no-repeat;
background-image: linear-gradient(to bottom, #64b448, #64b448), linear-gradient(to bottom, #ccc, #ccc);
background-size: 0 2px, 100% 1px;
background-position: 50% 100%, 50% 100%;
transition: background-size 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
}
.border_animation:focus {
background-size: 100% 2px, 100% 1px;
outline: none;
}
/* Not required */
html {
overflow: scroll;
overflow-x: hidden;
}
::-webkit-scrollbar {
width: 10px;
/* Make width zero, if you want to hide the scrollbar and retain scrolling */
background:#fff;
}
::-webkit-scrollbar-thumb {
background: #64b448;
}
JavaScript
var textarea = document.querySelector('textarea');
textarea.addEventListener('keydown', autosize);
function autosize(){
var el = this;
setTimeout(function(){
el.style.cssText = 'height:auto; padding:0';
// for box-sizing other than "content-box" use:
// el.style.cssText = '-moz-box-sizing:content-box';
el.style.cssText = 'height:' + el.scrollHeight + 'px';
},0);
}