欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一波CSS的Checkbox復(fù)選框樣式代碼分享

  發(fā)布時(shí)間:2015-11-16 11:22:19   作者:佚名   我要評(píng)論
這篇文章主要介紹了一些CSS的Checkbox復(fù)選框樣式的代碼分享,針對(duì)一些簡單頁面控件的設(shè)計(jì),需要的朋友可以參考下

Checkbox復(fù)選框是一個(gè)可能每一個(gè)網(wǎng)站都在使用的HTML元素,但大多數(shù)人并不給它們?cè)O(shè)置樣式,所以在絕大多數(shù)網(wǎng)站它們看起來是一樣的。為什么不把你的網(wǎng)站中的Checkbox設(shè)置一個(gè)與眾不同的樣式,甚至可以讓它看起來一點(diǎn)也不像復(fù)選框。

在本教程中,我們將創(chuàng)建5個(gè)不同的選擇框,你可以在你的網(wǎng)站上使用它。
20151116113259142.png (600×73)

查看演示,可以看到我們將要?jiǎng)?chuàng)建的復(fù)選框樣式。
演示地址

首先,需要添加一段CSS隱藏所有的Checkbox復(fù)選框,下面我們會(huì)改變它的外觀。要做到點(diǎn)需要添加一段代碼到你的CSS文件中。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * 隱藏默認(rèn)的checkbox  
  3.  */  
  4. input[type=checkbox] {   
  5.  visibilityhidden;   
  6. }  

隱藏掉所有的Checkbox復(fù)選框后,我們需要添加一個(gè)label HTML元素,我們都知道,當(dāng)點(diǎn)擊的有for屬性的label標(biāo)簽時(shí),對(duì)應(yīng)的Checkbox復(fù)選框會(huì)被選中。這意味著,我們可以通過label的點(diǎn)擊事件來處理我們的Checkbox復(fù)選框。

樣式一
20151116113348756.png (231×121)

此復(fù)選框風(fēng)格就像一個(gè)解鎖滑塊,滑塊選中和未選中狀態(tài)會(huì)顯示在的不同位置。當(dāng)單擊滑塊按鈕(label標(biāo)簽),將會(huì)選中復(fù)選框,然后滑塊移動(dòng)到ON位置。

我們開始創(chuàng)建復(fù)選框區(qū)的HTML。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <section>  
  2.   <!-- Checbox One -->  
  3.   <h3>Checkbox One</h3>  
  4.    <div class="checkboxOne">  
  5.     <input type="checkbox" value="1" id="checkboxOneInput" name="" />  
  6.     <label for="checkboxOneInput"></label>  
  7.    </div>  
  8. </section>  

因?yàn)檫@個(gè)樣式的復(fù)選框,一個(gè)label不足以完成任務(wù),我們用一個(gè)DIV元素包含checkbox,我們需要使用它們來做黑色條帶和圓角。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the slider bar  
  3.  */  
  4. .checkboxOne {   
  5.  width40px;   
  6.  height10px;   
  7.  background#555;   
  8.  margin20px 80px;   
  9.  positionrelative;   
  10.  border-radius: 3px;   
  11. }  

現(xiàn)在,我們可以把label作為條帶上的滑塊,我們希望按鈕效果是從條帶的一側(cè)移動(dòng)到另一側(cè),我們可以添加label的過渡。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the slider from the label  
  3.  */  
  4. .checkboxOne label {   
  5.  displayblock;   
  6.  width16px;   
  7.  height16px;   
  8.  border-radius: 50%;   
  9.     
  10.  -webkit-transition: all .5s ease;   
  11.  -moz-transition: all .5s ease;   
  12.  -o-transition: all .5s ease;   
  13.  -ms-transition: all .5s ease;   
  14.  transition: all .5s ease;   
  15.  cursorpointer;   
  16.  positionabsolute;   
  17.  top: -3px;   
  18.  left: -3px;   
  19.     
  20.  background#ccc;   
  21. }  

