我正在研究我從 youtube 視頻制作的輸入設計,它作業得很好,但我想稍微修改一下,代碼:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
flex-direction: column;
gap: 30px;
background: #1d2b3a;
}
/******************** Input Box - Not Required ********************/
.inputBox{
position: relative;
width: 250px;
}
.inputBox input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox input:invalid ~ span,
.inputBox input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox input:invalid,
.inputBox input:focus{
border: 1px solid #00dfc4;
}
/******************** Input Box - Required ********************/
.inputBox_r{
position: relative;
width: 250px;
}
.inputBox_r input{
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox_r span{
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255,255,255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox_r input:valid,
.inputBox_r input:focus{
border: 1px solid #00dfc4;
}
.inputBox_r input:valid ~ span,
.inputBox_r input:focus ~ span{
background: #00dfc4;
color: #1d2b3a;
border-radius: 2px;
}
**HTML:**
<div class="inputBox">
<input type="text" name="">
<span>First Name</span>
</div>
<br>
<div class="inputBox_r">
<input type="text" name="" required>
<span>Last Name</span>
</div>
如您所見,有兩個輸入,一個不是強制性的,另一個是必需的屬性。當您在第二個輸入框中輸入文本時,由于required 屬性,它處于焦點狀態,這正是我想要的第一個輸入框。
第一個輸入框沒有必需的屬性,這就是為什么即使我在其中輸入文本并單擊其他地方它也會失去焦點并弄亂我的 css 樣式。
那么有沒有辦法在輸入文本但不傳遞所需屬性的情況下將注意力集中在第一個輸入上?
我到處搜索,但找不到任何東西。抱歉,如果我說錯了,我是 HTML 和 CSS 新手
uj5u.com熱心網友回復:
這是一個簡單的 JS 代碼,它將檢查欄位中是否有值并.filled
相應地添加/洗掉一個類。然后CSS也被調整
// Get all .inputBox
const inputBoxes = Array.from(document.getElementsByClassName('inputBox'));
// Get all .inputBox_r
const rInputBoxes = Array.from(document.getElementsByClassName('inputBox_r'));
// Merge both .inputBox and .inputBox_r into 1 array
const allInputBoxes = inputBoxes.concat(rInputBoxes);
// For each box
allInputBoxes.forEach( box => {
// Get input as first child element
const input = box.firstElementChild;
// add event listener for any change on the input
input.addEventListener('input', () => {
// if input is not empty add class filled
// else remove class filled if is empty
input.value ? input.classList.add('filled') : input.classList.remove('filled')
})
});
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
flex-direction: column;
gap: 30px;
background: #1d2b3a;
}
/******************** Input Box - Not Required ********************/
.inputBox {
position: relative;
width: 250px;
}
.inputBox input {
width: 100%;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox span {
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255, 255, 255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox input:invalid~span,
.inputBox input:focus~span,
.inputBox input.filled~span {
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox input:invalid,
.inputBox input:focus,
.inputBox input.filled {
border: 1px solid #00dfc4;
}
/******************** Input Box - Required ********************/
.inputBox_r {
position: relative;
width: 250px;
}
.inputBox_r input {
width: 100%;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.25);
background: #1d2b3a;
border-radius: 5px;
outline: none;
color: #fff;
font-size: 1em;
transition: 0.5s;
}
.inputBox_r span {
position: absolute;
left: 0;
padding: 10px;
pointer-events: none;
font-size: 1em;
color: rgba(255, 255, 255, 0.25);
text-transform: uppercase;
transition: 0.5s;
}
.inputBox_r input:valid~span,
.inputBox_r input:focus~span,
.inputBox_r input.filled~span{
color: #00dfc4;
transform: translateX(10px) translateY(-7px);
font-size: 0.65em;
padding: 0 10px;
background: #1d2b3a;
border-left: 1px solid #00dfc4;
border-right: 1px solid #00dfc4;
letter-spacing: 0.2em;
}
.inputBox_r input:valid,
.inputBox_r input:focus,
.inputBox_r input.filled {
border: 1px solid #00dfc4;
}
.inputBox_r input:valid~span,
.inputBox_r input:focus~span,
.inputBox_r input.filled~span {
background: #00dfc4;
color: #1d2b3a;
border-radius: 2px;
}
<div class="inputBox">
<input type="text" name="">
<span>First Name</span>
</div>
<div class="inputBox_r">
<input type="text" name="" required>
<span>Last Name</span>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495417.html
下一篇:運行硒問題