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

The issue of a centered image not displaying properly and the inability to adjust the font

I'm trying to get the image to display bigger and in the center, but no matter what I try it's not working. HTML: <div class="content-grid"> <img src="test.jpg" /> </div> CSS: .content-grid img{ display: block; margin: ...

How can I implement an AJAX request with MongoDB in Node/Express?

Let's begin with a simple webpage: an HTML Form, a button, and a div-box. When the button is clicked, the Form data will be sent via AJAX. The data will then be stored in MongoDB and retrieved into the div-box seamlessly without any page refresh. A ...

Trouble arises with Pagination feature of Mui Data Table

Currently, I am working on a project that involves retrieving data from the CoinMarketCap API and presenting it in an MUI Data Table (specifically a StickyHeader Data Table). However, I have been encountering difficulties with changing the color of the tex ...

Incorporating the use of font color in CSS styles and

I am creating a newsletter template using foundation. Within the table, there are 2 <th> elements containing the content "Reservationnumber" and "23425-FF3234". I have multiple instances of these <th>. I want to apply a colored font to them. Wh ...

CSS Trick: Applying first-of-type selector to specific instances of a div

I'm currently grappling with how to specify that a 'first-of-type' should only be present on specific instances of a div. For instance, suppose I have a class called ".maintext" and in some cases I want the first paragraph to feature a dropc ...

Crafting interactive image checkboxes

At present, the checkboxes in my application are quite basic with labels. However, our app designer has requested that we revamp this feature and make it more appealing by using clickable images that still function like checkboxes. Allow me to provide an ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Leverage the power of express-session in your NextJS project

Currently, I am working on developing a login system using NextJS and MySQL. I am looking to implement sessions for user login, but I am unsure of how to integrate express-session with NextJS. Can anyone provide guidance on whether express-session can be ...

The navigation bar vanishes as soon as I click the Modal button

I am experiencing an issue where the Navbar disappears when I open a modal while using Bootstrap. Navbar before opening Modal https://i.stack.imgur.com/Oo9qb.png Navbar after opening Modal https://i.stack.imgur.com/dlK1V.png <script src="https:// ...

The X axis labels are missing on the Google column chart

Problem: All column charts are rendering correctly in Internet Explorer. However, Upon clicking the "View Build Performances" button, project names are displayed on the x-axis of the first three column charts only. The other column charts do not show pro ...

Ways to troubleshoot setTimeout functioning during HTTP requests?

Utilizing Socket IO in my client app, I have implemented a feature to check for unread events. The process involves making a request to the backend, which then sets a 5-second timeout before checking for any new events and sending them back. // client soc ...

What is the best way to trim a string property of an object within an array?

I am seeking a solution to access the "description" property of objects within an array and utilize a string cutting method, such as slice, in order to return an array of modified objects. I have attempted using for loops but have been unsuccessful. Here ...

Tips for updating the appearance of material-ui table pagination

I'm looking to enhance the way table pagination is presented in material-ui. The current default display by material-ui looks like this: https://i.stack.imgur.com/d4k4l.png My goal is to modify it to look more like this: https://i.stack.imgur.com/A ...

Clicking on the background does not alter the onclick function

Can anyone help me troubleshoot my code? My function load() isn't changing the background of .firstDiv for some reason. I've checked multiple times but I can't seem to spot any errors. function load() { document.getElementsBy ...

NextAuth encountered a CLIENT_FETCH_ERROR error while processing the session callback

Encountering issues while trying to set up nextauth v4. Keep getting this error: Client fetch error, Unexpected end of JSON input {error: {…}, path: 'session', message: 'JSON.parse: unexpected end of data at line 1 column 1 of the JSON d ...

Ways to ensure a form label is accessible via keyboard navigation

My HTML5 form collects user information and processes it upon submission. With the help of the jQuery Validation Plugin, input validation is performed. In case of errors, an error-message-container div is displayed at the top of the screen, listing all err ...

Are there any web browsers that automatically switch to a non-SSL connection if an attempt to connect with SSL

I regularly utilize jQuery along with jQuery.ajax to make connections between pages. I am interested in establishing a connection from a non-SSL page to an SSL page using ajax. Are there any web browsers that will attempt to connect via non-SSL if the con ...

Could use some help with configuring express routes with parameters

Greetings! I am new to working with Express and I am currently in the process of creating a portfolio website. In my project, I have a Pug file named project.pug which includes HTML content that should dynamically render based on the ID of each project sto ...

Navigate to the Bootstrap Panel body when the relevant link is clicked

I am facing an issue with a long list of panels that are tedious to scroll through. To make navigation easier, I am attempting to create a shortcut link at the top of the page. Below is the code for the shortcut: <a data-toggle="collapse" data-parent ...

Is `html.fa-events-icons-ready` causing clutter and disrupting the layout of the body?

I am seeking some clarification on my code and the resulting issues it is causing. Check out this image for reference: https://i.stack.imgur.com/jBQYd.png Why is the body height not taking up 100% of the space? The background image appears to be affect ...