当前位置:Gxlcms > html代码 > 自定义表单样式之checkbox和radio_html/css_WEB-ITnose

自定义表单样式之checkbox和radio_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:45人阅读

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)页面结构

(2)jquery code(前提必须引入jquery库)

  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. //bind custom event, trigger it, bind click,focus,blur events
  23. input.bind('updateState',function(){
  24. if(input.is(':checked')){
  25. if(input.is(':radio')){
  26. allInputs.each(function(){
  27. $('label[for='+$(this).attr('id')+']').removeClass('checked');
  28. });
  29. };
  30. label.addClass('checked');
  31. } else {
  32. label.removeClass('checked checkedHover checkedFocus');
  33. }
  34. })
  35. .trigger('updateState')
  36. .click(function(){
  37. $(this).trigger('updateState');
  38. })
  39. .focus(function(){
  40. label.addClass('focus');
  41. if(inputType == 'checkbox' && input.is(':checked')) {
  42. $(this).addClass('checkedFocus');
  43. }
  44. })
  45. .blur(function(){
  46. label.removeClass('focus checkedFocus');
  47. });
  48. }
  49. });}

引入jquery库,再引入上面的代码后,就可以执行下面的代码

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

(3)生成的外层div

如果你的代码结构是label和input成对写的话,那么在它们的外层就会生成一个div,如图

(4)设置自定义默认样式

准备好一张图,如下:

你可能会问,为什么上面没有在顶端,而是有一定的距离,因为我们的input选项多是居中的,而我们是使用label的背景图片来模拟的,所以我们是为了让input选项居中显示。总之:ico小图标一定要垂直排列,一定要留有一定的距离来达到居中显示。

  1. /* wrapper divs */
  2. .custom-checkbox,
  3. .custom-radio {
  4. position: relative;
  5. display: inline-block;
  6. }
  7. /* input, label positioning */
  8. .custom-checkbox input,
  9. .custom-radio input {
  10. position: absolute;
  11. left: 2px;
  12. top: 3px;
  13. margin: 0;
  14. z-index: -1;
  15. }
  16. .custom-checkbox label,
  17. .custom-radio label {
  18. display: block;
  19. position: relative;
  20. z-index: 1;
  21. font-size: 1.3em;
  22. padding-right: 1em;
  23. line-height: 1;
  24. padding: .5em 0 .5em 30px;
  25. margin: 0 0 .3em;
  26. cursor: pointer;
  27. }

这是最外层的一些设置,当然你可以自己修改

  1. /* ==默认状态效果== */ .custom-checkbox label { background: url(images/checkbox.gif) no-repeat; } .custom-radio label { background: url(images/button-radio.png) no-repeat; } .custom-checkbox label, .custom-radio label { background-position: 0px 0px; }

  1. /*==鼠标悬停和得到焦点状态==*/ .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; }

结尾:总之,我是完美的解决了我的问题,顺便截图发一个看看

参考来源:

美化表单??自定义单选按钮和复选按钮(个人推荐)

美化表单

人气教程排行