超碰人人人人人,亚洲AV午夜福利精品一区二区,亚洲欧美综合区丁香五月1区,日韩欧美亚洲系列

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

三種方式使用純 CSS 實現(xiàn)星級評分

freeflydom
2025年1月18日 10:13 本文熱度 476

本文介紹三種使用純 CSS 實現(xiàn)星級評分的方式。每種都值得細(xì)品一番~

五角星取自 Element Plus 的 svg 資源

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style="">
    <path
        fill="currentColor"
        d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z">
    </path>
</svg>

三種實現(xiàn)方式的 html 結(jié)構(gòu)是一樣的

<div>
  <input type="radio" name="radio" id="radio1">
  <label for="radio1">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio2">
  <label for="radio2">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio3">
  <label for="radio3">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio4">
  <label for="radio4">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
  <input type="radio" name="radio" id="radio5">
  <label for="radio5">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" style=""><path fill="currentColor" d="M283.84 867.84 512 747.776l228.16 119.936a6.4 6.4 0 0 0 9.28-6.72l-43.52-254.08 184.512-179.904a6.4 6.4 0 0 0-3.52-10.88l-255.104-37.12L517.76 147.904a6.4 6.4 0 0 0-11.52 0L392.192 379.072l-255.104 37.12a6.4 6.4 0 0 0-3.52 10.88L318.08 606.976l-43.584 254.08a6.4 6.4 0 0 0 9.28 6.72z"></path></svg>
  </label>
</div>

利用 radio + label 的方式實現(xiàn)點擊效果;將 label 的 for 屬性保持和 radio 的 id 一致,并將 radio 框隱藏,這樣點擊 label 就是點擊 radio 了;label 在這里就是每個星星;

html,body{
  width:100%;height:100%;
}
body{
  display:flex;
  justify-content:center;
  align-items:center;
}
div{
  display : flex;
  justify-content:center;
  align-items:center;
}
div input{
  display:none;
}
div label{
  width:50px;height:50px;
  padding:0 4px;
  color:#ccc;
  cursor:pointer;
}

html 布局效果如下:

通常星級評分效果包括鼠標(biāo)滑入和點擊,滑入或點擊到第幾顆星的位置,該位置之前的星高亮,之后的星不高亮或者有高亮的則取消高亮;

接下來分別闡述三種 CSS 實現(xiàn)方式;

1、:has()選擇器 + input:checked

當(dāng)點擊星星時,高亮當(dāng)前星星

input:checked + label{
    color:gold;
}

input:checked + label 表示 選擇緊挨著已選中 input 框的后面一個 label;

當(dāng)鼠標(biāo)移入星星時,高亮當(dāng)前星星,并且該位置之后的星星取消高亮;

label:hover{
  cursor:pointer;
  color:gold;
  & ~ label{
    color:#ccc!important;
  }
}

那么如何讓該位置之前的星星也高亮呢,目前的兄弟選擇器包括 + 和 ~ ,但都不能選擇之前的兄弟元素;此時 :has() 選擇器就登場了;

:has() 提供了一種針對引用元素選擇父元素或者先前的兄弟元素的方法。

比如:

a:has(p) 表示選擇包含子元素 p 的 a 元素;

a:has(> p) 表示選擇有直接后代 p 元素的 a 元素,也就是 p 只能是 a 的 "兒子" 元素;

a:has(+ p) 表示選擇后面緊跟著的兄弟元素是 p 的 a 元素;

所以回到上面問題,當(dāng)鼠標(biāo)移入星星時,讓該位置之前的所有星星也高亮,可以這么做

div:has(label:hover) label:not(:hover,:hover ~ *){
  color:gold;
}

label:not(:hover,:hover ~ *) 表示排除當(dāng)前 hover 的 label 和之后的所有元素;也就自然選擇了前面所有星星;

同樣,當(dāng)點擊星星時,點亮當(dāng)前選擇的之前所有的星星也如此

div:has(input:checked) label:not(input:checked ~ label){
  color:gold;
}

div:has(input:checked) 表示選擇包含被選中的 input 的 div;

label:not(input:checked ~ label) 表示排除當(dāng)前選中的 input 后面的所有 label,也就選擇到前面所有的 label 了;

完整示例

2、:indeterminate + input:checked 巧妙實現(xiàn)

這種實現(xiàn)的思路是,假設(shè)初始所有的星星都是高亮的,鼠標(biāo)移入或點擊時保持前面星星的高亮,取消后面星星的高亮;

div label{
  width:50px;height:50px;
  padding:0 4px;
  color:gold;  => 默認(rèn)星星高亮
  cursor:pointer;
}

然后當(dāng)鼠標(biāo)移入或點擊時,取消該位置后面的星星的高亮

div input:checked ~ input + label,
div label:hover ~ label{
  color:#ccc;
}

但一開始默認(rèn)設(shè)置的星星是高亮的,但頁面上并不想在 radio 未被選中時高亮,這時 :indeterminate 就登場了;

:indeterminate表示任意的狀態(tài)不確定的表單元素。對于 radio 元素,:indeterminate 表示當(dāng)表單中具有相同名稱值的所有單選按鈕均未被選中時。

所以這里設(shè)置每個星星在對應(yīng)的 radio 的未被選中時非高亮;并且只是在初始狀態(tài),鼠標(biāo)移入時這種初始狀態(tài)就應(yīng)該被改變

div:not(:hover) input:indeterminate + label,
div:not(:hover) input:checked ~ input + label,
div input:hover ~ input + label{
  color:#ccc;
}

:not() 表示用來匹配不符合一組選擇器的元素;div:not(:hover) 表示鼠標(biāo)移入時,不匹配這行規(guī)則,這樣在初始狀態(tài)下或者在鼠標(biāo)點擊星星后,鼠標(biāo)移入仍然會高亮當(dāng)前點擊位置之前的星星;

這樣效果就達(dá)到了;

完整示例

3、flex-direction:row-reverse; + input:checked 巧妙實現(xiàn)

目前 html 布局是從左到右布局,但如果我們倒過來呢,從右到左布局;

div{
  width:300px;
  display:flex;
  /* 從右往左排列 */
  flex-direction:row-reverse;
  justify-content:space-around;
}

那么之前利用 :has() 選擇之前的兄弟元素現(xiàn)在就可以直接用 ~ 來選擇了;

// 之前
label:not(input:checked ~ label){
  color:gold;
}
// 現(xiàn)在
label:hover ~ label{
  color:gold;
}

點擊星星也是

input:checked ~ label{
  color:gold;
}

但是這樣還不夠完善

當(dāng)我們點擊第二顆星星時,鼠標(biāo)滑入到第三個星星,第二顆星星并沒有取消高亮,所以這里還是得借助下 :has()

label:has(~ label:hover){
  color:#ccc;
}

上面表示選擇后面被 hover 的兄弟元素的元素,也就是 hover 元素的前面的所有元素;這樣就沒問題了;

完整示例

總結(jié)

以上使用了三種純 css 實現(xiàn)星級評分的方式;

  • :has()選擇器 + input:checked
  • :not()選擇器 + input:checked
  • flex-direction:row-reverse; + input:checked 巧妙實現(xiàn)

特別是 :has() 選擇器可以選擇之前的兄弟元素,搭配 :not() 能發(fā)揮很多作用,以前很多需要用 js 實現(xiàn)的效果或許現(xiàn)在可以用 :has() 來試試了;

附上 :has() 和 :not() 的兼容性截圖

:has()

:not()

轉(zhuǎn)自https://www.cnblogs.com/zsxblog/p/18072530


該文章在 2025/1/18 10:13:23 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved