我不知道如何在使用focus的输入上同时获得这两种效果(参见下面的代码片段)。我注意到焦点只适用于这个元素,它是第一行的。谁能告诉我原因吗?在不使用JavaScript的情况下,是否有可能在焦点上同时获得这两种效果?
form {
margin: 30px;
font-family: Arial;
}
.expand {
display: block;
position: relative;
margin-top: 20px;
width: 300px;
}
input[type="text"],
input[type="email"] {
border: none;
background-color: #eee;
padding: 10px;
width: 280px;
}
.border {
position: absolute;
display: block;
height: 3px;
width: 100%;
top: 100%;
background: red;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: 0 50%;
}
label {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
color: #999;
pointer-events: none;
transition: 0.2s;
margin-left: 10px;
}
input[type="text"]:focus + .border,
input[type="email"]:focus + .border {
transform: scaleX(1);
}
input[type="text"]:focus + label,
input[type="email"]:focus + label {
top: -10px;
font-size: 12px;
color: red;
font-weight: bold;
margin: 0;
}<form action="">
<div class="expand">
<input type="text" name="" id="">
<!-- border before label -->
<div class="border"></div>
<label for="">Here works border effect</label>
</div>
<div class="expand">
<input type="email" name="" id="">
<!-- border after label -->
<label for="">Here works label effect</label>
<div class="border"></div>
</div>
</form>
发布于 2016-09-14 12:49:06
如果只使用+选择器将影响直接兄弟姐妹,则使用~
form {
margin: 30px;
font-family: Arial;
}
.expand {
display: block;
position: relative;
margin-top: 20px;
width: 300px;
}
input[type="text"],
input[type="email"],
textarea {
border: none;
background-color: #eee;
padding: 10px;
width: 280px;
}
textarea {
resize: none;
margin-bottom: -1px;
}
.border {
position: absolute;
display: block;
height: 3px;
width: 100%;
top: 100%;
background: red;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: 0 50%;
}
label {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
color: #999;
pointer-events: none;
transition: 0.2s;
margin-left: 10px;
}
input[type="text"]:focus ~ .border,
input[type="email"]:focus ~ .border,
textarea:focus ~ .border {
transform: scaleX(1);
}
input[type="text"]:focus ~ label,
input[type="email"]:focus ~ label,
textarea:focus ~ label {
top: -10px;
font-size: 12px;
color: red;
font-weight: bold;
margin: 0;
}<form action="">
<div class="expand">
<input type="text" name="" id="">
<!-- border before label -->
<div class="border"></div>
<label for="">Here works border effect</label>
</div>
<div class="expand">
<input type="email" name="" id="">
<!-- border after label -->
<label for="">Here works label effect</label>
<div class="border"></div>
</div>
</form>
发布于 2016-09-14 12:52:07
这里小提琴:https://jsfiddle.net/3nw7mkfe/1/
您可以连接选择器+ like或~或其他。
<form action="">
<div class="expand">
<input type="email" name="" id="">
<!-- border after label -->
<label for="">Here works label effect</label>
<div class="border"></div>
</div>
</form>
form {
margin: 30px;
font-family: Arial;
}
.expand {
display: block;
position: relative;
margin-top: 20px;
width: 300px;
}
input[type="text"],
input[type="email"],
textarea {
border: none;
background-color: #eee;
padding: 10px;
width: 280px;
}
textarea {
resize: none;
margin-bottom: -1px;
}
.border {
position: absolute;
display: block;
height: 3px;
width: 100%;
top: 100%;
background: red;
transform: scaleX(0);
transition: transform 0.5s;
transform-origin: 0 50%;
}
label {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
color: #999;
pointer-events: none;
transition: 0.2s;
margin-left: 10px;
}
input[type="text"]:focus + label + .border,
input[type="email"]:focus + label + .border,
textarea:focus + .border {
transform: scaleX(1);
}
input[type="text"]:focus + label,
input[type="email"]:focus + label,
textarea:focus + label {
top: -10px;
font-size: 12px;
color: red;
font-weight: bold;
margin: 0;
}https://stackoverflow.com/questions/39490796
复制相似问题