我最近問了一個關于聯系表單影片的問題(單擊框時標簽文本向上移動),雖然我讓影片正常作業,但我遇到了一個錯誤:
當我單擊第一個和第二個框(由于某種原因不是第三個框)時,它會激活其余框的影片,這很煩人。
我怎樣才能得到它,以便在單擊一個框時,它只影片我單擊的框而不是其余的?
<div class="contact-col" style="position:relative display:inline;" >
<input type="text" name="name" required>
<span id="floating-label">Yourname</span>
<input type="email" name="email" required>
<span id="floating-label-1">Your email</span>
<input type="text" name="subject" required>
<span id="floating-label-2">Subject</span>
</div>
.contact-col input, .contact-col textarea{
width: 100%;
padding: 15px;
margin-bottom: 25px;
outline: none;
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.contact-col input:focus, .contact-col textarea:focus {
border: 1px solid yellowgreen;
box-shadow: 0 0 0 1px #8C7D50;
transition: 25ms box-shadow ease-in-out;
}
input:focus ~ #floating-label,
input:not(:focus):valid ~ #floating-label{
top: -0.5%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8C7D50 ;
font-weight: 400;
transition: 0.2s ease-in-out;
}
#floating-label {
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 4.5%;
color: #777;
background: white;
transition: 0.2s ease all;
}
#floating-label-1{
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 19%;
color: #777;
background: white;
transition: 0.2s ease all;
}
input:focus ~ #floating-label-1,
input:not(:focus):valid ~ #floating-label-1{
top: 14.25%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8C7D50 ;
font-weight: 400;
transition: 0.2s ease-in-out;
}
#floating-label-2{
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 33.5%;
color: #777;
background: white;
transition: 0.2s ease all;
}
input:focus ~ #floating-label-2,
input:not(:focus):valid ~ #floating-label-2{
top: 28.5%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8C7D50 ;
font-weight: 400;
transition: 0.2s ease-in-out;
}
uj5u.com熱心網友回復:
這是一個問題,因為您沒有明確選擇輸入。因此,如果您按名稱選擇輸入,問題將得到解決。
.contact-col input,
.contact-col textarea {
width: 100%;
padding: 15px;
margin-bottom: 25px;
outline: none;
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.contact-col input:focus,
.contact-col textarea:focus {
border: 1px solid yellowgreen;
box-shadow: 0 0 0 1px #8c7d50;
transition: 25ms box-shadow ease-in-out;
}
input[name='name']:focus ~ #floating-label,
input[name='name']:not(:focus):valid ~ #floating-label {
top: -0.5%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8c7d50;
font-weight: 400;
transition: 0.2s ease-in-out;
}
#floating-label {
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 4.5%;
color: #777;
background: white;
transition: 0.2s ease all;
}
#floating-label-1 {
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 19%;
color: #777;
background: white;
transition: 0.2s ease all;
}
input[name='email']:focus ~ #floating-label-1,
input[name='email']:not(:focus):valid ~ #floating-label-1 {
top: 14.25%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8c7d50;
font-weight: 400;
transition: 0.2s ease-in-out;
}
#floating-label-2 {
font-family: 'Merriweather Sans', sans-serif;
font-weight: 300;
font-size: 16px;
position: absolute;
pointer-events: none;
left: 1rem;
transform: translateY(-50%);
top: 33.5%;
color: #777;
background: white;
transition: 0.2s ease all;
}
input[name='subject']:focus ~ #floating-label-2,
input[name='subject']:not(:focus):valid ~ #floating-label-2 {
top: 28.5%;
left: 0.5rem;
padding: 0.5rem;
font-size: 14px;
color: #8c7d50;
font-weight: 400;
transition: 0.2s ease-in-out;
}
uj5u.com熱心網友回復:
問題
當前,當任何輸入值集中或有效時,您正在向上移動所有浮動標簽。
input:focus ~ #floating-label,
input:not(:focus):valid ~ #floating-label{
//...
}
解決方案
input-field-X要解決這個問題,您可以在輸入標簽中添加相同的 id 或類似的東西。不過,這對您的 css 來說并不是很好,因為它必須是可重復的并且它會快速增長。所以我只會創建一個<div vertical-align: inherit;">里面有標簽和輸入欄位的地方。這使得標簽的定位和影片方式更容易,因為您可以從 div 絕對定位它。
<div class="contact-col">
<div class="input-field">
<input type="text" name="name" required>
<label for="name">Your name</label>
</div>
<div class="input-field">
<input type="text" name="email" required>
<label for="name">Your email</label>
</div>
<div class="input-field">
<input type="text" name="subject" required>
<label for="name">Subject</label>
</div>
</div>
我還稍微修復了你的 CSS。
html,
body {
margin: 0;
}
body * {
box-sizing: border-box;
}
.input-field {
--input-field-height: 50px;
--input-field-padding-left: 10px;
position: relative; /* this is important for the positioning of the label*/
width: 600px;
height: var(--input-field-height);
margin: 15px;
}
.input-field input {
transition: 25ms box-shadow ease-in-out;
width: 100%;
height: 100%;
padding-left: var(--input-field-padding-left);
}
.input-field label {
position: absolute;
top: calc(var(--input-field-height) / 2);
transform: translateY(-50%);
left: 20px;
font-family: "Merriweather Sans", sans-serif;
font-weight: 300;
font-size: 16px;
pointer-events: none;
color: #777;
background: white;
transition: 0.2s ease all;
}
.input-field input:focus ~ label,
.input-field input:not(:focus):valid ~ label {
top: 0;
left: var(--input-field-padding-left);
color: #8c7d50;
}
.input-field input:focus {
outline: 1px solid yellowgreen; /* using outline istead of the border doesn't change the size of the input field */
box-shadow: 0 0 0 1px #8c7d50;
}
這是jsfiddle。
如果您有任何問題,請不要害怕提問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530744.html
上一篇:如何避免溢位:只有一個元素隱藏?
下一篇:如何-將資料屬性添加到當前元素
