Home > OS >  Contact Form Input Animation Bug
Contact Form Input Animation Bug

Time:11-10

I recently asked a question about a contact form animation (the label text moves up when the box is clicked) and while I got the animation to work properly, I've run into a bug:

When I click on either the first and second boxes (not the third for some reason), it activates the animation for the rest of the boxes which is very annoying.

How can I get it so that when clicking on one box, it ONLY animates the box I clicked on and not the rest?

<div   style="position:relative display:inline;" >

        <input type="text" name="name" required>
         <span id="floating-label">Yourname</span>

        <input type="email" name="email" required>
        <span id="floating-label-1">Your email</span>
                    
        <input type="text"  name="subject"  required>
        <span id="floating-label-2">Subject</span>
</div>
.contact-col input, .contact-col textarea{
    width: 100%;
    padding: 15px;
    margin-bottom: 25px;
    outline: none;
    font-family: 'Merriweather Sans', sans-serif;
    font-weight: 300;
    font-size: 16px;
    border: 1px solid #ccc;
    box-sizing: border-box;
}



.contact-col input:focus, .contact-col textarea:focus {
    border: 1px solid yellowgreen;
    box-shadow: 0 0 0 1px #8C7D50;
    transition: 25ms box-shadow ease-in-out;
}




 input:focus ~ #floating-label,
    input:not(:focus):valid ~ #floating-label{
        top: -0.5%;
        left: 0.5rem;
        padding: 0.5rem;
        font-size: 14px;
        color: #8C7D50 ;
        font-weight: 400;
        transition: 0.2s ease-in-out;
     
    }

    #floating-label {
        font-family: 'Merriweather Sans', sans-serif;
        font-weight: 300;
        font-size: 16px;
        position: absolute;
        pointer-events: none;
        left: 1rem;
        transform: translateY(-50%);
        top: 4.5%;
        color: #777;
        background: white;
        transition: 0.2s ease all;
    }



    #floating-label-1{
        font-family: 'Merriweather Sans', sans-serif;
        font-weight: 300;
        font-size: 16px;
        position: absolute;
        pointer-events: none;
        left: 1rem;
        transform: translateY(-50%);
        top: 19%;
        color: #777;
        background: white;
        transition: 0.2s ease all;
    }



input:focus ~ #floating-label-1,
    input:not(:focus):valid ~ #floating-label-1{
        top: 14.25%;
        left: 0.5rem;
        padding: 0.5rem;
        font-size: 14px;
        color: #8C7D50 ;
        font-weight: 400;
        transition: 0.2s ease-in-out;
    }




 #floating-label-2{
        font-family: 'Merriweather Sans', sans-serif;
        font-weight: 300;
        font-size: 16px;
        position: absolute;
        pointer-events: none;
        left: 1rem;
        transform: translateY(-50%);
        top: 33.5%;
        color: #777;
        background: white;
        transition: 0.2s ease all;
    }



input:focus ~ #floating-label-2,
    input:not(:focus):valid ~ #floating-label-2{
        top: 28.5%;
        left: 0.5rem;
        padding: 0.5rem;
        font-size: 14px;
        color: #8C7D50 ;
        font-weight: 400;
        transition: 0.2s ease-in-out;
    }



CodePudding user response:

This is an issue since you do not explicitly pick the inputs. As a result, if you selects your inputs by name, the problem will be resolved.


.contact-col input,
.contact-col textarea {
  width: 100%;
  padding: 15px;
  margin-bottom: 25px;
  outline: none;
  font-family: 'Merriweather Sans', sans-serif;
  font-weight: 300;
  font-size: 16px;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

.contact-col input:focus,
.contact-col textarea:focus {
  border: 1px solid yellowgreen;
  box-shadow: 0 0 0 1px #8c7d50;
  transition: 25ms box-shadow ease-in-out;
}

input[name='name']:focus ~ #floating-label,
input[name='name']:not(:focus):valid ~ #floating-label {
  top: -0.5%;
  left: 0.5rem;
  padding: 0.5rem;
  font-size: 14px;
  color: #8c7d50;
  font-weight: 400;
  transition: 0.2s ease-in-out;
}

