我有一些由CSS设置样式的输入,在一个输入上我需要删除所需的属性。出于某种原因,这会改变CSS动画的行为方式。将所需属性添加到输入时,它会更改动画的行为方式。
任何解决方案?
HTML
<input type="text" class="form-input" autofocus required>
<span class="highlight"></span>
<span class="bar"></span>
<label class="form-label">Name</label>
<input type="text" class="form-input" autofocus>
<span class="highlight"></span>
<span class="bar"></span>
<label class="form-label">Name</label>
CSS
body {
background-color:#fff;
padding:40px;
}
.group {
position:relative;
margin-bottom:45px;
}
.form-input {
font-size:18px;
padding:10px 10px 10px 5px;
display:block;
width:100%;
border:none;
border-bottom:1px solid #757575;
}
.form-input:focus { outline:none; }
/* LABEL ======================================= */
.form-label {
color:#999;
font-size:18px;
font-weight:normal;
position:absolute;
pointer-events:none;
left:5px;
top:10px;
transition:0.2s ease all;
-moz-transition:0.2s ease all;
-webkit-transition:0.2s ease all;
}
/* active state */
.form-input:focus ~ .form-label, .form-input:valid ~ .form-label {
top:-20px;
font-size:14px;
color:#d438c5;
}
/* BOTTOM BARS ================================= */
.bar { position:relative; display:block; width:100%; }
.bar:before, .bar:after {
content:'';
height:2px;
width:0;
bottom:1px;
position:absolute;
background:#d438c5;
transition:0.2s ease all;
-moz-transition:0.2s ease all;
-webkit-transition:0.2s ease all;
}
.bar:before {
left:50%;
}
.bar:after {
right:50%;
}
/* active state */
.form-input:focus ~ .bar:before, .form-input:focus ~ .bar:after {
width:50%;
}
/* HIGHLIGHTER ================================== */
.highlight {
position:absolute;
height:60%;
width:100px;
top:25%;
left:0;
pointer-events:none;
opacity:0.5;
}
/* active state */
.form-input:focus ~ .highlight {
-webkit-animation:inputHighlighter 0.3s ease;
-moz-animation:inputHighlighter 0.3s ease;
animation:inputHighlighter 0.3s ease;
}
.form-select {
height: 47px;
background: transparent;
}
/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
from { background:#d438c5; }
to { width:0; background:transparent; }
}
@-moz-keyframes inputHighlighter {
from { background:#d438c5; }
to { width:0; background:transparent; }
}
@keyframes inputHighlighter {
from { background:#d438c5; }
to { width:0; background:transparent; }
}
看看我的小提琴。 https://jsfiddle.net/97892awh/
答案 0 :(得分:2)
这是因为这个选择器:
.form-input:valid ~ .form-label
正如您所看到的那样,您有一个:valid
选择器,当输入有效时(显然)触发,在您的情况下,当输入为required
时。删除它,它将起作用:
.form-input:focus ~ .form-label{
top:-20px;
font-size:14px;
color:#d438c5;
}
jsFiddle上的示例。
答案 1 :(得分:1)
如果您不想要不同的CSS,只需删除css中的 valid
选择器。
/* active state */ .form-input:focus ~ .form-label,.form-input:valid ~ .form-label{ top:-20px; font-size:14px; color:#d438c5; }
答案 2 :(得分:0)
以下是我如何解决它。
添加了CSS类
.not-empty {
top:-20px;
font-size:14px;
color:#d438c5;
}
还有一些jquery
$('#name').focusout(function() {
var val = this.value;
if (val.length > 0) {
$('#name-label').addClass('not-empty');
} else {
$('#name-label').removeClass('not-empty');
}
});
还编辑了这样的HTML
<div class="group">
<input type="text" class="form-input" id="name" autofocus>
<span class="highlight"></span>
<span class="bar"></span>
<label class="form-label" id="name-label">Name</label>
</div>