我無法弄清楚如何在使用焦點的輸入上獲得這兩種效果(請參閱下面的代碼片段)。我注意到焦點只適用於這個第一行的元素。有人能告訴我爲什麼嗎?是否可以在不使用JavaScript的情況下獲得兩種效果?焦點上的這兩件事情,是JS需要的?
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>
是啊,這樣的事情,但對於兩個輸入:)謝謝你啦。 – rndm