#floating-label {
  font-family: 'Merriweather Sans', sans-serif;
  font-weight: 300;
  font-size: 16px;
  position: absolute;
  pointer-events: none;
  left: 1rem;
  transform: translateY(-50%);
  top: 4.5%;
  color: #777;
  background: white;
  transition: 0.2s ease all;
}

#floating-label-1 {
  font-family: 'Merriweather Sans', sans-serif;
  font-weight: 300;
  font-size: 16px;
  position: absolute;
  pointer-events: none;
  left: 1rem;
  transform: translateY(-50%);
  top: 19%;
  color: #777;
  background: white;
  transition: 0.2s ease all;
}

input[name='email']:focus ~ #floating-label-1,
input[name='email']:not(:focus):valid ~ #floating-label-1 {
  top: 14.25%;
  left: 0.5rem;
  padding: 0.5rem;
  font-size: 14px;
  color: #8c7d50;
  font-weight: 400;
  transition: 0.2s ease-in-out;
}

#floating-label-2 {
  font-family: 'Merriweather Sans', sans-serif;
  font-weight: 300;
  font-size: 16px;
  position: absolute;
  pointer-events: none;
  left: 1rem;
  transform: translateY(-50%);
  top: 33.5%;
  color: #777;
  background: white;
  transition: 0.2s ease all;
}

input[name='subject']:focus ~ #floating-label-2,
input[name='subject']:not(:focus):valid ~ #floating-label-2 {
  top: 28.5%;
  left: 0.5rem;
  padding: 0.5rem;
  font-size: 14px;
  color: #8c7d50;
  font-weight: 400;
  transition: 0.2s ease-in-out;
}


CodePudding user response:

Issue

Currently you are moving all the floating labels up when any of the input values are either focused or valid.

input:focus ~ #floating-label,
input:not(:focus):valid ~ #floating-label{
  //...
}

Solution

To fix that, you couldadd the same id or something similar like input-field-X to the input tags. This isn't great for your css though, as it has to be repeatable and it grows quickly. So I would just create a single <div lang-html prettyprint-override"> <div > <div > <input type="text" name="name" required> <label for="name">Your name</label> </div> <div > <input type="text" name="email" required> <label for="name">Your email</label> </div> <div > <input type="text" name="subject" required> <label for="name">Subject</label> </div> </div>

I also fixed your CSS a bit.

html,
body {
  margin: 0;
}
body * {
  box-sizing: border-box;
}

.input-field {
  --input-field-height: 50px;
  --input-field-padding-left: 10px;
  position: relative; /* this is important for the positioning of the label*/
  width: 600px;
  height: var(--input-field-height);
  margin: 15px;
}

.input-field input {
  transition: 25ms box-shadow ease-in-out;
  width: 100%;
  height: 100%;
  padding-left: var(--input-field-padding-left);
}

.input-field label {
  position: absolute;
  top: calc(var(--input-field-height) / 2);
  transform: translateY(-50%);
  left: 20px;
  font-family: "Merriweather Sans", sans-serif;
  font-weight: 300;
  font-size: 16px;
  pointer-events: none;
  color: #777;
  background: white;
  transition: 0.2s ease all;
}
.input-field input:focus ~ label,
.input-field input:not(:focus):valid ~ label {
  top: 0;
  left: var(--input-field-padding-left);
  color: #8c7d50;
}

.input-field input:focus {
  outline: 1px solid yellowgreen; /* using outline istead of the border doesn't change the size of the input field */
  box-shadow: 0 0 0 1px #8c7d50;
}

Here is the jsfiddle.

If you have any questions, don't be afraid to ask.

  • Related