現(xiàn)在這個(gè)滑塊在選中(關(guān)閉)位置,當(dāng)我們選中復(fù)選框,我們希望有一個(gè)反應(yīng)發(fā)生,所以我們可以移動(dòng)滑塊到另一端。我們需要知道,判斷復(fù)選框被選中,如果是則改變label元素的left屬性。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Move the slider in the correct position if the checkbox is clicked  
  3.  */  
  4. .checkboxOne input[type=checkbox]:checked + label {   
  5.  left27px;   
  6. }  

這就是你需要的第一個(gè)Checkbox復(fù)選框的CSS。

樣式二
20151116113455427.png (236×127)

此復(fù)選框風(fēng)格像樣式一樣,但不同的是,這個(gè)滑塊按鈕會(huì)改變顏色。當(dāng)您單擊滑塊按鈕,它移動(dòng)到條帶的另一邊,并改變按鈕的顏色。

HTML代碼和樣式一是完全一樣的。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <section>  
  2.   <!-- Checbox Two -->  
  3.   <h3>Checkbox Two</h3>  
  4.    <div class="checkboxTwo">  
  5.     <input type="checkbox" value="1" id="checkboxTwoInput" name="" />  
  6.     <label for="checkboxTwoInput"></label>  
  7.    </div>  
  8. </section>  

這個(gè)DIV會(huì)變成比樣式一大一些的條帶,label依然是作為滑塊,使用下面的CSS來定義它。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Checkbox Two  
  3.  */  
  4. .checkboxTwo {   
  5.  width120px;   
  6.  height40px;   
  7.  background#333;   
  8.  margin20px 60px;   
  9.     
  10.  border-radius: 50px;   
  11.  positionrelative;   
  12. }  

這個(gè)樣式中間有一個(gè)黑色的條,滑塊會(huì)沿著它左右滑動(dòng),但是DIV元素已經(jīng)使用了,所以我們需要用:before偽類創(chuàng)建一個(gè)新的元素。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the line for the circle to move across  
  3.  */  
  4. .checkboxTwo:before {   
  5.  content'';   
  6.  positionabsolute;   
  7.  top19px;   
  8.  left14px;   
  9.  height2px;   
  10.  width90px;   
  11.  background#111;   
  12. }  

和樣式一一樣,接下來我們?yōu)閘abel寫CSS樣式,把它用作滑塊。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the circle to click  
  3.  */  
  4. .checkboxTwo label {   
  5.  displayblock;   
  6.  width22px;   
  7.  height22px;   
  8.  border-radius: 50%;   
  9.     
  10.  -webkit-transition: all .5s ease;   
  11.  -moz-transition: all .5s ease;   
  12.  -o-transition: all .5s ease;   
  13.  -ms-transition: all .5s ease;   
  14.  transition: all .5s ease;   
  15.  cursorpointer;   
  16.  positionabsolute;   
  17.  top9px;   
  18.  z-index: 1;   
  19.  left12px;   
  20.  background#ddd;   
  21. }  

我要實(shí)現(xiàn)和樣式一差不多的選中狀態(tài),當(dāng)選中時(shí)改變label的left和background屬性。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the click event for the checkbox  
  3.  */  
  4. .checkboxTwo input[type=checkbox]:checked + label {   
  5.  left84px;   
  6.  background#26ca28;   
  7. }  

樣式三
20151116113623446.png (264×126)

這個(gè)復(fù)選框的樣式比樣式二更復(fù)雜一些,它和前面的例子一樣會(huì)左右滑動(dòng),并且當(dāng)改變選中和未選中的狀態(tài)時(shí),滑塊滑動(dòng)到另一側(cè)并且在原位置顯示對(duì)應(yīng)的文本。

首先,我們寫HTML代碼,這和前面是相同的。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <section>  
  2.   <!-- Checbox Three -->  
  3.   <h3>Checkbox Three</h3>  
  4.    <div class="checkboxThree">  
  5.     <input type="checkbox" value="1" id="checkboxThreeInput" name="" />  
  6.     <label for="checkboxThreeInput"></label>  
  7.    </div>  
  8. </section>  

然后,我們用相同的方式把div作為滑塊,下面的代碼會(huì)創(chuàng)建一個(gè)黑色圓角的條帶,我們可以把滑塊和文本放到里面。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Checkbox Three  
  3.  */  
  4. .checkboxThree {   
  5.  width120px;   
  6.  height40px;   
  7.  background#333;   
  8.  margin20px 60px;   
  9.     
  10.  border-radius: 50px;   
  11.  positionrelative;   
  12. }  

