trigger a CSS animation using JavaScript upon a specified event

I am attempting to incorporate a class for an autogrow animation effect to a textarea element.

Here is a demo: http://jsfiddle.net/d0kmg7d3/15/

var tx = document.getElementsByTagName('textarea');
for (var i = 0; i < tx.length; i++) {
  tx[i].setAttribute('style', 'height:' + (tx[i].scrollHeight) + 'px;overflow-y:hidden;');
  tx[i].addEventListener("input", OnInput, false);
}

function OnInput(e) {
  this.style.height = 'auto';
  this.style.height = (this.scrollHeight) + 'px';


}

How can I trigger the animation only when the height changes? Perhaps something like this:

this.classList.toggle("horizTranslate")

But how can I detect when the height is altered?

Answer №1

Perhaps you can achieve this without JavaScript, using only pure CSS. The key attributes to focus on are transition and max-height.

I have made some modifications to your code in the example below, hoping that it will be helpful for you.

Take a look at the code snippet provided:

var tx = document.getElementsByTagName('textarea');
/*for (var i = 0; i < tx.length; i++) {
  tx[i].setAttribute('style', 'height:' + (tx[i].scrollHeight) + 'px;overflow-y:hidden;');
  tx[i].addEventListener("input", OnInput, false);
}*/

function OnInput(e) {
  this.style.height = 'auto';
  this.style.height = (this.scrollHeight) + 'px';
 
}
textarea {
  max-height: 40px;
  height: 500px;
  transition: max-height 0.5s;
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid rgba(0, 0, 0, 0.5);
  border-radius: 3px;
  padding: .5em;
  font-size: 1em;
}

textarea:focus {
  background: orange;
  height: 200px;
  max-height: 200px;
}

body {
  font-family: sans-serif;
  font-size: 16px;
}
<textarea class="horizTranslate" placeholder="Type, paste, cut text here..."></textarea>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with Bootstrap 4: Dropdown menu extends beyond the edge of the screen towards the right side

I'm having trouble with the dropdown items extending off the page. I've tried a few solutions from BS3 but they don't seem to be working for me. I suspect it might have something to do with the ml-auto class. (please disregard the if-else st ...

Error encountered: AngularJS routes causing 500 internal server error

I am struggling with organizing my directory structure. Here is how it currently looks - -project -public -app -app.js ( angular app module ) -server -server.js ( node root js file ) -includes -layout.jade - ...

Code for switching between accordion panels with a simple button press

I took some code snippets from a website and developed a shopping cart accordion module. The complete code is available on my CodePen profile, you can access it through this link: http://codepen.io/applecool/pen/YXaJLa <div class="summary"> <but ...

Using ASP.net MVC 4 to Implement Validation with Bootstrap Modal and PartialView

After switching from a simple View with validation to using a bootstrap modal and PartialView in my application, I encountered some issues. The client-side validation no longer works and the server-side validation redirects me to a new page instead of disp ...

Effortlessly initiate the consecutive playback on SoundCloud

I'm currently working on my music blog and I'm looking for some guidance in implementing a feature where the widgets start playing one after the other. Specifically, I have both SoundCloud and YouTube widgets, but I would like to focus on achievi ...

What is the reason behind the restriction on retrieving data from an API within the constructor of a React Component?

After conducting thorough research, it has come to my attention that fetching data in the constructor of a React component can lead to potential issues. I am seeking a detailed explanation with examples illustrating the specific troubles that may arise f ...

Issue encountered when assigning a new value to a private class property at a global scope in JavaScript

I am encountering an issue in my code where I define a private class member using "#", initialize it in the constructor, and then receive an error when attempting to set/access the value. Uncaught TypeError: Cannot read private member #mouse from an object ...

Is there a way to ensure the collapsible item stays in its position?

I'm encountering an issue with the display of items within collapsible cards. Here is what it currently looks like: https://i.sstatic.net/DM8sX.png And this is how I want it to appear: https://i.sstatic.net/BXGpW.png Is there a way to achieve the ...

Line of cubes - tap on a single cube and the others gracefully slide away from view

I am working on a project where I need to create a row of blocks. The goal is for the blocks to slide off the screen when one is clicked, leaving the clicked block in the first position. For instance: https://i.sstatic.net/IB5LI.jpg I attempted using jQ ...

Getting started with the AngularJs plugin is the first step in building dynamic web applications. However, a common issue arises when the browser is

Exploring the world of AngularJS has been an interesting journey for me, as outlined in the title. I recently came across a useful drag and drop plug-in that I wanted to incorporate into my website. However, after downloading the code from GitHub, I faced ...

The "div width 100%" property functions flawlessly when tested locally, yet fails to operate on the live server

Hello, I recently launched my new website at www.leafletsdistributors.com. However, I'm encountering an issue with the Get in touch section (the light grey colored area). Despite setting the width to 100%, it's not fully extending across the scre ...

What is the best way for my controller to access the appropriate view component in extjs?

In my project, I have defined a Ext.grid.Panel named JobList, which includes an Ext button with the unique identifier myButton. The JobList also has its own controller. Within the controller, I have incorporated the following code: Ext.define('App.co ...

Unable to update innerHTML of asp.net literal tag using JavaScript

Here is a snippet of my JavaScript code: <script type="text/javascript">function CountCharacters(idTxtBox, idCharCount) { var maxLimit = 500; var remainingChars = maxLimit - document.getElementById(idTxtBox).value.length; do ...

Is there a way to change the color of the icon when the input fields are filled?

I am trying to change the color of the related icon for inputs when they are focused on or not empty, but my code doesn't seem to be working correctly for non-empty inputs. <div class="form-group"> &l ...

Tips for showcasing the currently active item in a Bootstrap dropdown menu using jQuery

I am trying to highlight the menu item that is selected in a drop-down menu. I attempted the following code, which works if I prevent the default behavior of the link element, but I do not want to do that. So I tried using local storage instead, but it d ...

Need to style today's date and selected date differently using react datepicker

I am currently working with the React Datepicker and I am facing an issue where two different styles need to be applied for the current date and a selected date. Specifically, I have defined a style for the current date as well as a separate style for whe ...

Is there a problem with using Nth-Child / Nth-Of-Type in CSS when classes are dynamically added or removed using jQuery?

Currently, I have a set of LI tags. As users filter the LIs, some may be removed from view. My goal is to assign alternating background colors to the visible LIs. When an LI is visible, it is given the class "LI-Shown." If an LI gets filtered out, I remov ...

Three.js - Placing 2D elements within a 3D environment using Vertices

Looking for advice on drawing a face in 3D Space using an array of 3D Points. How can I achieve this? Essentially, I want to create a flat object in a 3D environment. My current approach involves connecting points Points[0] to Points[1], Points[1] to Poin ...

Exploring the array push method in ES6 destructuring

Is it possible to use something similar to the destructing assignment feature in ES6 to write cleaner code when pushing items into an array? I'm unsure how to implement it properly, especially within a context like Vue.js. Here is an example code snip ...

Is it not possible to populate select fields by entering values into the input field?

Hello everyone, I hope you are all doing well. The issue at hand is... I have an input field where entering a value should display a select box. However, I am only able to see one and it's not meeting the required values. The second problem arises w ...