自定义表单样式之checkbox和radio
2015-07-12 18:15
746 查看
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)页面结构<form class="form" method="post"> <fieldset> <legend>Which genres do you like?</legend> <input type="checkbox" value="action" id="check-1" name="genre"><label for="check-1" class="">Action / Adventure</label> <input type="checkbox" value="comedy" id="check-2" name="genre"><label for="check-2" class="">Comedy</label> <input type="checkbox" value="epic" id="check-3" name="genre"><label for="check-3" class="">Epic / Historical</label> <input type="checkbox" value="science" id="check-4" name="genre"><label for="check-4" class="">Science Fiction</label> <input type="checkbox" value="romance" id="check-5" name="genre"><label for="check-5" class="">Romance</label> <input type="checkbox" value="western" id="check-6" name="genre"><label for="check-6" class="">Western</label> </fieldset> <fieldset> <legend>Caddyshack is the greatest movie of all time, right?</legend> <input type="radio" value="1" id="radio-1" name="opinions"><label for="radio-1" class="">Totally</label> <input type="radio" value="1" id="radio-2" name="opinions"><label for="radio-2" class="">You must be kidding</label> <input type="radio" value="1" id="radio-3" name="opinions"><label for="radio-3" class="">What's Caddyshack?</label> </fieldset> </form>
(2)jquery code(前提必须引入jquery库)
jQuery.fn.customInput = function(){ $(this).each(function(i){ if($(this).is('[type=checkbox],[type=radio]')){ var input = $(this); //get the associated label using the input's id var label = $('label[for='+input.attr('id')+']'); //get type,for classname suffix var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio'; //wrap the input + label in a div $('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input,label); //find all inputs in this set using the shared name attribute var allInputs = $('input[name='+input.attr('name')+']'); //necessary for browsers that don't support the :hover pseudo class on labels label.hover(function(){ $(this).addClass('hover'); if(inputType == 'checkbox' && input.is(':checked')) { $(this).addClass('checkedHover'); } },function(){ $(this).removeClass('hover checkedHover'); }); //bind custom event, trigger it, bind click,focus,blur events input.bind('updateState',function(){ if(input.is(':checked')){ if(input.is(':radio')){ allInputs.each(function(){ $('label[for='+$(this).attr('id')+']').removeClass('checked'); }); }; label.addClass('checked'); } else { label.removeClass('checked checkedHover checkedFocus'); } }) .trigger('updateState') .click(function(){ $(this).trigger('updateState'); }) .focus(function(){ label.addClass('focus'); if(inputType == 'checkbox' && input.is(':checked')) { $(this).addClass('checkedFocus'); } }) .blur(function(){ label.removeClass('focus checkedFocus'); }); } }); }
引入jquery库,再引入上面的代码后,就可以执行下面的代码
$('input').customInput();
(3)生成的外层div
如果你的代码结构是label和input成对写的话,那么在它们的外层就会生成一个div,如图
/*==鼠标悬停和得到焦点状态==*/ .custom-checkbox label.hover, .custom-checkbox label.focus, .custom-radio label.hover, .custom-radio label.focus { /*background-position: -10px -114px;*/ } /*==选中状态==*/ .custom-checkbox label.checked, .custom-radio label.checked { background-position: 0px -47px; } .custom-checkbox label.checkedHover, .custom-checkbox label.checkedFocus { /*background-position: -10px -314px;*/ } .custom-checkbox label.focus, .custom-radio label.focus { outline: 1px dotted #ccc; }
View Code
结尾:总之,我是完美的解决了我的问题,顺便截图发一个看看
参考来源:
美化表单——自定义单选按钮和复选按钮(个人推荐)美化表单
相关文章推荐
- 用实例详细讲解将PSD转成HTML&CSS
- CSS3学习(二)CSS3动画
- CSS3学习(一) css基础补充
- CSS vertical-align属性的用法
- CSS的SASS样式编程指南
- CSS3的REM设置字体大小
- css3 选择器记
- CSS中overflow:hidden在ie6、ie7无效不能隐藏解决办法
- 在HTML文档中嵌入CSS的三种常用方式
- CSS3文字立体效果
- CSS+Js在段落文字最后自动加入隐藏文字
- 使用brackets编译scss文件
- CSS层叠样式表
- css中box-shadow属性与text-shadow属性
- CSS布局模型
- DOM 操作技术之动态样式
- CSS——LESS
- css3 web字体记
- wordpress利用CSS让每个页面都不一样
- css3新增属性API