简体   繁体   中英

Input control floating placeholder stays on validation

I am trying to achieve a floating placeholder functionality. Everything works fine with the code I have written except for the fact that when there is an email field and I try to add a wrong entry which is not of an email format and i shift focus from the input box, the floating text still appears. You can check the codepen link

Belew is my code:-

 .-input-container { max-width: 540px; padding-bottom: 30px; margin-top: 30px; } input.-textbox { width: 100%; border-radius: 40px; background-color: #f5f5f5; border: 1px solid #dddddd; padding: 16px 30px; font-size: 16px; color: #555555; font-family: OpenSans; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1.88; letter-spacing: normal; text-align: left; } input.-textbox:focus{ outline: none; box-shadow: none } input.-textbox:focus ~ .floating-label, input.-textbox:not(:focus):valid ~ .floating-label { top: 8px; bottom: 10px; left: 45px; font-size: 10px; opacity: 1; line-height: 2.1; font-family: OpenSans; } input.-textbox ~ .floating-label { position: absolute; pointer-events: none; left: 45px; top: 18px; transition: 0.2s ease all; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet"/> <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 "> <input type="email" class="-textbox form-control" required=""> <span class="floating-label">E-postadress</span> </div> 

Try this:

 .-input-container { max-width: 540px; padding-bottom: 30px; margin-top: 30px; } input.-textbox { width: 100%; border-radius: 40px; background-color: #f5f5f5; border: 1px solid #dddddd; padding: 16px 30px; font-size: 16px; color: #555555; font-family: OpenSans; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1.88; letter-spacing: normal; text-align: left; &:focus{ outline: none; box-shadow: none } } 
 <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 "> <input type="email" class="-textbox form-control" placeholder="E-postadress" required=""> </div> 

Use javascript in it. Try this

 $('input').on('focusin', function() { $(this).parent().find('span').addClass('test2'); }); $('input').on('focusout', function() { if (!this.value) { $(this).parent().find('span').removeClass('test2'); } }); 
 .-input-container { max-width: 540px; padding-bottom: 30px; margin-top: 30px; } input.-textbox { width: 100%; border-radius: 40px; background-color: #f5f5f5; border: 1px solid #dddddd; padding: 16px 30px; font-size: 16px; color: #555555; font-family: OpenSans; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1.88; letter-spacing: normal; text-align: left; } input.-textbox:focus { outline: none; box-shadow: none } input.-textbox:focus~.floating-label, input.-textbox:not(:focus):valid~.floating-label, { top: 18px; bottom: 10px; left: 45px; font-size: 10px; opacity: 1; line-height: 2.1; font-family: OpenSans; } input.-textbox~.floating-label { position: absolute; pointer-events: none; left: 45px; top: 18px; transition: 0.2s ease all; } .test2{ margin-top:-20px; line-height:2.1; font-size:10px; font-family: OpenSans; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.css" rel="stylesheet" /> <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 "> <input type="email" class="-textbox form-control" required="" id=spa> <span class="floating-label">E-postadress</span> </div> 

It is simple with some Javascript and minor changes in styles. Try this one.

 $(document).ready(function () { $('.-textbox').click(function (){ $(this).siblings('span').addClass('active'); }); $('.-textbox').blur(function(){ if($('.-textbox').val()=== ''){ $(this).siblings('span').removeClass('active'); } }); }); 
 .-input-container { max-width: 540px; padding-bottom: 30px; margin-top: 30px; position: relative; } input.-textbox { width: 100%; border-radius: 40px; background-color: #f5f5f5; border: 1px solid #dddddd; padding: 16px 30px; font-size: 16px; color: #555555; font-family: OpenSans; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1.88; letter-spacing: normal; text-align: left; &:focus{ outline: none; box-shadow: none } } input.-textbox ~ .floating-label { position: absolute; pointer-events: none; left: 45px; top: 18px; transition: 0.2s ease all; } input.-textbox ~ .floating-label.active{ top: 8px; bottom: 10px; left: 45px; font-size: 10px; opacity: 1; line-height: 2.1; font-family: OpenSans; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="-input-container col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 "> <input type="email" class="-textbox form-control" required=""> <span class="floating-label">E-postadress</span> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM