jQuery實現自定義checkbox和radio樣式

1,原由

最近在工做中要實現自定義式的radio樣式,而咱們一般使用的時默認的樣式,由於本身實在想不到解決的方法,因而開始搜索,最終看到了不錯的解決辦法,能夠完美解決咱們遇到的問題。

2,原理

你們都知道在寫結構的時候,radio或checkbox都會跟隨label一塊兒使用,label的for屬性值和input的id值相同的狀況下,點擊label就能夠選中input,這裏正是利用label 來覆蓋咱們的input默認樣式,經過給label添加背景圖片(美化的checkbox或radio),也就是在點擊的過程當中,咱們是看不到默認的input的(給input設置z-index:-1),而點擊的是label,經過不一樣的事件,加載不一樣的背景圖片(這裏是改變背景圖片的位置)

3,設置美化checkbox或radio的默認樣式

(1)頁面結構

css

[xhtml] view plaincopyhtml

  1. <form class="form" method="post">  jquery

  2.   <fieldset>  app

  3.     <legend>Which genres do you like?</legend>  post

  4.     <input type="checkbox" value="action" id="check-1" name="genre"><label for="check-1" class="">Action / Adventure</label>  this

  5.     <input type="checkbox" value="comedy" id="check-2" name="genre"><label for="check-2" class="">Comedy</label>  url

  6.     <input type="checkbox" value="epic" id="check-3" name="genre"><label for="check-3" class="">Epic / Historical</label>  spa

  7.     <input type="checkbox" value="science" id="check-4" name="genre"><label for="check-4" class="">Science Fiction</label>  code

  8.     <input type="checkbox" value="romance" id="check-5" name="genre"><label for="check-5" class="">Romance</label>  orm

  9.     <input type="checkbox" value="western" id="check-6" name="genre"><label for="check-6" class="">Western</label>  

  10.   </fieldset>   

  11.   <fieldset>  

  12.     <legend>Caddyshack is the greatest movie of all time, right?</legend>  

  13.     <input type="radio" value="1" id="radio-1" name="opinions"><label for="radio-1" class="">Totally</label>  

  14.     <input type="radio" value="1" id="radio-2" name="opinions"><label for="radio-2" class="">You must be kidding</label>  

  15.     <input type="radio" value="1" id="radio-3" name="opinions"><label for="radio-3" class="">What's Caddyshack?</label>  

  16.   </fieldset>  

  17. </form>  



(2)jquery code(前提必須引入jquery庫)

[js] view plaincopy

  1. jQuery.fn.customInput = function(){  

  2.   $(this).each(function(i){  

  3.     if($(this).is('[type=checkbox],[type=radio]')){  

  4.       var input = $(this);  

  5.       //get the associated label using the input's id  

  6.       var label = $('label[for='+input.attr('id')+']');  

  7.       //get type,for classname suffix  

  8.       var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio';  

  9.       //wrap the input + label in a div  

  10.       $('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input,label);  

  11.       //find all inputs in this set using the shared name attribute  

  12.       var allInputs = $('input[name='+input.attr('name')+']');  

  13.       //necessary for browsers that don't support the :hover pseudo class on labels  

  14.       label.hover(function(){  

  15.         $(this).addClass('hover');  

  16.         if(inputType == 'checkbox' && input.is(':checked')) {  

  17.           $(this).addClass('checkedHover');  

  18.         }  

  19.       },function(){  

  20.         $(this).removeClass('hover checkedHover');  

  21.       });  

  22.          

  23.       //bind custom event, trigger it, bind click,focus,blur events  

  24.       input.bind('updateState',function(){  

  25.         if(input.is(':checked')){  

  26.           if(input.is(':radio')){  

  27.             allInputs.each(function(){  

  28.               $('label[for='+$(this).attr('id')+']').removeClass('checked');  

  29.             });  

  30.           };  

  31.           label.addClass('checked');  

  32.         } else {  

  33.           label.removeClass('checked checkedHover checkedFocus');  

  34.         }  

  35.       })  

  36.       .trigger('updateState')  

  37.       .click(function(){  

  38.         $(this).trigger('updateState');  

  39.       })  

  40.       .focus(function(){  

  41.         label.addClass('focus');  

  42.         if(inputType == 'checkbox' && input.is(':checked')) {  

  43.           $(this).addClass('checkedFocus');  

  44.         }  

  45.       })  

  46.       .blur(function(){  

  47.         label.removeClass('focus checkedFocus');  

  48.       });          

  49.     }  

  50.   });  

  51. }  



引入jquery庫,再引入上面的代碼後,就能夠執行下面的代碼

[js] view plaincopy

  1. $('input').customInput();  



(3)生成的外層div

若是你的代碼結構是label和input成對寫的話,那麼在它們的外層就會生成一個div,如圖



(4)設置自定義默認樣式

準備好一張圖,以下:



你可能會問,爲何上面沒有在頂端,而是有必定的距離,由於咱們的input選項可能是居中的,而咱們是使用label的背景圖片來模擬的,因此咱們是爲了讓input選項居中顯示。總之:ico小圖標必定要垂直排列,必定要留有必定的距離來達到居中顯示。

[css] view plaincopy

  1. /* wrapper divs */  

  2.       .custom-checkbox,  

  3.       .custom-radio {  

  4.         positionrelative;  

  5.         display: inline-block;  

  6.       }  

  7.       /* input, label positioning */  

  8.       .custom-checkbox input,  

  9.       .custom-radio input {  

  10.         positionabsolute;  

  11.         left: 2px;  

  12.         top: 3px;  

  13.         margin0;  

  14.         z-index-1;  

  15.       }  

  16.       .custom-checkbox label,  

  17.       .custom-radio label {  

  18.         displayblock;  

  19.         positionrelative;  

  20.         z-index1;  

  21.         font-size1.3em;  

  22.         padding-right1em;  

  23.         line-height1;  

  24.         padding: .5em 0 .5em 30px;  

  25.         margin0 0 .3em;  

  26.         cursorpointer;  

  27.       }  



這是最外層的一些設置,固然你能夠本身修改

[css] view plaincopy

  1. /* ==默認狀態效果== */  

  2.       .custom-checkbox label {   

  3.         backgroundurl(images/checkbox.gif) no-repeat;   

  4.       }  

  5.       .custom-radio label {   

  6.         backgroundurl(images/button-radio.png) no-repeat;   

  7.       }  

  8.       .custom-checkbox label,   

  9.       .custom-radio label {  

  10.         background-position0px 0px;  

  11.       }  



[js] view plaincopy

  1. /*==鼠標懸停和獲得焦點狀態==*/  

  2.       .custom-checkbox label.hover,  

  3.       .custom-checkbox label.focus,  

  4.       .custom-radio label.hover,  

  5.       .custom-radio label.focus {  

  6.         /*background-position: -10px -114px;*/  

  7.       }  

  8.         

  9.       /*==選中狀態==*/  

  10.       .custom-checkbox label.checked,  

  11.       .custom-radio label.checked {  

  12.         background-position: 0px -47px;  

  13.       }  

  14.       .custom-checkbox label.checkedHover,  

  15.       .custom-checkbox label.checkedFocus {  

  16.         /*background-position: -10px -314px;*/  

  17.       }  

  18.       .custom-checkbox label.focus,  

  19.       .custom-radio label.focus {  

  20.         outline: 1px dotted #ccc;  

  21.       }  



結尾:總之,我是完美的解決了個人問題,順便截圖發一個看看



以上所述就是本文的所有內容了,但願你們可以喜歡。

相關文章
相關標籤/搜索