radio美化代码实例

快乐打工仔 分类:实例代码

radio单选按钮默认状态下并不美观,但是我们可以通过其他途径将其美化。

代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
<style>
.box{
  width:24px;
  height:18px;
  float:left;
  padding-top:3px;
  cursor:pointer;
  text-align:center;
  margin-right:10px;
  background-image:url(demo/CSS/img/inputradio.gif);
  background-repeat:no-repeat;
  background-position:-24px 0;
}
.antzone{
  opacity: 0;
  -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: alpha(opacity=0);
}
.on{
  background-position: 0 0;
}
</style>
</head>
<body>
<span class="box on">
  <input type="radio" checked="checked" class="antzone">
</span>
</body>
</html>

上面的代码实现了美化效果,原理也很简单,下面做一下简单介绍。

无非就是让浏览者看到的是一个背景图片而不是真正的单选按钮(单选按钮设置为透明状态)。

背景图片如下:

前端教程

通过设置背景图片的位置,来模拟实现单选按钮的选中和非选中状态。

background-position可以参阅CSS background-position一章节。

一线大厂高级前端编写,前端初中阶面试题,帮助初学者应聘,需要联系微信:javadudu

回复

我来回复
  • 暂无回复内容