jQuery实现自定义checkbox和radio样式

 更新时间:2015年07月13日 09:27:34   投稿:hebedich  
这篇文章主要介绍了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)页面结构

<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,如图

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

准备好一张图,如下:

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

/* wrapper divs */
      .custom-checkbox,
      .custom-radio {
        position: relative;
        display: inline-block;
      }
      /* input, label positioning */
      .custom-checkbox input,
      .custom-radio input {
        position: absolute;
        left: 2px;
        top: 3px;
        margin: 0;
        z-index: -1;
      }
      .custom-checkbox label,
      .custom-radio label {
        display: block;
        position: relative;
        z-index: 1;
        font-size: 1.3em;
        padding-right: 1em;
        line-height: 1;
        padding: .5em 0 .5em 30px;
        margin: 0 0 .3em;
        cursor: pointer;
      }

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

/* ==默认状态效果== */
      .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;
      }

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

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

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮

    jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮

    这篇文章主要介绍了jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮,需要的朋友可以参考下
    2015-11-11
  • AJAX实现瀑布流触发分页与分页触发瀑布流的方法

    AJAX实现瀑布流触发分页与分页触发瀑布流的方法

    瀑布流触发分页可以理解为微博中的浏览效果、到一定程度时显示页数进行分页,而分页触发瀑布流可以理解为Twitter拉到一页设定的最大条数后继续用瀑布流展示下一页,接下来我们就来详细看看AJAX实现瀑布流触发分页与分页触发瀑布流的方法
    2016-05-05
  • 实例代码详解jquery.slides.js

    实例代码详解jquery.slides.js

    slides是一款强大的,专业的幻灯片组件,能够全方位对幻灯片的速度控制,本文通过代码实例给大家讲解jquery.slides.js,感兴趣的朋友一起学习
    2015-11-11
  • jquery.param()实现数组或对象的序列化方法

    jquery.param()实现数组或对象的序列化方法

    今天小编就为大家分享一篇jquery.param()实现数组或对象的序列化方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • 谈一谈jQuery核心架构设计

    谈一谈jQuery核心架构设计

    这篇文章主要和大家谈一谈jQuery核心架构设计,什么是jQuery核心架构设计,多方面了解jQuery核心架构设计,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 利用jQuery实现WordPress中@的ID悬浮显示评论内容

    利用jQuery实现WordPress中@的ID悬浮显示评论内容

    这篇文章主要介绍了使用JavaScript实现WordPress中ID悬浮显示评论的功能,就是在楼中楼式的评论中显示被评论的主体内容,需要的朋友可以参考下
    2015-12-12
  • jquery 插件学习(三)

    jquery 插件学习(三)

    用惯jquery的用户可能习惯于连写行为,也就是说在调用一个方法之后,紧跟着调用另一个方法,如此连写不断,形成一个珍珠链,而且编写灵活,方便
    2012-08-08
  • 谈谈对jquery ui tabs 的理解

    谈谈对jquery ui tabs 的理解

    本文给大家介绍jquery ui tabs的理解,包括属性、事件、方法和技巧等相关知识,对jquery ui tabs感兴趣的朋友一起学习吧
    2015-11-11
  • 基于jQuery ztree实现表格风格的树状结构

    基于jQuery ztree实现表格风格的树状结构

    zTree 是一个依靠 jQuery 实现的多功能 “树插件”,zTree 是开源免费的软件。接下来通过本文给大家介绍基于jQuery ztree实现表格风格的树状结构,感兴趣的朋友一起看看吧
    2018-08-08
  • jquery检测input checked 控件是否被选中的方法

    jquery检测input checked 控件是否被选中的方法

    这篇文章主要介绍了jquery检测input checked 控件是否被选中的方法,需要的朋友可以参考下
    2014-03-03

最新评论