css3和jquery实现自定义checkbox和radiobox组件
程序员文章站
2022-05-21 14:14:55
这篇文章主要介绍了css3和jquery实现自定义美化Checkbox和Radiobox组件的示例,需要的朋友可以参考下... 14-04-22...
主要思路是利用隐藏原来的checkbox和radiobox,用一个div来模拟checkbox/radiobox,并使用jquery来完成选择切换时的动画效果。
复制代码
代码如下:<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>favorite music:</p>
</li>
<li>
<input type="checkbox" name="check-box" /> <span>pop music</span></p> <p> </li>
<li>
<input type="checkbox" name="check-box" /> <span>rock music</span></p> <p> </li>
<li>
<input type="checkbox" name="check-box" /> <span>rap music</span></p> <p> </li>
<li>
<input type="checkbox" name="check-box" /> <span>hiphop music</span></p> <p> </li>
</ul>
然后我们用jquery代码来为每一个checkbox和radiobox创建一个div,这个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>');
那么我们接下来要让原来的checkbox隐藏,同时设置模拟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('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;
}
上面这段css3代码就是用样式来自定义div,让div的样式和checkbox和radiobox一样。
最后我们来模拟点击选中和取消选中,这部分也是用jquery来实现:
复制代码
代码如下:$(".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');
});
这段代码可以让选中和取消选中时产生一点小小的动画。