當(dāng)滑塊處于未選中狀態(tài)時(shí),滑塊會(huì)在左側(cè),并且右邊顯示”OFF”,當(dāng)點(diǎn)擊的時(shí)候,滑塊移動(dòng)到右側(cè),左側(cè)顯示”ON”。
但是元素?cái)?shù)量不足以讓我們實(shí)現(xiàn)這些功能,所以我們要用:before和:after兩個(gè)偽類創(chuàng)建兩個(gè)元素,分別放置”ON”和”OFF”。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the text for the On position  
  3.  */  
  4. .checkboxThree:before {   
  5.  content'On';   
  6.  positionabsolute;   
  7.  top12px;   
  8.  left13px;   
  9.  height2px;   
  10.  color#26ca28;   
  11.  font-size16px;   
  12. }   
  13. /**  
  14.  * Create the label for the off position  
  15.  */  
  16. .checkboxThree:after {   
  17.  content'Off';   
  18.  positionabsolute;   
  19.  top12px;   
  20.  left84px;   
  21.  height2px;   
  22.  color#ddd;   
  23.  font-size16px;   
  24. }  

和前面一樣,我們來添加滑塊的樣式,當(dāng)被點(diǎn)擊時(shí)它會(huì)移動(dòng)到另一側(cè),并且改變顏色。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the pill to click  
  3.  */  
  4. .checkboxThree label {   
  5.  displayblock;   
  6.  width52px;   
  7.  height22px;   
  8.  border-radius: 50px;   
  9.     
  10.  -webkit-transition: all .5s ease;   
  11.  -moz-transition: all .5s ease;   
  12.  -o-transition: all .5s ease;   
  13.  -ms-transition: all .5s ease;   
  14.  transition: all .5s ease;   
  15.  cursorpointer;   
  16.  positionabsolute;   
  17.  top9px;   
  18.  z-index: 1;   
  19.  left12px;   
  20.  background#ddd;   
  21. }   
  22.     
  23. /**  
  24.  * Create the checkbox event for the label  
  25.  */  
  26. .checkboxThree input[type=checkbox]:checked + label {   
  27.  left60px;   
  28.  background#26ca28;   
  29. }  

樣式四
20151116113733887.png (249×125)

在這個(gè)樣式中,我們會(huì)創(chuàng)建兩個(gè)圓形,當(dāng)點(diǎn)擊時(shí)改變里面的圓形的顏色表示選中與未選中的狀態(tài)。
和前面一樣的HTML代碼。

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <section>  
  2.   <!-- Checbox Four -->  
  3.   <h3>Checkbox Four</h3>  
  4.    <div class="checkboxFour">  
  5.     <input type="checkbox" value="1" id="checkboxFourInput" name="" />  
  6.     <label for="checkboxFourInput"></label>  
  7.    </div>  
  8. </section>  

接下來我們要為checkbox創(chuàng)建外面的圓形,使用CSS的border-radius屬性,并且設(shè)置為100%就可以創(chuàng)建一個(gè)正圓形。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <section>   
  2.   <!-- Checbox Four -->   
  3.   <h3>Checkbox Four</h3>   
  4.    <div class="checkboxFour">   
  5.     <input type="checkbox" value="1" id="checkboxFourInput" name="" />   
  6.     <label for="checkboxFourInput"></label>   
  7.    </div>   
  8. </section>  

然后我們用label元素來創(chuàng)建一個(gè)小一點(diǎn)的圓形,它會(huì)根據(jù)checkbox狀態(tài)來改變顏色。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the checkbox button  
  3.  */  
  4. .checkboxFour label {   
  5.  displayblock;   
  6.  width30px;   
  7.  height30px;   
  8.  border-radius: 100px;   
  9.     
  10.  -webkit-transition: all .5s ease;   
  11.  -moz-transition: all .5s ease;   
  12.  -o-transition: all .5s ease;   
  13.  -ms-transition: all .5s ease;   
  14.  transition: all .5s ease;   
  15.  cursorpointer;   
  16.  positionabsolute;   
  17.  top5px;   
  18.  left5px;   
  19.  z-index: 1;   
  20.     
  21.  background#333;   
  22.     
  23.  -webkit-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);   
  24.  -moz-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);   
  25.  box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);   
  26. }  

