代碼筆:鏈接
.body {
position: relative;
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0, 0, 0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance: none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
.select-label {
color: rgba(0, 0, 0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* active state */
.select-text:focus~.select-label,
.select-text:valid~.select-label {
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before,
.select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus~.select-bar:before,
.select-text:focus~.select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<html>
<head>
</head>
<body>
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text" required>
<option value="" disabled selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Select</label>
</div>
<!--Select with pure css-->
</div>
</body>
</html>
當<select>is 時required,它作業正常。
但問題是,當我洗掉時"required",它是 Label Floating 沒有選擇的選項。
在這種情況下如何維護浮動標簽?
我錯過了什么?還是我們需要 JavaScript?
所以這個想法不是在標簽為空時浮動標簽。
我input使用鏈接解決了這個問題
uj5u.com熱心網友回復:
只需洗掉:valid選擇器,如果不需要輸入,則有效始終為真。
所以在洗掉這個之后:
.select-text:valid ~ .select-label
:focus如果用戶選擇一個選項,您將面臨另一個問題,即在洗掉后保持標簽不變。
所以你需要添加 onchange 事件
onchange="this.dataset.chosen=this.value;"
如果用戶選擇了一個選項,那么您可以輕松地保留標簽。
.select-text[data-chosen] ~.select-label
這樣,如果選擇具有值,則標簽保持不變。
它變成如下片段:
.body {
position: relative;
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0, 0, 0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance: none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
.select-label {
color: rgba(0, 0, 0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* active state */
.select-text:focus~.select-label,
.select-text[data-chosen] ~.select-label{
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before,
.select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus~.select-bar:before,
.select-text:focus~.select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<html>
<head>
</head>
<body>
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text" onchange="this.dataset.chosen=this.value;">
<option value="" disabled selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Select</label>
</div>
<!--Select with pure css-->
</div>
</body>
</html>
編輯:
如果用戶選擇空選項,則此新解決方案有效,標簽下降,
我只是添加了空值選擇器以保持標簽不變,并blur()在選擇更改時添加了一個事件以平滑所有內容。
查看片段:
.body {
position: relative;
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0, 0, 0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance: none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
.select-label,select.select-text[data-chosen=""] ~.select-label {
color: rgba(0, 0, 0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* active state */
select.select-text:focus~.select-label,
.select-text[data-chosen] ~.select-label{
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before,
.select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus~.select-bar:before,
.select-text:focus~.select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<html>
<head>
</head>
<body>
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text" onchange="this.dataset.chosen=this.value;this.blur();">
<option value="" selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Select</label>
</div>
<!--Select with pure css-->
</div>
</body>
</html>
uj5u.com熱心網友回復:
我從這個改變
.select-text:focus~.select-label,
.select-text:valid~.select-label {
/* your code */
}
對此
.select-text:focus~.select-label {
/* your code */
}
現在正在作業,這里是固定代碼:
.body {
position: relative;
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0, 0, 0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance: none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
.select-label {
color: rgba(0, 0, 0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* active state, where I changed */
.select-text:focus~.select-label {
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before,
.select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus~.select-bar:before,
.select-text:focus~.select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text">
<option value="" disabled selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Select</label>
</div>
<!--Select with pure css-->
</div>
</body>
</html>
uj5u.com熱心網友回復:
它按預期作業。
當<select>is 時required,它作業正常。
這是因為這種風格 .select-text:valid ~ .select-label
當 select 為 時required,如果您選擇值為 的第一個選項null,則您的選擇無效并且 css 樣式將不起作用,這.select-text:valid ~ .select-label意味著您的標簽將為灰色。如果select不是required,那么如果您選擇第一個選項,那么.select-text將處于有效狀態,并且.select-text:valid ~ .select-label將應用樣式,這使得標簽變小且變藍。
為了解決這個問題,我們必須使用 css javascript 解決方案。
我已經通過參考這個答案實作了解決方案。
作業解決方案
.body {
position: relative;
font-family:
'Roboto','Helvetica','Arial',sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family:
'Roboto','Helvetica','Arial',sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0,0,0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0,0,0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance:none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
/* active state */
.select-text:focus ~ .select-label,
.select-text:valid ~ .select-label {
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
.select-label,
.select-text[data-chosen=''] ~ .select-label {
color: rgba(0,0,0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before, .select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus ~ .select-bar:before, .select-text:focus ~ .select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text" onchange="this.dataset.chosen = this.value; ">
<option value="" selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Please Select</label>
</div>
<!--Select with pure css-->
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/403297.html
標籤:
