在”前端里“发现了”9款超炫的 CSS3 复选框(Checkbox)“的源码,网址为http://www.yyyweb.com/3103.html,可进行在线演示以及源码下载。
9种CSS3复选框效果如下所示:
而我特选其中的一种先进行研究并加以实践:
我选择的效果是第三种:
源码如下,并加以注释:
<!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-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
/* .slideThree */
.slideThree {
width: 80px;
height: 26px;
background: #333;
position: relative;
/* 圆角兼容性 */
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
/* 阴影兼容性 */
-moz-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
-webkit-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.2);
}
/* OFF */
.slideThree:after {
content: 'OFF';
color: #000;
position: absolute;
right: 10px;
z-index: 0;
font: 12px/26px Arial, sans-serif;
font-weight: bold;
text-shadow: 1px 1px 0px rgba(255, 255, 255, 0.15);
}
/* ON */
.slideThree:before {
content: 'ON';
color: #00bf00;
position: absolute;
left: 10px;
z-index: 0;
font: 12px/26px Arial, sans-serif;
font-weight: bold;
}
/* 滑动的按钮 跟checkbox相连 */
.slideThree label {
display: block;
width: 34px;
height: 20px;
cursor: pointer;
position: absolute;
top: 3px;
left: 3px;
z-index: 1;
background: #fcfff4;
background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
background: linear-gradient(to bottom, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
-moz-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
-webkit-box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.3);
}
.slideThree input[type=checkbox] {
visibility: hidden;
}
.slideThree input[type=checkbox]:checked + label {
left: 43px;
}
/* end .slideThree */
</style>
</head>
<body>
<div class="slideThree">
<input type="checkbox" value="None" id="slideThree" name="check" checked />
<label for="slideThree"></label>
</div>
</body>
</html>
效果如下:
然后我就在网上找相关按钮的UI模板psd素材,网址为http://www.58pic.com/wangye/15678651.html
模板如下:
我选择了第一种样式进行了实践,代码如下:
<!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-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
/* .slideThree */
.slideThree {
width: 122px;
height: 38px;
position: relative;
/* 圆角兼容性 */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
background: #ddd;
-moz-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
-webkit-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
}
.slideThree:after {
content: 'OFF';
color: #8b8b8b;
position: absolute;
right: 18px;
z-index: 0;
font: 16px/38px Arial, sans-serif;
font-weight: bold;
text-shadow: 1px 1px 1px rgb(255, 255, 255);
}
.slideThree:before {
content: 'ON';
color: #fff;
position: absolute;
left: 18px;
z-index: 0;
font: 16px/38px Arial, sans-serif;
font-weight: bold;
text-shadow: 1px 1px 1px rgb(0, 0, 0);
}
.slideThree label {
display: block;
width: 64px;
height: 38px;
cursor: pointer;
position: absolute;
top: -1px;
left: 0;
z-index: 1;
background: #fcfff4;
/* 按钮圆角 */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
/* 按钮背景 */
background: #f4f4f3;
background: -moz-linear-gradient(top, #fdfdfd, #f3f3f3);
background: -webkit-linear-gradient(top,#fdfdfd, #f3f3f3);
background: linear-gradient(to bottom,#fdfdfd, #f3f3f3);
/* 按钮过渡 */
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
border:1px solid #d4d4d4;
}
.slideThree input[type=checkbox] {
visibility: hidden;
}
.slideThree input[type=checkbox]:checked + label {
left: 58px;
}
/* end .slideThree */
</style>
</head>
<body>
<div class="slideThree">
<input type="checkbox" value="None" id="slideThree" name="check" checked />
<label for="slideThree"></label>
</div>
</body>
</html>
效果如图:
对于模板中的ON状态背景色为蓝色,OFF状态背景色为灰的情况的处理上遇到了些问题,有尝试在“before”,"after"里修改代码,但是效果不佳,先以JS程序进行实现,但效果也差强人意,待有更好方式再进行更新。
代码如下:
<!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-Type" content="text/html; charset=utf-8" />
<script src="js/jquery-1.4.3.min.js" type="text/javascript"></script>
<title>无标题文档</title>
<style>
/* .slideThree */
.slideThree {
width: 122px;
height: 38px;
position: relative;
/* 圆角兼容性 */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
/* ON状态下 */
background:#3daae7;
-moz-box-shadow: inset 0px 0px 13px rgba(45, 82, 142, 0.47);
-webkit-box-shadow: inset 0px 0px 13px rgba(45, 82, 142, 0.47);
box-shadow: inset 0px 0px 13px rgba(45, 82, 142, 0.47);
overflow:hidden;
/* OFF状态下 */
/*background: #ddd;
-moz-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
-webkit-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);*/
}
.slideThree1 {
background: #ddd;
-moz-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
-webkit-box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
box-shadow: inset 0px 0px 16px rgba(0, 0, 0, 0.29);
}
.slideThree:after {
content: 'OFF';
color: #8b8b8b;
position: absolute;
right:18px;
z-index: 0;
font: 16px/38px Arial, sans-serif;
font-weight: bold;
text-shadow: 1px 1px 1px rgb(255, 255, 255);
}
.slideThree:before {
content: 'ON';
color: #fff;
position: absolute;
left: 18px;
z-index: 0;
font: 16px/38px Arial, sans-serif;
font-weight: bold;
text-shadow: 1px 1px 1px rgb(0, 0, 0);
}
.slideThree label {
display: block;
width: 64px;
height: 36px;
cursor: pointer;
position: absolute;
top: 0;
left: 0;
z-index: 1;
background: #fcfff4;
/* 按钮圆角 */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
/* 按钮背景 */
background: #f4f4f3;
background: -moz-linear-gradient(top, #fdfdfd, #f3f3f3);
background: -webkit-linear-gradient(top,#fdfdfd, #f3f3f3);
background: linear-gradient(to bottom,#fdfdfd, #f3f3f3);
/* 按钮过渡 */
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
border:1px solid #d4d4d4;
}
.slideThree input[type=checkbox] {
visibility: hidden;
}
.slideThree input[type=checkbox]:checked + label {
left: 56px;
}
/* end .slideThree */
</style>
<script type="text/javascript">
$(function(){
$($('#slideThree')).click(function(){
if($("input[type='checkbox']").is(':checked'))
{
$('.slideThree').removeClass('slideThree1');
}
else
{
$('.slideThree').addClass('slideThree1');
}
});
});
</script>
</head>
<body>
<div class="slideThree">
<input type="checkbox" value="None" id="slideThree" name="check" checked />
<label for="slideThree"></label>
</div>
</body>
</html>
效果如下: