Issue with animation transition not triggering on initial click

I am currently working on creating an animation for a form using JS and CSS. Here is the code snippet I have:

var userInput = document.getElementById("login-user");

var userLabel = document.getElementById("user-label");

// User Label Functions
function activateUser() {
  userLabel.style.transition = "0.2s";
  userLabel.style.top = "-1.5vw";
}

function deactivateUser() {
  if (userInput.value.length == 0) {
    userLabel.style.top = "0vw";
  }
}
#login-user {
  margin-bottom: 3vw;
  width: 80%;
  height: 3vw;
  border-radius: 0.5vw;
  border: solid #0057e0 0.1vw;
  background: none;
  color: #ddd;
  text-indent: 0.5vw;
  font-size: 1.5vw;
}

#user-label {
  color: black;
  font-size: 2vw;
  position: relative;
  left: 8vw;
  background: #fff;
  margin-right: -2vw;
  font-family: 'Open Sans';
  cursor: text;
  transition: 0.2s;
}
<label for="login-user" onclick="activateUser()" id="user-label">Username</label>
<input type="text" name="user" id="login-user" onfocusout="deactivateUser()" onclick="activateUser()" onfocus="activateUser()">

Upon running the provided code, you will notice that the "username" label jumps up instantly without any transition. However, when the onfocusout event occurs, it smoothly transitions back down. My goal is to achieve a smooth transition when the input field is activated, but I'm unsure why it's not happening.

Answer №1

One reason for this is that #user-label needs to have the initial value of top defined. Simply include top: 0; in your code and it will function properly.

var userInput = document.getElementById("login-user");

var userLabel = document.getElementById("user-label");

// User Label Functions
function activateUser() {
  userLabel.style.transition = "0.2s";

  userLabel.style.top = "-1.5vw";
}

function deactivateUser() {
  if (userInput.value.length == 0) {
    userLabel.style.top = "0vw";
  }
}
#login-user {
  margin-bottom: 3vw;
  width: 80%;
  height: 3vw;
  border-radius: 0.5vw;
  border: solid #0057e0 0.1vw;
  background: none;
  color: #ddd;
  text-indent: 0.5vw;
  font-size: 1.5vw;
}

#user-label {
  color: black;
  font-size: 2vw;
  position: relative;
  left: 8vw;
  background: #fff;
  margin-right: -2vw;
  font-family: 'Open Sans';
  cursor: text;
  transition: 0.2s;
  top: 0;
}
<label for="login-user" onclick="activateUser()" id="user-label">Username</label>
<input type="text" name="user" id="login-user" onfocusout="deactivateUser()" onclick="activateUser()" onfocus="activateUser()">

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

Tips for deactivating all JavaScript events on a webpage that has been loaded within an iframe

My website is equipped with various JS click/mouseover/mouseout/scroll events and incorporates several JS libraries to operate features such as carousels, bootstrap modal pop-ups, and more. I am looking to halt all events on a specific element. Although I ...

Using the Context API dispatch (consumer) within the _app.js class component in Next.js

How can I access the dispatch Context API methods in the _app.js file? The issue I am facing is that I am using React hooks along with Context API, and as _app.js is a Class component, I cannot directly use hooks within it. Below is my current code snipp ...

Use the jQuery .GET() method two times to retrieve data and obtain the outcomes

My code involves making a series of GET calls where the returned data from one call is used in another call before returning the final results. However, I want to ensure that my program waits until all the data is retrieved. This is what I have come up wi ...

Vue.js: The href attribute in a link is different from the data

Having <a> href tag below, clicking will redirect to www.duckduckgo.com, with the value of page.publicWebsite being displayed as well. {{page.publicWebsite}} shows www.duckduckgo.com. Is everything functioning correctly? https://i.stack.imgur.com/ ...

Oops! The system encountered a problem: the property 'modalStack' is not recognized on the type 'NgxSmartModalService'. Maybe you meant to use '_modalStack' instead?

Currently, I'm facing an issue while attempting to run ng build --prod in my Angular 6 project. I have also incorporated the NgxSmartModal package for handling modals. Unfortunately, the build process is failing and I can't seem to figure out why ...

How to Modify CSS in Angular 6 for Another Element in ngFor Loop Using Renderer2

I have utilized ngFor to add columns to a table. When a user clicks on a <td>, it triggers a Dialog box to open and return certain values. Using Renderer2, I change the background-color of the selected <td>. Now, based on these returned values, ...

Creating HTML elements dynamically with attributes based on the `as` prop in React using TypeScript

Is there a way to dynamically create HTML elements such as b, a, h4, h3, or any element based on the as={""} prop in my function without using if guards? I have seen a similar question that involves styled components, but I am wondering if it can be done ...

What's the secret behind generating a crisp 400 on paper?

Understanding Why it Prints 400 I'm struggling to comprehend the logic behind this var x = {}, y = { key: "y" }, z = { key: "z" }; x[y] = 100; x[z] = 200; console.log(x[y] + x[z]); ...

What is the best way to create a global variable using jQuery?

Is it possible to pass the value of a_href = $(this).attr('href'); function globally and set a_href as "home"? var a_href; $('click a').on('hover', function(e){ a_href = $(this).attr('href'); ...

Using React to Retrieve Array Data from an API and Implementing Filtering

I have successfully made a call to my API endpoint and passed the token. However, I only need specific details from the response data - specifically, I want to work with the first index in the array that contains fields. My goal is to fetch all the relevan ...

Add a flexible element to a container without disrupting the alignment of the other

I'm facing a challenge in adding an action button to the end of a grid layout without affecting the centering of the items. The button should seamlessly fit into the grid, just slightly offset. If you check out my demo, you'll see what I mean. ...

Is there a way to add a price to an object in JavaScript?

Purchasedata.find(function(err, purchasedatas) { if (err) { return handleError(res, err); } var totalprice = 0; for (var i = 0; i < purchasedatas.length; i++) { findProduct(i, function(i, price) { }); } ...

Guide to implementing PCF (SOFT) shadows with three.js

Is it possible to apply the PCF (SOFT) shadow type, like the one found in the Three.js online editor, to your renderer using javascript code? https://i.sstatic.net/x0QmH.png ...

Stripping HTML elements from the body of an HTML document using AJAX before transmitting it as data to the controller

My JSP page contains two buttons labeled "download" and "sendemail". When the "Sendmail" button is clicked, an ajax method is triggered to generate a PDF version of the HTML body and send it to the back-end controller. I attempted to utilize the following ...

Finding a specific cell in a Django HTML table: tips and tricks

Recently delving into django and its intricacies. Struggling to grasp the concept of accurately pinpointing a specific cell within an html table. Yearning to modify the background color of select cells based on certain conditions derived from the generate ...

Mix up table data cells using Javascript/jQuery

Can anyone provide me with some helpful tips? I'm really struggling with this. My Objective I aim to create a table with two columns ("name" and "rating"), consisting of 5 rows. 2 of these rows should have a random "rating" between 6-10 2 other ro ...

What is the solution to prevent angular-material components from covering my fixed navbar?

After creating a navbar using regular CSS3, I incorporated input fields and buttons from angular material. However, my sticky navbar is being obscured by the components created with angular material. Here is the CSS for the navbar: position: sticky; top: ...

Creating a responsive web design with a user-friendly CSS grid layout

Currently, I am delving into the realm of CSS grid to better comprehend its functionality. In my layout design, there are two menus (colored in red) positioned on either side of a central main content box (colored in blue). As the screen size reduces and ...

Switch Focus and Collapse Submenus upon Menu Click in Recursive React Menu

I've created a dynamic menu system in React using Material-UI that supports recursion for submenus. I'm aiming to implement the following features: 1. When a menu item is clicked, all other open submenus should close and focus on the clicked men ...

Incorporating various language URLs in Next.js using next-i18n-next functionality

Thinking about using the next-i18next internationalization library. Check out next-i18next on GitHub Curious about how to change the language in the path of my URL. For example: /about-us /over-ons -> takes you to the Dutch version of the about us p ...