0

0

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

php中文网

php中文网

发布时间:2016-06-24 11:41:13

|

1060人浏览过

|

来源于php中文网

原创

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

Which genres do you like?
Caddyshack is the greatest movie of all time, right?

 (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			$('
').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)设置自定义默认样式

准备好一张图,如下:

紫东太初
紫东太初

中科院和武汉AI研究院推出的新一代大模型

下载

你可能会问,为什么上面没有在顶端,而是有一定的距离,因为我们的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;            }

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

 

参考来源:

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

美化表单

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

22

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

24

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

99

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

132

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

15

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

65

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

61

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

63

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号