自定义表单样式之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;
            }

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

参考来源:

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

美化表单

时间: 2024-08-04 18:31:50

自定义表单样式之checkbox和radio的相关文章

自定义表单——jQuery制作个性化Checkbox

要让表单中的元素具有个性化风格,仅仅通过样式来实现是相当困难的,大家都很清楚,每种内核的浏览器.每种系统平台下对form元素解析的风格都是不一致的.如果你想让form元素在各种平台或者说各种浏览器具有统一的样式风格,仅仅通过CSS来制作是很难达到统一的标准,但实现他也并不是一件很难的事情,你只要借助jQuery就能轻松的实现.那么今天开始一起来学习使用jQuery制作表单元素的系列,让你不在为此感到头痛. 今天我们就先来看第一个实例,这个实例是由Martin Angelov给我们带来的<Bett

div仿checkbox表单样式美化及功能

div仿checkbox表单样式美化及功能(checkbox的样式不好看)素材在底部: 效果图: window.css .bj { position: absolute; top: 0; left: 0; bottom: 1px; width: 100 % ; height: 980px; z - index: 9; background - color: #000; filter: alpha(opacity = 50); - moz - opacity: 0.5; - khtml - opa

struts2 自定义表单

自定义表单一定会涉及<s:iterator/>迭代,一个复杂的自定义表单可能会嵌套n多层迭代. 比如一个自定义一个问卷调查页面涉及3个模型:一个Survey代表一个调查,一个Page代表一个页面,一个Question代表一个问题.每个问题中会包含不同的表单元素,就会涉及迭代. 3个模型类如下: Survey package com.atguigu.surveypark.model; import java.util.Date; import java.util.HashSet; import

JEECG 3.6 自定义表单版本发布,智能快速开发平台

JEECG 3.6(智能快速开发平台) 自定义表单版本发布 平台介绍:JEECG(J2EE Code Generation),一款基于代码生成器的JAVA快速开发平台,集成强大代码生成器和在线开发机制,在线报表配置机制,自定义表单,数据权限等企业平台机制.---------------------------------------- Version:  jeecg-framework-3.6版 本:   JEECG 智能快速开发平台Date:      2015-12-04作 者:      

伪元素 控制表单样式

转载 http://www.csswang.com/exp/4842.html 当开发web应用程序时, 表单样式 是个头疼的问题.以前,web开发人员不得不接受一个现实,就是由客户端浏览器控制 表单样式 .然而,作者通过 伪元素 给web渲染引擎添加钩子,就可以控制表单的显示. 然而,所有这些 伪元素 都是依赖于特定浏览器引擎的(所以要带有浏览器引擎前缀),这样方便区分特定的浏览器引擎.以下是我自己搜集整理的,在Trident, Gecko, 和 WebKit浏览器引擎下面都可用的伪元素列表.

activiti自定义流程之自定义表单(二):创建表单

注:环境配置:activiti自定义流程之自定义表单(一):环境配置 在上一节自定义表单环境搭建好以后,我就正式开始尝试自己创建表单,在后台的处理就比较常规,主要是针对ueditor插件的功能在前端进行修改. 由于自己的前端相关技术太渣,因此好多东西都不会用,导致修改实现的过程也是破费了一番功夫,头皮发麻了好几天. 既然是用别人的插件进行修改,那么我想如果只是单独的贴出我修改后的代码,可能没有前后进行对比好理解,因此这里就把原代码和修改后的同时对比着贴出,以便于朋友们能从对比中更快的得到启发.

织梦DedeCMS自定义表单diy_list.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

[转] 两种自定义表单设计方案

无涯 原文 两种自定义表单设计方案 [原创] 2006-12 最近参与一个项目,客户要求提供自定义表单的功能.主要的要求是:能够对表单的字段进行增删改,对显示表单的格式可以灵活定制.由于客户的表单变动可能比较频繁,所以决定实现自定义表单功能.初步设想出以下两种自定义表单的解决方案,目前只涉及到表单的显示方案. 请大家讨论一下两种方案的优劣,使用哪种较好.也欢迎大家提出更好的解决方案. HTML模板方案 概述:采用HTML模板方式.对于每一种样式的表单定义HTML模板:在模板中定义Web页面的HT

DEDE自定义表单:加个留言板再加验证码功能

公司网站要做个留言板,DEDE自带的留言板是0几年的产物了,真不敢恭维,于是用上多说了,插件就算了,还是用通用代码吧.但是在首页的时候老板 又说要搞个提交留言的功能,那就不能用多说了,用自定义表单轻松搞定.虽然玩DEDE没折腾WP这么有乐趣,不过有时候发现某个功能还是挺有成就感的. 比如你要加个留言板或者报名之类的表单,用这个还是挺方便的,只是有点小麻烦的. 1.找到后台-核心-频道模型-自定义表单 2.然后点击增加新的自定义表单 diyid 这个,不管他,默认就好 自定义表单名称 这个的话,比