输入控件浮动占位符在验证时保持不变



我正在尝试实现浮动占位符功能。我编写的代码一切正常,除了当有一个电子邮件字段并且我尝试添加一个不是电子邮件格式的错误条目并且我从输入框中转移焦点时,浮动文本仍然出现。您可以查看代码笔链接

Belew是我的代码:-

.-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>

试试这个:

.-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>

在其中使用 javascript。试试这个

$('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>

它很简单,有一些Javascript和样式上的微小变化。试试这个。

$(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>

最新更新