Animating the fixed location with CSS and JQuery

I'm attempting to create a seamless transition from one form field type to another. However, when I add another <input> element, the following elements do not animate into their static positions.

$("#switchlogin").click(function() {
  //Creating the label element
  var emaillabel = $("<label>").text("Email").attr("for", "email");
  //Creating the input element
  var emailinput = $('<input type="text">').attr({
    id: "email",
    name: "email"
  });

  //Creating the label element
  var pwdlabel = $("<label>").text("Repeat Password").attr("for", "password-wh");
  //Creating the input element
  var pwdinput = $('<input type="password">').attr({
    id: "password-wh",
    name: "password-wh"
  });

  $("#username-field").after(emaillabel);
  emaillabel.after(emailinput);

  $("#password-field").after(pwdlabel);
  pwdlabel.after(pwdinput);
});
input {
  border: solid #e3e3e3 1pt;
  display: block;
  margin-bottom: 1em;
  font-size: 14pt;
  opacity: 0;
  animation: fadeIn .3s forwards;
}
label {
  opacity: 0;
  animation: fadeIn .3s forwards;
}
@keyframes fadeIn {
  to {
    opacity: 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form action="<?php the_permalink(); ?>" id="login" method="post">
  <div id="username-field">
    <label for="username">Username</label>
    <input type="text" id="username" name="username">
  </div>

  <div id="password-field">
    <label for="password">Password</label>
    <input type="password" id="password" name="password">
  </div>

  <div id="buttons-field">
    <button type="submit">Login</button>
    <a href="#" id="switchlogin">Register</a>
  </div>
</form>

https://jsfiddle.net/fpvctpjs/2/

My goal is to have the password-field smoothly move down to accommodate the new elements when the register link is clicked.

I've experimented with adding styles like margin-top, position:absolute, and position:relative, but haven't been able to achieve the desired effect.

Any assistance would be greatly appreciated,
marsman

Answer №1

The reason why your password field is not animating smoothly is because it relies on the movement of other appended fields.

An effective solution would be to keep the other fields always present but concealed within a div with height:0;—and when the reset button is clicked, smoothly animate to the intended height of that div while simultaneously fading in the child inputs.

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

Nothing is in the Laravel array when using `$request->all()`

In the process of developing a shopping cart using Laravel. Here are the details : Routes : Route::post('/cart/add', 'CartController@store')->name('cart.store'); Route::patch('/cart/{product}', 'CartContro ...

Exploring the concept of union return types in TypeScript

Hello, I am facing an issue while trying to incorporate TypeScript in a way that may not be its intended use. I have created a custom hook called useGet in React which can return one of the following types: type Response<T> = [T, false, false] | [nul ...

Android browser experiences a sudden surge of unexpected data influx

I am facing an issue with my application where it maps an array from the state. The array should ideally only contain 6 sets of data, as limited by the backend. However, sometimes it spikes and displays data that is not supposed to be there or shows old da ...

Guide on retrieving the initial value from HTML and passing it to the controller in AngularJS during page load

<ul> <li ng-init="formData.mdpSunday='5'"> <input ng-model="formData.mdpSunday" name="mdpSunday" type="radio" value="5"> </li> </ul> app.controller(&a ...

Even after applying trim() function, PHP's return statement still adds spaces unnecessarily

My function is supposed to return a boolean, but for some reason, it is adding spaces at the beginning of the string. Even after using trim(), the spaces persist. What could be causing this unexpected behavior? PHP function checkFile($v){ $result = is_ ...

"The printing function of the "Print page" button appears to be malfunctioning

I am having some trouble with my JavaScript code for a "print page" button in an HTML document. The button appears and is clickable, but it doesn't actually print the page as intended. I keep receiving 3 errors related to the `document` object being u ...

Tips for displaying HTML content using an array in Vue JS

Hi, I'm a beginner with Vue JS and I'm working on passing an HTML table using this array. I have a dropdown where I can select the option I want, but I'm struggling to figure out how to include HTML within it. Whenever I try, it ends up disp ...

Observable<void> fails to trigger the subscriber

I am currently facing a challenge with implementing a unit test for an Observable in order to signal the completion of a process. While there is no asynchronous code in the logout function yet, I plan to include it once the full logic is implemented. The m ...

Navigating on the horizontal axis within a container with overflow properties

I am trying to insert multiple children-divs into a parent-div. The parent div has a fixed width. The children are set to float left and will overflow the container. I need the ability to scroll along the x-axis. However, when I attempt to do this, the c ...

A step-by-step guide on injecting HTML code dynamically within a Django application

I have a project that involves creating an online encyclopedia. To achieve this, I must generate a webpage for each entry file written in Markdown. Converting Markdown to HTML is essential before uploading them to the site. Instead of relying on external l ...

Align the content of a collapsed bootstrap row vertically

In a row, I have two columns, each containing their own row. When the page width hits the 'xs' break-point, the left column's row will stack its columns on top of each other. The right column is taller than the left column, which leaves a l ...

Issue with Stack Divider not appearing on Chakra UI card

I'm currently designing a card element using Chakra UI. However, I've noticed that the Stack Divider in the Card Body isn't displaying as expected. Is there a specific way it needs to be structured for it to show up? For example, should I se ...

What are the drawbacks of incorporating side effects into JavaScript constructors?

In my coding, I often utilize a design pattern similar to the concept of custom objects. However, JSLint disapproves of code constructs like this: function MyClass() { this.init(); } new MyClass(data); The reason being that the newly created object is d ...

Submitting feedback using ajax and jquery

I need help figuring out how to display the posted comment under all existing comments, similar to Facebook's setup. Here is the code snippet I currently have: // Intercepting the submit event $('#CommentAddForm').submit(function() { ...

Error Encountered While Creating a Polygon Wallet on Fireblocks

After following the instructions from Fireblocks Docs, I successfully created a default wallet named "BTC_TEST" like this: enter image description here. However, when attempting to create a Matic wallet, I encountered an Axios Error. Despite Matic being a ...

Passing a variable between two PHP files and then showcasing its value in a separate third file

bookincome.php receives several inputs, and bi.php calculates the total net income based on the values provided by bookincome.php. It then displays a table containing all the user-given values and the calculated total book income ($tnibeforetax) value. How ...

Having difficulty including my JavaScript file located in /app/assets/javascripts when using Rails 4 and "//= require_tree ." in application.js

I'm currently attempting to implement a d3.js graph into my Rails 4 application by following a tutorial found on this website. The example application provided on GitHub works perfectly as expected. The issue I am facing is that when I try to recreat ...

establishing a predetermined dimension using css and integrating it into javascript

I'm a beginner in CSS and JavaScript and I've been searching through various sources for an answer to my question without any luck. My goal is to pre-set the sizes of all sections using CSS and then utilize these sections in JavaScript. However, ...

Utilizing the sAjaxSource property in Datatables to fetch data through Ajax from multiple tables while dynamically passing arguments

I am facing an issue with populating two datatables using data retrieved from a flask API through a GET request. My data source URL is localhost:5000/data, but for some reason, I am unable to display the data in the datatables. Interestingly, when I use a ...

Controlling the DOM with Angular 2

I am currently developing a website in Angular 2. I have successfully implemented a JQuery code that adds 2 input fields and 1 delete button when a custom button is clicked. When the user clicks on the delete button, it removes the inputs by manipulating t ...