當(dāng)復(fù)選框被選中的時(shí)候,我們要改變內(nèi)圈的背景顏色來表示選中狀態(tài)。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the checked state  
  3.  */  
  4. .checkboxFour input[type=checkbox]:checked + label {   
  5.  background#26ca28;   
  6. }  

樣式五
20151116113832981.png (239×109)

這個(gè)復(fù)選框的樣式有些不同,它看起來只是比瀏覽器默認(rèn)的checkbox樣式稍微好了些,但是不同的是我們可以根據(jù)自己的需要來定義它的樣式了。
首先還是一樣的HTML代碼

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <section>  
  2.   <!-- Checbox Five -->  
  3.   <h3>Checkbox Five</h3>  
  4.    <div class="checkboxFive">  
  5.     <input type="checkbox" value="1" id="checkboxFiveInput" name="" />  
  6.     <label for="checkboxFiveInput"></label>  
  7.    </div>  
  8. </section>  

在前面的例子中,我們把div作為checkbox的滑動(dòng)條帶或者外部的圓圈,但是這一次我們不需要了,可以使用div元素來設(shè)置復(fù)選框的區(qū)域。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Checkbox Five  
  3.  */  
  4. .checkboxFive {   
  5.  width25px;   
  6.  margin20px 100px;   
  7.  positionrelative;   
  8. }  

label標(biāo)簽用于Click事件和我們要定義的復(fù)選框的方框樣式。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the box for the checkbox  
  3.  */  
  4. .checkboxFive label {   
  5.  cursorpointer;   
  6.  positionabsolute;   
  7.  width25px;   
  8.  height25px;   
  9.  top: 0;   
  10.    left: 0;   
  11.  background#eee;   
  12.  border:1px solid #ddd;   
  13. }  

接下來,我們要?jiǎng)?chuàng)建方框中的對(duì)勾,對(duì)于這一點(diǎn),我們可以使用:after偽類創(chuàng)建一個(gè)新的元素,為了實(shí)現(xiàn)這個(gè)樣式,我們可以創(chuàng)建一個(gè)5px x 9px的長方形并給他加上邊框。這時(shí)候我們?nèi)サ羯厦婧陀疫叺倪吙蛑?,它?huì)看起來像一個(gè)字母L。然后我們可以使用CSS的transform屬性讓它旋轉(zhuǎn)一下,這樣看起來就像是一個(gè)對(duì)勾。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Display the tick inside the checkbox  
  3.  */  
  4. .checkboxFive label:after {   
  5.  opacity: 0.2;   
  6.  content'';   
  7.  positionabsolute;   
  8.  width9px;   
  9.  height5px;   
  10.  backgroundtransparent;   
  11.  top6px;   
  12.  left7px;   
  13.  border3px solid #333;   
  14.  border-topnone;   
  15.  border-rightnone;   
  16.     
  17.  -webkit-transform: rotate(-45deg);   
  18.  -moz-transform: rotate(-45deg);   
  19.  -o-transform: rotate(-45deg);   
  20.  -ms-transform: rotate(-45deg);   
  21.  transform: rotate(-45deg);   
  22. }  

在上面的CSS中,我們已經(jīng)設(shè)置它的透明度為0.2,所以你會(huì)看到的復(fù)選框有一個(gè)半透明的對(duì)勾。你可以在懸停的時(shí)候加深一點(diǎn),在選中時(shí),可以把設(shè)置為不透明。

CSS Code復(fù)制內(nèi)容到剪貼板
  1. /**  
  2.  * Create the hover event of the tick  
  3.  */  
  4. .checkboxFive label:hover::after {   
  5.  opacity: 0.5;   
  6. }   
  7.     
  8. /**  
  9.  * Create the checkbox state for the tick  
  10.  */  
  11. .checkboxFive input[type=checkbox]:checked + label:after {   
  12.  opacity: 1;   
  13. }  

相關(guān)文章

最新評(píng)論