「前端詞典」這些功能其實(shí)不需要 JS,CSS 就能搞定

直接入題

1. 每個(gè)單詞的首字母大寫(xiě)

其實(shí)我第一次看到這個(gè)功能的時(shí)候就是使用 JS 去實(shí)現(xiàn)這個(gè)功能,想都沒(méi)想 CSS 可以完成這個(gè)功能。馬上就屁顛屁顛的寫(xiě)了一個(gè)方法:

<pre style="margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: inherit; vertical-align: baseline; word-break: break-word; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

function capitalizeFirst( str ) {
 let result = '';
 result = str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
 return result
} 

</pre>

寫(xiě)完這個(gè)方法后,還有點(diǎn)小得意,也就沒(méi)想其他方案。直到有一天看到 CSS 也能做這個(gè)功能的時(shí)候,我才反應(yīng)過(guò)來(lái)明明一句 CSS 就能解決的問(wèn)題,我卻使用了更復(fù)雜的方案。

CSS 方案如下:

<pre style="margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: inherit; vertical-align: baseline; word-break: break-word; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

.capitalizeFirst-css {
 text-transform: capitalize;
}

</pre>

是不是特別簡(jiǎn)單(代碼在上面的 blog 倉(cāng)庫(kù),訪問(wèn) cssDo 路由便可,下面的案例都是這個(gè)路由下):

image

text-transform 簡(jiǎn)單介紹

這是 CSS2 中的屬性,參數(shù)有 capitalize | uppercase | lowercase | none

參數(shù)介紹:

  1. none: 默認(rèn)。定義帶有小寫(xiě)字母和大寫(xiě)字母的標(biāo)準(zhǔn)的文本。
  2. capitalize: 文本中的每個(gè)單詞以大寫(xiě)字母開(kāi)頭。
  3. uppercase: 定義僅有大寫(xiě)字母。
  4. lowercase: 定義無(wú)大寫(xiě)字母,僅有小寫(xiě)字母。

從這個(gè)屬性我們可以知道全部大寫(xiě)(小寫(xiě))的需求這個(gè)屬性也能輕易實(shí)現(xiàn)。

2. 單選高亮

可能你看到“單選高亮”沒(méi)反應(yīng)過(guò)來(lái),直接來(lái)張圖片你就馬上清楚了:

image

不知道你是否第一次看到這種單選高亮的需求時(shí),是怎么處理的。我第一次直接是用 JS 控制的。后來(lái)我發(fā)現(xiàn)這個(gè)需求用 CSS 更方便處理。

主要代碼就是一段 CSS 代碼:

<pre style="margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: inherit; vertical-align: baseline; word-break: break-word; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

// 省略部分代碼,詳細(xì)代碼看倉(cāng)庫(kù)
.input:checked + .colors {
 border-color: #e63838;
 color: #e63838;
}
<div class="single-check">
 <input class="input" type="radio" name="colors" value="1">
 <div class="colors">天空之境</div> 
</div>

</pre>

看效果:

image

兩個(gè)選擇器的區(qū)別

~ 選擇器:查找某個(gè)元素后面的所有兄弟元素

  • 選擇器:查找某個(gè)元素后面緊鄰的兄弟元素

擴(kuò)展

其實(shí)這個(gè)技巧也完全可以使用在導(dǎo)航欄的交互效果,個(gè)人覺(jué)得可以簡(jiǎn)化一部分工作。

3、多列等高問(wèn)題

之前做 pc 端的客戶畫(huà)像需求時(shí),遇到需要左右兩邊等到的需求(左邊塊的高度會(huì)隨著內(nèi)容變化)。

最初我使用的 JS 計(jì)算高度再賦值,可是這樣會(huì)有頁(yè)面閃動(dòng)的效果。所以找到了兩種 CSS 的處理方案:

  1. 每列設(shè)置一個(gè)很大的 padding,再設(shè)置一個(gè)很大的負(fù)的 margin
  2. 使用 display: table;

第一種有明顯的缺陷:

  1. border-bottom 看不到了
  2. 設(shè)置的下方的兩個(gè)圓角也不見(jiàn)了

所以我使用了 display: table; 的方式來(lái)實(shí)現(xiàn)等高,可以說(shuō)非常的方便。

image

建議不要一味的抵觸 table,有的場(chǎng)景還是可以使用的。

4、表單驗(yàn)證

先聲明:這里沒(méi)有用到 JS,不過(guò)用到了 HTML5 關(guān)于 <input> 的新屬性 —— pattern( 檢查控件值的正則表達(dá)式 )。

還有一點(diǎn):其實(shí)我在實(shí)際項(xiàng)目中沒(méi)這么用過(guò)。

代碼如下:

<pre style="margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: inherit; vertical-align: baseline; word-break: break-word; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

input[type="text"]:invalid ~ input[type="submit"] {
 display: none
}
<div class="form-css">
 <input type="text" name="tel" placeholder="輸入手機(jī)號(hào)碼" pattern="^1[3456789]\d{9}$" required><br>
 <input type="text" name="smscode" placeholder="輸入驗(yàn)證碼" pattern="\d{4}" required><br>
 <input type="submit" ></input>
</div>

</pre>

效果如下(樣式不好看的問(wèn)題請(qǐng)忽略):

image

invalid 偽類和 vaild 偽類

  • valid 偽類,匹配通過(guò) pattern 驗(yàn)證的元素
  • invalid 偽類,匹配未通過(guò) pattern 驗(yàn)證的元素

后記

還有一些大家比較常用的這里就不介紹了,

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容