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,如图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlUAAAA+CAIAAACNyHlVAAANzUlEQVR4nO1d3YocxxWud9JdXweSizzBPkA9g4kFc7uXBb7x2ixsCLnRQsgQVDJi7GRQMkKKGKzEJh7aHrMIB+FMxo6FIztGnFxUd3X9dlfPdE/3znwfB7Gq6fo5p36+PlXdfRgBAAAAwOmBDd0AAAAAABgA4D8AAADgFAH+AwAAAE4RJ8t/K86mUk4ZXw3dknEC9gEA4MjB7r7/4K1375ty9/0HHzz5rCFfvsiclVESYyT174IY37dxkp8zds5l85VtkYtLLjcim/ZQdhfgjHZQW2SUCTdlJ/O59vG7GwAA4JaDvf7xJyJ68+bND6//++q7b77ZvHzxYv32Re2imS+ybJF7yZJT93Qipz3w30qIDcmF8HXYAbIPDpU78VZO3Oa/HcsJ2Qe+IAAAxwVGRP/78YfvX/3n2+3X/3r54p8vPr/54tO33r0fzREhP7pN/NchfMrpBMPyXwSgQAAAjgjs9fevvvt28++vv3r51ZcvvvxsvXq++uRpnP82grvklwtijB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c++x/qsdGdPu1A9TgoBxS97Z9bV/jl1OUT5XrEyynnv+CdnP1jdm/ybY1LaxKnhGtjWaE5kuwPSn8Z/V+bCZehGd6QLv+5j7IDxKScT//Err11je2/j3gnZm1VdXg/xm5wneOodq1WPebkZRWtftrkKtXKJd+RMXcI4rl6sP/a81/feoelpSdcH9xvKZJ2ZvFw6KRMVM0w7i+2f9L4L9G56++hWZfz8l6VtafL3X8V9Yy11uj2wj/xWbihZcxpl1Pcx/kB4nI0Px3EXj/gcg7h9jSVXkCMZnRnIhCHhjFyglJdW7XpvY5ubn8lBRxaq8eUyyPVYJ6xXKpPxpzzdu8aaD10gt94ByrtE+rByV61D2hl2uGnylX21IvNQy2dBYprUi0z/+CLTft5urrNyDF+Yu30Oxrywf1bBtrT1ivLc23dLOM9L7XazFT+9r1Nffx/gMkLiPgPwgEYkuLvYRbKMetHeQWCfgPAoFAIKcoB+W/X+D7nxAIBAIZhyD+AwQCgUBOUQaI//ezXw+vNgQCgUBOXBD/HQAAADhFdMp/+ULILssDAAAAgJ6wK//li4yvnDQpFrn1f2KMNCHmghhvVyAAAAAA9ISd+C9fZJlNdRR2/iSnFg6hnDJQIAAAAHAQtOe/IPkRST71qa4d/xEoEAAAADgQ2vLfRvAA+VG+4GJT/U8QY8QY8ZL/JCfGiJdkqP4rciIizigTRlFyynGICAAAAPSMlvwXcf5ysag4K6esPOfjxvkfSTKJTXBS5UiH7eACAgAAAP2jk/3PlTCcP5PnrP1Pm/9IEuOUCycR5AcAAAAcAh08/2I5f5Tq/6lfWUYVlYL8AAAAgENh//cfbOdP/Wic/6l3HtSBH2PWKxCW84f3HwAAAIADYt/333MxFYHnYQAAAABg1MD3zwAAAIBTBPgPAAAAOEWA/wAAAIBTxP8B5/7iT7mdXtwAAAAASUVORK5CYII=" alt="" />
(4)设置自定义默认样式
准备好一张图,如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAAFuCAIAAACQn6jpAAAMc0lEQVR4nO3dy2sc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OEguznkt3TNIaRk0j3OgHa9k0wo1h1DRJy6U7ugkm8+cCnz5zLUdxxyQtI9zo+Ku7eGkbLbS9uZZb19n25rbRwi5e6gYNI9wwSQvFnRF/gfCS696u5cOo2fFXt9FCy62n8gfOtzpT//6dFkFjGDWzfin5MR9+LvC6wxPuYNYzSavjr7a9OZO0xvxJ9k6LXnDHpm2fGZSjRBAhk4LhmQjCZOjSHSPc2EYLJml18f2rK2cvL54ezdE7LQbRVvrwj9/zc4HXo91nhn7sTaeqlLq6cvabnz7VH+h95J0W/WDNpu0wGe73/ALhE0EoR5j1hlFTz3BKqcuLp495p8U9u/LEXzZJy2dGzPHP+KN7fl7whDso7ug+3/bmNP6Yd1rcHV7fxUvDqIlZj3J0/DkvL/gosVy60w/W9Nyu8ce500IpdXd4fRst6GFPuJMIUlS8fqtp/JF3WlxePJ3ijXCjVPgj77QoM/7IOy3Khs+O+SPvtMjiCzzmD5ztj7zTIsU/O9sX61V34HteHXWnhf6de3al468W9T2fXeF1g8ZgMFBKXV05e6v5SRB7+kKD0TstbjU/md28FPOg5db7wZpLdwq5wtNrez3sdc+3abuL789uXjpwba9zdvMS5WgQbek+nw74gq3t9a6OcpQ+/LY3N4yaXNIxfxXzYBBtbaMFvbDXu7ribWlH9/O94I7ez9+zK3eH10fznl1pufVdvNQP1kzSwqxHuLO/pSsYPlO9036btgfRVj9Ye+Iv6ykwm7t4qeOv9oO1YdS0aTuV68desErOM9XLKLF8Zugm0GW8QbRlhBs6B9FWWsBDccdnRiovZA1v1E85ihIrW8DNpi7dhslQ17CLXr191q/r9tnSfTYPKNpPdG6RK7waPbHROXpWo3OfXfwTm6ea4JfjugNP7NIfleus7uCGeBFHtIXAv8AAPOABD3jAAx7wgAc84AEPeMAfD//q5CuN/x/t+QXpVZyr7QAAAABJRU5ErkJggg==" alt="" />
你可能会问,为什么上面没有在顶端,而是有一定的距离,因为我们的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;
}
结尾:总之,我是完美的解决了我的问题,顺便截图发一个看看
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOIAAAA3CAIAAACegQEcAAAF60lEQVR4nO2bXU/qSBjHz8fayzXZ78EFCclmz97tzZ4NCX6HzS4uIQGjorxLpYqJQnF9AQWNblwTQQgqBcGwGuXF0nYviqW0QFtsgZHnl96cMuNM/+ffZ+aZznxhAWDq+TLpDgCAPGBTAAHApgACgE0BBACbAggANgUQAGwKIADYFEAAsCmAAGBTAAHApgACgE0BBACbAggANgUQAGwKIADYFEAAsCmAAKPblGGY29vb/f399fX15eVlh8PhcrkwDEskEqVSScMuzjiczgcHBxiGra2tOZ1Ol8sVCoWOjo5mR+cRbZrL5Xw+38bGxuXlZbVardfrNE2/vr6Wy+WLi4tg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