1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 自定义表单样式之checkbox和radio【HTML】

自定义表单样式之checkbox和radio【HTML】

时间:2024-03-17 12:47:08

相关推荐

自定义表单样式之checkbox和radio【HTML】

web前端|html教程

自定义表单样式之checkbox和radio

web前端-html教程

1,起因

织梦带文章采集源码,vscode插件大全图解,ubuntu多少内存,简述tomcat的部署,法西斯爬虫,php 开源产品,seo属于什么广告类,下载优酷视频网站源码,吉林二手模板lzw

最近在工作中要实现自定义式的radio样式,而我们通常使用的时默认的样式,因为自己实在想不到解决的方法,于是开始搜索,最终看到了不错的解决办法,可以完美解决我们遇到的问题。

开源网盘系统源码,vscode盒子圆角,ubuntu base使用,怎样开关tomcat,云桌面sqlite,imap服务器端口,bootstrap手风琴插件,前端python自动补全框架,爬虫设计开发,php在线学习视频教程,知名SEO网站,客户信息查询系统网站源码,网页中的透明层,炫酷博客模板下载,dedecms页面使用php调用栏目,教育管理系统php源码,博彩程序php源码lzw

2,原理

手机视频直播视频源码,vscode无法登陆git,ubuntu 14 内核,宝塔面板多个tomcat,深圳爬虫宠物,php 控制浏览器,济南网络seo推广公司排名,网站模板扒皮神器,橱柜模板下载lzw

大家都知道在写结构的时候,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库)

jQuery.fn.customInput = function(){$(this).each(function(i){if($(this).is([type=checkbox],[type=radio])){var input = $(this);//get the associated label using the inputs idvar label = $(label[for=+input.attr(id)+]);//get type,for classname suffixvar inputType = (input.is([type=checkbox])) ? checkbox : adio;//wrap the input + label in a div$(

).insertBefore(input).append(input,label);//find all inputs in this set using the shared name attributevar allInputs = $(input[name=+input.attr( ame)+]);//necessary for browsers that don support the :hover pseudo class on labelslabel.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 eventsinput.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.jpg) no-repeat; } .custom-radio label { background: url(images/button-radio.jpg) 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; }

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

参考来源:

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

美化表单

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。