【发布时间】:2020-03-15 04:45:17
【问题描述】:
我希望标签中的跨度在我的输入处于焦点和有效模式时向上移动,在电子邮件类型输入上,当我写除电子邮件格式文本之外的任何内容,然后将焦点移出输入时,跨度又回来了下,我只想让它保持不变,即使我的文本格式不是“电子邮件”。
.form-group {
position: relative;
}
.form-group input {
width: 100%;
height: 100%;
color: #595f6e;
padding-top: 20px;
border: none;
outline: none;
}
.form-group label {
height: 100%;
position: absolute;
left: 0;
bottom: 0;
pointer-events: none;
width: 100%;
border-bottom: 1px solid black;
margin: 0 !important;
}
.form-group label::after {
content: "";
position: absolute;
left: 0px;
bottom: -1px;
height: 100%;
width: 100%;
border-bottom: 3px solid #5fa8d3;
transform: translateX(-100%);
}
.form-group label .content-name {
position: absolute;
bottom: 5px;
left: 0px;
transition: all 0.3s ease;
}
.form-group input:focus + .label-name .content-name,
.form-group input:valid + .label-name .content-name {
transform: translateY(-80%);
font-size: 14px;
color: #5fa8d3;
}
<form id="loginForm" action="{{ url_for('login.login') }}" method="POST">
<div class="form-group">
<input type="email" id="email" name="email"
pattern="[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}" required>
<label class="label-name">
<span class="content-name">
Name
</span>
</label>
</div>
</form>
【问题讨论】:
-
不完全确定您的要求。我认为如果输入不为空,您希望无论标签是否有效都被翻译?
-
是的,在焦点和有效性上都应该翻译!
-
"valid" 表示输入不为空且模式匹配。如果输入为 invalid,您的选择器将不会选择标签,该标签要么为空,要么与模式不匹配。
-
是的,我知道这一点,但是不管输入的有效性如何,有没有办法让它发生?
-
CSS 中没有“输入有值”的选择器。
input[value=""]仅在输入值设置为加载页面之前的值时才有效,并且它不响应输入值的更改。您必须使用某种 JavaScript 来设置类似“is-dirty”类的内容并在您的 CSS 中使用它。这几乎就是像 Knockout.js 这样的框架所做的(不知道 Angular、React 等)。