果圖:
移入:
在之前的一篇文章《默認(rèn)的input標(biāo)簽太丑?教你如何使用CSS寫(xiě)出帶動(dòng)畫(huà)的樣式》中有講過(guò)如何實(shí)現(xiàn)一個(gè)漂亮的input標(biāo)簽樣式。
既然已經(jīng)折騰了,今天這篇文章我們就繼續(xù)折騰,看看如何通過(guò)CSS3實(shí)現(xiàn)一個(gè)帶動(dòng)畫(huà)的checkbox樣式?
CSS3
感興趣的可以自行去github上看源文件,地址如下:
https://github.com/zhouxiongking/article-pages/blob/master/articles/beautifulCheckbox/beautifulCheckbox.html
checkbox作為多選框,在form表單中的使用頻率是非常高的,例如問(wèn)卷調(diào)查的多選題,個(gè)人興趣愛(ài)好的選擇時(shí)都會(huì)用到多選框。
而瀏覽器默認(rèn)的checkbox樣式都很丑,看起來(lái)就是一個(gè)小小的方框,雖然Bootstrap已經(jīng)有好看的checkbox樣式,但是它不支持動(dòng)畫(huà),在交互上仍然存在缺陷,所以我們就試圖自己寫(xiě)出一個(gè)帶動(dòng)畫(huà)的checkbox效果。
首先我們來(lái)看看checkbox的動(dòng)態(tài)效果圖(中間的黑色圓圈實(shí)際為錄制GIF圖時(shí)產(chǎn)生的,請(qǐng)忽略)
checkbox效果圖
接下來(lái)我們具體分析下頁(yè)面的元素組成,主要包括以下部分。
默認(rèn)的checkbox標(biāo)簽,將其display設(shè)置為none,隱藏起來(lái)。
label標(biāo)簽,表現(xiàn)為實(shí)際顯示的動(dòng)態(tài)選中和未選中效果。
我們需要達(dá)到的效果是,在未選中checkbox時(shí),默認(rèn)為灰色的邊框,白色的背景;在選中checkbox時(shí),改變背景色,出現(xiàn)白色的打勾動(dòng)畫(huà)效果。
在這部分,我們來(lái)具體分析下頁(yè)面的代碼組成。
HTML
首先是頁(yè)面的HTML部分代碼,如上一部分所示,頁(yè)面其實(shí)只有兩個(gè)元素組成,這兩個(gè)元素放在一個(gè)容器下,故HTML代碼如下所示。
HTML部分代碼
CSS
我們來(lái)看看CSS部分的代碼,因?yàn)镃SS部分代碼比較多,我們一一來(lái)分析。
首先是外層container的樣式,同時(shí)對(duì)頁(yè)面的html和body也做一些簡(jiǎn)單處理,代碼如下所示。
container樣式
其次是checkbox標(biāo)簽的樣式,因?yàn)樵腸heckbox除了在傳輸數(shù)據(jù)時(shí)有用處,在頁(yè)面呈現(xiàn)上并沒(méi)有作用,故設(shè)置display:none;將其隱藏。
checkbox樣式
然后是最重要的label部分,在未點(diǎn)擊之前l(fā)abel呈現(xiàn)一個(gè)正方形,有基本的樣式。
基本樣式
在點(diǎn)擊方形框后,出現(xiàn)的對(duì)勾是通過(guò)偽元素::before和::after實(shí)現(xiàn)的,對(duì)勾實(shí)際為兩個(gè)矩形拼接而成,左側(cè)的矩形用::after元素表示,右側(cè)的矩形用::before元素表示。兩者的通用樣式通過(guò)如下代碼實(shí)現(xiàn)。
偽元素實(shí)現(xiàn)
然后是針對(duì)::before和::after所特有的樣式,因?yàn)閮烧叩奈恢煤托D(zhuǎn)的角度不同,代碼也會(huì)出現(xiàn)差異。
特有樣式
然后是定義的打鉤的動(dòng)畫(huà)效果,這里使用keyframes關(guān)鍵字定義,考慮到瀏覽器兼容性,定義的動(dòng)畫(huà)具有不同的前綴。定義的動(dòng)畫(huà)也包括兩部分,首先是右側(cè)的矩形動(dòng)畫(huà)效果。
右側(cè)矩形動(dòng)畫(huà)效果
然后是左側(cè)矩形的動(dòng)畫(huà)效果,同樣考慮到瀏覽器的兼容性,樣式代碼如下。
左側(cè)矩形動(dòng)畫(huà)
至此,所有代碼都已講解完畢,如果運(yùn)行之后就會(huì)看到文章開(kāi)始的動(dòng)畫(huà)效果。
本篇文章主要講的是如何使用自定義的動(dòng)畫(huà)完成checkbox效果,感興趣的可以直接去github上看源文件代碼,后續(xù)會(huì)寫(xiě)一個(gè)跟開(kāi)關(guān)switch有關(guān)的文章,敬請(qǐng)期待~
adio、checkbox和switch應(yīng)該是一個(gè)比較常用的html標(biāo)簽,尤其是在中后臺(tái)ERP系統(tǒng)里面更為常見(jiàn)。不過(guò)瀏覽器自帶的樣式不怎么好看,而且不同瀏覽器效果也不一樣。出于美化和統(tǒng)一視覺(jué)效果的需求,自定義樣式就被提出來(lái)了。
純css實(shí)現(xiàn)的主要手段是利用label標(biāo)簽的模擬功能。label的for屬性可以關(guān)聯(lián)一個(gè)具體的input元素,即使這個(gè)input本身不可被用戶可見(jiàn),有個(gè)與它對(duì)應(yīng)的label后,用戶可以直接通過(guò)和label標(biāo)簽交互來(lái)替代原生的input——而這給我們的樣式模擬留下了空間。簡(jiǎn)而言之就是:
隱藏原生input,樣式定義的過(guò)程留給label (那為什么不直接改變checkbox的樣式?因?yàn)閏heckbox作為瀏覽器默認(rèn)組件,樣式更改上并沒(méi)有l(wèi)abel那么方便,很多屬性對(duì)checkbox都是不起作用的,比如background,而label在樣式上基本和div一樣'任人宰割')
而在選擇事件上,由于css的“相鄰選擇符(E+F)”的存在,讓我們可以直接利用html的默認(rèn)checkbox,免去了js模擬選擇的麻煩。
DEMO的部分CSS3屬性只寫(xiě)了webkit前綴,所以建議用webkit內(nèi)核的瀏覽器查看本頁(yè)案例,當(dāng)然只要你給樣式補(bǔ)上對(duì)應(yīng)的瀏覽器前綴,就可以實(shí)現(xiàn)更多樣式匹配
HTML代碼:
<!-- input的id必須有,這個(gè)是label進(jìn)行元素匹配所必需的 --> <!-- 可以看到每個(gè)input的id和label的“for”屬性對(duì)應(yīng)同一字符串 --> <input type="checkbox" id="checkbox01" /> <label for="checkbox01"></label> <input type="checkbox" id="checkbox02" /> <label for="checkbox02"></label> <input type="checkbox" id="checkbox03" /> <label for="checkbox03"></label> <input type="checkbox" id="checkbox04" /> <label for="checkbox04"></label>
HTML構(gòu)建完成,接下來(lái)是對(duì)應(yīng)的css:
/* 隱藏所有checkbox */ input[type='checkbox'] { display: none; } /* 對(duì)label進(jìn)行模擬.背景圖片隨便拼湊的,不要吐槽品味*/ /* transition效果是做個(gè)背景切換效果,這里單純演示而已,實(shí)際上這個(gè)過(guò)渡不加更自然*/ label { display: inline-block; width: 60px; height: 60px; position: relative; background: url(//www.chitanda.me/images/blank.png); background-position: 0 0px; -webkit-transition: background 0.5s linear; } /* 利用相鄰選擇符和checkbox`:checked`的狀態(tài)偽類來(lái)模擬默認(rèn)選中效果(就是點(diǎn)擊后那個(gè)勾號(hào)的效果) */ /*如果這段代碼注釋,點(diǎn)擊后將沒(méi)有任何反饋給用戶*/ /*因?yàn)閘abel本身是沒(méi)有點(diǎn)擊后被選中的狀態(tài)的,checkbox被隱藏后,這個(gè)狀態(tài)只能手動(dòng)模擬*/ input[type='checkbox']:checked+label { background-position: 0 -60px; }
上面代碼看起來(lái)好像也可以了。不過(guò)仔細(xì)想想,貌似缺了點(diǎn)什么:選項(xiàng)對(duì)應(yīng)的提示文字
對(duì)css不了解的新人可能這時(shí)候第一反應(yīng)就是在label后面用p標(biāo)簽或者span標(biāo)簽來(lái)添加文字。不過(guò)這種方式都不怎么優(yōu)雅。個(gè)人建議用css的::before和::after偽元素(::before和:before是一個(gè)東西。不過(guò)為了把“偽元素”和“偽類”區(qū)分出來(lái),W3C建議的寫(xiě)法是偽元素用::而偽類用:)
/* 偽元素的生效很簡(jiǎn)單,定義`content`就好,其余的屬性和普通div一樣 */ label::after { content: attr(data-name); /*利用attr可以減少css代碼量,data-name寫(xiě)在html部分的label屬性里*/ display: inline-block; position: relative; width: 120px; height: 60px; left: 100%; vertical-align: middle; margin: 10px; }
當(dāng)然既然可以用::after模擬label的文字,那也就可以用::before模擬label的checkbox樣式,這里就不做解析了。
這里提一下偽類和偽元素的區(qū)分:
1)偽類:存在的意義是為了通過(guò)選擇器找到那些不存在于DOM樹(shù)中的信息以及不能被常規(guī)CSS選擇器獲取到的信息。 偽類由一個(gè)冒號(hào):開(kāi)頭,冒號(hào)后面是偽類的名稱和包含在圓括號(hào)中的可選參數(shù)。
常用的偽類:
:active 向被激活的元素添加樣式。 :focus 向擁有鍵盤(pán)輸入焦點(diǎn)的元素添加樣式。 :hover 當(dāng)鼠標(biāo)懸浮在元素上方時(shí),向元素添加樣式。 :link 向未被訪問(wèn)的鏈接添加樣式。 :visited 向已被訪問(wèn)的鏈接添加樣式。 :first-child 向元素的第一個(gè)子元素添加樣式。 :checked 向選中的控件元素添加樣式
2)偽元素:偽元素在DOM樹(shù)中創(chuàng)建了一些抽象元素,這些抽象元素是不存在于文檔語(yǔ)言里的(可以理解為html源碼);
注意: css3為了區(qū)分偽類和偽元素,規(guī)定偽類前面有一個(gè)冒號(hào),偽元素前面有兩個(gè)冒號(hào)
常用偽元素:
關(guān)于偽元素的講解,可以參考CSS偽類與偽元素總是傻傻分不清,這份總結(jié)夠面試用了
::before 為作用元素的第一個(gè)子節(jié)點(diǎn)插入dom中 ::after 為作用元素的最后一個(gè)子節(jié)點(diǎn)插入dom中
自定義radio
html代碼:
<input type="radio" id="radio"> <label for="radio"></label>
css代碼:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; border-radius: 50%; position: relative; } label::after { -webkit-transition: all .5s ease; -moz-transition: all .5s ease; -o-transition: all .5s ease; -ms-transition: all .5s ease; transition: all .5s ease; cursor: pointer; position: absolute; width: 16px; height: 16px; border-radius: 50%; top: 50%; left: 50%; margin-top:-8px; margin-left:-8px; z-index: 1; content: ''; border:1px solid #333; } input:checked+label::after{ background:red; }
實(shí)現(xiàn)效果:
點(diǎn)擊前和點(diǎn)擊后:
自定義checkbox
漂亮的checkbox長(zhǎng)這樣的,看著就很可愛(ài)
我們可以不追求那么完美的樣式,可以實(shí)現(xiàn)下面簡(jiǎn)單好看的樣式就可以
html代碼:
<input type="checkbox" id="checkbox"> <label for="checkbox"></label>
css代碼:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; position: relative; } label::after { -webkit-transition: opacity .5s ease; -moz-transition: opacity .5s ease; -o-transition: opacity .5s ease; -ms-transition: opacity .5s ease; transition: opacity .5s ease; cursor: pointer; position: absolute; content: ''; opacity: 0; } input:checked+label::after{ border: 2px solid #d73d32; border-top: none; border-right: none; -webkit-transform: rotate(-45deg); -ms-transform: rotate(-45deg); transform: rotate(-45deg); width:20px; height:10px; top:50%; margin-top:-8px; left:50%; margin-left:-10px; opacity: 1.0; }
實(shí)現(xiàn)效果:
點(diǎn)擊前和點(diǎn)擊后:
自定義switch
繼續(xù)分享一個(gè)iOS風(fēng)格的switch開(kāi)關(guān)按鈕,樣子也非常常見(jiàn),如圖:
主要是使用了<input ?type="checkbox">來(lái)模擬實(shí)現(xiàn),具體的HTML:
html 代碼:
<label><input class="mui-switch" type="checkbox"> 默認(rèn)未選中</label> <label><input class="mui-switch" type="checkbox" checked> 默認(rèn)選中</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默認(rèn)未選中,簡(jiǎn)單的背景過(guò)渡效果,加mui-switch-animbg類即可</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默認(rèn)選中</label> <label><input class="mui-switch mui-switch-anim" type="checkbox"> 默認(rèn)未選中,過(guò)渡效果,加 mui-switch-anim 類即可</label> <label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默認(rèn)選中</label>
在實(shí)際的使用中后來(lái)又增加了兩個(gè)過(guò)渡效果,分別加?mui-switch-animbg和mui-switch-anim?類即可,具體效果查看下面的demo頁(yè)面。
CSS代碼(SCSS導(dǎo)出的,排版有些奇怪):
css 代碼:
剩下部分
這里給出具體的css,方便大家復(fù)制本地實(shí)現(xiàn)
<style> .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; } .mui-switch:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } .mui-switch:checked { border-color: #64bd63; box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; } .mui-switch:checked:before { left: 21px; } .mui-switch.mui-switch-animbg { transition: background-color ease 0.4s; } .mui-switch.mui-switch-animbg:before { transition: left 0.3s; } .mui-switch.mui-switch-animbg:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: #64bd63; transition: border-color 0.4s, background-color ease 0.4s; } .mui-switch.mui-switch-animbg:checked:before { transition: left 0.3s; } .mui-switch.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; } .mui-switch.mui-switch-anim:before { transition: left 0.3s; } .mui-switch.mui-switch-anim:checked { box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; } .mui-switch.mui-switch-anim:checked:before { transition: left 0.3s; } /*# sourceMappingURL=mui-switch.css.map */ </style>
如果你喜歡scss,那么代碼更加簡(jiǎn)潔
@mixin borderRadius($radius:20px) { border-radius: $radius; border-top-left-radius: $radius; border-top-right-radius: $radius; border-bottom-left-radius: $radius; border-bottom-right-radius: $radius; } $duration: .4s; $checkedColor: #64bd63; .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; @include borderRadius(); background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; &:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; @include borderRadius(); background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } &:checked { border-color: $checkedColor; box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; &:before { left: 21px; } } &.mui-switch-animbg { transition: background-color ease $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: $checkedColor; transition: border-color $duration, background-color ease $duration; &:before { transition: left 0.3s; } } } &.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3; &:before { transition: left 0.3s; } } } }
鏈接文章
https://www.html.cn/archives/9274
https://segmentfault.com/a/1190000003711140
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。