Fiddle:这是我的代码:
.accountbox {
background-color:#ffffff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform:translate(-50%,-50%);
padding: 15px 120px 50px 50px;
}
:focus{outline: none;}
.input1div {
display:inline-block; position: relative;
}
.input1div input {
font: 15px/24px "Lato", Arial, sans-serif; color: #333; width: 100%; box-sizing: border-box; letter-spacing: 1px;
}
.input1div input {border: 0; padding: 7px 0; border-bottom: 1px solid #ccc;}
.input1div input ~ .focus-border{position: absolute; bottom: 0; left: 0; width: 0; height: 2px; background-color: #3399FF; transition: 0.4s;}
.input1div input:focus ~ .focus-border{width: 100%; transition: 0.4s;}
.fakeplaceholder {
position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.28s ease all;
}
.firstname:focus ~ .fakeplaceholder{
top: -10px;
bottom: 10px;
left: 0px;
font-size: 11px;
opacity: 1;
color:red;
}

<div class="accountbox">
<form class="accountform">
<div class="input1div">
<input class="firstname" type="text" name="firstname" placeholder="" />
<span class="fakeplaceholder">First Name</span>
<span class="focus-border"></span>
</div>
</form>
</div>
&#13;
如果您点击input
框,则.fakeplaceholder
会上升。
但我想要更多的东西:
预期结果:
我尝试使用此代码,以实现我想要的,但它不起作用(测试出来)
.firstname:not(:focus):valid ~ .fakeplaceholder{
top: -10px;
bottom: 10px;
left: 0px;
font-size: 11px;
opacity: 1;
color:red;
}
答案 0 :(得分:1)
var input = document.querySelector('.firstname');
var placeholder = document.querySelector('.fakeplaceholder');
input.addEventListener('change',function(){
if(this.value == ''){
placeholder.classList.remove('isValid');
}else{
placeholder.classList.add('isValid');
}
})
.accountbox {
background-color:#ffffff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform:translate(-50%,-50%);
padding: 15px 120px 50px 50px;
}
:focus{outline: none;}
.input1div {
display:inline-block; position: relative;
}
.input1div input {
font: 15px/24px "Lato", Arial, sans-serif; color: #333; width: 100%; box-sizing: border-box; letter-spacing: 1px;
}
.input1div input {border: 0; padding: 7px 0; border-bottom: 1px solid #ccc;}
.input1div input ~ .focus-border{position: absolute; bottom: 0; left: 0; width: 0; height: 2px; background-color: #3399FF; transition: 0.4s;}
.input1div input:focus ~ .focus-border{width: 100%; transition: 0.4s;}
.fakeplaceholder {
position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.28s ease all;
}
.firstname:focus ~ .fakeplaceholder{
top: -10px;
bottom: 10px;
left: 0px;
font-size: 11px;
opacity: 1;
color:red;
}
.isValid{
top: -10px;
bottom: 10px;
left: 0px;
font-size: 11px;
opacity: 1;
color:red;
}
<div class="accountbox">
<form class="accountform">
<div class="input1div">
<input class="firstname" type="text" name="firstname" placeholder="" />
<span class="fakeplaceholder">First Name</span>
<span class="focus-border"></span>
</div>
</form>
</div>