jQuery CSS3自定義美化Checkbox實(shí)現(xiàn)代碼
效果圖:

是不是比默認(rèn)的好看多了,個(gè)人的審美觀應(yīng)該還是可以的。
接下來(lái)我們一起來(lái)看看實(shí)現(xiàn)這款美化版Checkbox的源代碼,主要思路是利用隱藏原來(lái)的checkbox和radiobox,用一個(gè)div來(lái)模擬checkbox/radiobox,并使用jQuery來(lái)完成選擇切換時(shí)的動(dòng)畫(huà)效果。
先來(lái)看看HTML代碼:
<div class="wrapper">
<ul>
<li>
<p>Gender:</p>
</li>
<li>
<input type="radio" name="radio-btn" />Male
</li>
<li>
<input type="radio" name="radio-btn" />Female
</li>
</ul>
<ul>
<li>
<p>推薦網(wǎng)站:</p>
</li>
<li>
<input type="checkbox" name="check-box" /> <span>何問(wèn)起</span>
</li>
<li>
<input type="checkbox" name="check-box" /> <span>柯樂(lè)義</span>
</li>
<li>
<input type="checkbox" name="check-box" /> <span>hwq2.com</span>
</li>
<li>
<input type="checkbox" name="check-box" /> <span>hovertree.net</span>
</li>
</ul>
</div>
然后我們用jQuery代碼來(lái)為每一個(gè)checkbox和radiobox創(chuàng)建一個(gè)div,這個(gè)div的classname為check-box和radio-btn。
$("input[name="radio-btn"]").wrap("<div class="radio-btn"><i></i></div>");
$("input[name="check-box"]").wrap("<div class="check-box"><i></i></div>");
那么我們接下來(lái)要讓原來(lái)的checkbox隱藏,同時(shí)設(shè)置模擬div的樣式:
.radio-btn input[type="radio"], .check-box input[type="checkbox"] {
visibility: hidden;
}
.check-box {
width: 22px;
height: 22px;
cursor: pointer;
display: inline-block;
margin: 2px 7px 0 0;
position: relative;
overflow: hidden;
box-shadow: 0 0 1px #ccc;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
background: rgb(255, 255, 255);
background: -moz-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(47%, rgba(246, 246, 246, 1)), color-stop(100%, rgba(237, 237, 237, 1)));
background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -o-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -ms-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffffff", endColorstr="#ededed", GradientType=0);
border: 1px solid #ccc;
}
.check-box i {
background: url("http://hovertree.com/texiao/html5/32/css/check_mark.png") no-repeat center center;
position: absolute;
left: 3px;
bottom: -15px;
width: 16px;
height: 16px;
opacity: .5;
-webkit-transition: all 400ms ease-in-out;
-moz-transition: all 400ms ease-in-out;
-o-transition: all 400ms ease-in-out;
transition: all 400ms ease-in-out;
-webkit-transform:rotateZ(-180deg);
-moz-transform:rotateZ(-180deg);
-o-transform:rotateZ(-180deg);
transform:rotateZ(-180deg);
}
.checkedBox {
-moz-box-shadow: inset 0 0 5px 1px #ccc;
-webkit-box-shadow: inset 0 0 5px 1px #ccc;
box-shadow: inset 0 0 5px 1px #ccc;
border-bottom-color: #fff;
}
.checkedBox i {
bottom: 2px;
-webkit-transform:rotateZ(0deg);
-moz-transform:rotateZ(0deg);
-o-transform:rotateZ(0deg);
transform:rotateZ(0deg);
}
/*Custom radio button*/
.radio-btn {
width: 20px;
height: 20px;
display: inline-block;
float: left;
margin: 3px 7px 0 0;
cursor: pointer;
position: relative;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
border: 1px solid #ccc;
box-shadow: 0 0 1px #ccc;
background: rgb(255, 255, 255);
background: -moz-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(47%, rgba(246, 246, 246, 1)), color-stop(100%, rgba(237, 237, 237, 1)));
background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -o-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: -ms-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(246, 246, 246, 1) 47%, rgba(237, 237, 237, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffffff", endColorstr="#ededed", GradientType=0);
}
.checkedRadio {
-moz-box-shadow: inset 0 0 5px 1px #ccc;
-webkit-box-shadow: inset 0 0 5px 1px #ccc;
box-shadow: inset 0 0 5px 1px #ccc;
}
.radio-btn i {
border: 1px solid #E1E2E4;
width: 10px;
height: 10px;
position: absolute;
left: 4px;
top: 4px;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
}
.checkedRadio i {
background-color: #898A8C;
}/* 何問(wèn)起 hovertree.com */
上面這段CSS3代碼就是用樣式來(lái)自定義div,讓div的樣式和checkbox和radiobox一樣。
最后我們來(lái)模擬點(diǎn)擊選中和取消選中,這部分也是用jQuery來(lái)實(shí)現(xiàn):
$(".radio-btn").on("click", function () {
var _this = $(this),
block = _this.parent().parent();
block.find("input:radio").attr("checked", false);
block.find(".radio-btn").removeClass("checkedRadio");
_this.addClass("checkedRadio");
_this.find("input:radio").attr("checked", true);
});
$.fn.toggleCheckbox = function () {
this.attr("checked", !this.attr("checked"));
}
$(".check-box").on("click", function () {
$(this).find(":checkbox").toggleCheckbox();
$(this).toggleClass("checkedBox");
});/* 何問(wèn)起 hovertree.com */
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)jquery程序設(shè)計(jì)有所幫助。
- 利用jQuery實(shí)現(xiàn)CheckBox全選/全不選/反選的簡(jiǎn)單代碼
- jquery獲取復(fù)選框checkbox的值實(shí)現(xiàn)方法
- jquery獲取復(fù)選框checkbox的值的簡(jiǎn)單實(shí)現(xiàn)方法
- jquery判斷checkbox是否選中及改變checkbox狀態(tài)的實(shí)現(xiàn)方法
- jquery獲取所有選中的checkbox實(shí)現(xiàn)代碼
- JQuery點(diǎn)擊行tr實(shí)現(xiàn)checkBox選中的簡(jiǎn)單實(shí)例
- jQuery判斷checkbox選中狀態(tài)
- jQuery 更改checkbox的狀態(tài),無(wú)效的解決方法
相關(guān)文章
40個(gè)新鮮出爐的jQuery 插件和免費(fèi)教程[上]
作為最流行的 JavaScript 庫(kù),jQuery 擁有豐富的插件,能夠幫助開(kāi)發(fā)者簡(jiǎn)單快速的實(shí)現(xiàn)各種功能2012-07-07
jQuery簡(jiǎn)單獲取DIV和A標(biāo)簽元素位置的方法
這篇文章主要介紹了jQuery簡(jiǎn)單獲取DIV和A標(biāo)簽元素位置的方法,結(jié)合實(shí)例形式分析了jQuery針對(duì)頁(yè)面div及A標(biāo)簽元素相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
JQuery中attr方法和removeAttr方法用法實(shí)例
這篇文章主要介紹了JQuery中attr方法和removeAttr方法用法,實(shí)例分析了jQuery中attr方法設(shè)置屬性與removeAttr方法移除屬性的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
EasyUI框架 使用Ajax提交注冊(cè)信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了EasyUI框架 使用Ajax提交注冊(cè)信息的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
基于Jquery Ajax type的4種類(lèi)型(詳解)
下面小編就為大家?guī)?lái)一篇基于Jquery Ajax type的4種類(lèi)型(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
jQuery mobile轉(zhuǎn)換url地址及獲取url中目錄部分的方法
這篇文章主要介紹了jQuery mobile轉(zhuǎn)換url地址及獲取url中目錄部分的方法,轉(zhuǎn)換url地址本文中介紹的是將相對(duì)url轉(zhuǎn)化為絕對(duì)url,需要的朋友可以參考下2015-12-12
jquery 頁(yè)眉單行信息滾動(dòng)顯示實(shí)現(xiàn)思路及代碼
將滾動(dòng)的內(nèi)容查詢(xún)出來(lái),放在一個(gè)div 或者別的容器里面,具體的實(shí)現(xiàn)代碼如下2014-06-06

