利用CSS3实现单选框动画特效示例代码

所属分类: 网页制作 / CSS 阅读数: 497
收藏 0 赞 0 分享

先看我们的第一个特效图

注意,这个地方的黄点不是我们特效的一部分,这个黄点之所以存在是我使用的屏幕录制软件自带的。可以很清楚的看到这个特效就是当我们点击的时候,黑点会以一种缩放的动画显示出来,下面来看看具体如何实现。


复制代码
代码如下:

<div class="radio-1">
<input type="radio" name="radio-1" id="radio-1-1" checked="checked">
<label for="radio-1-1"></label>

<input type="radio" name="radio-1" id="radio-1-2">
<label for="radio-1-2"></label>

<input type="radio" name="radio-1" id="radio-1-3">
<label for="radio-1-3"></label>
</div>

这里,我们指定 input 标签的 type 值为 radio,并且一下所有的 radio 的 name 值都相同,这样才可以实现单选效果。对于这里的 label 中的 for 属性,为什么这么设置一开始我也不明白,后来搜索了一下这个属性的定义,反正大概的意思就是说,只要设置了这个属性,当我们点击label 元素的时候,浏览器会自动把焦点转移到 radio 上去。下面用 CSS 对HTML设置效果。


复制代码
代码如下:

.radio-1 {
width: 900px;
padding: 3% 0%;
margin: 10px auto;
background-color: darkseagreen;
text-align: center;
}

.radio-1 label {
display: inline-block;
position: relative;
width: 28px;
height: 28px;
border: 1px solid #cccccc;
border-radius: 100%;
cursor: pointer;
background-color: #ffffff;
margin-right: 10px;
}

这里我们首先看一下对 label 元素的设定,其中大部分属性我都在以前的文章中介绍过了,唯一一个陌生的属性就是 cursor,这个属性是设定鼠标样式的,设置成 pointer 之后,当我们的鼠标放到 label 元素上时,鼠标样式就变成了一只手(在我电脑上是这样)。好了,下面继续来看


复制代码
代码如下:

.radio-1 label:after {
content: "";
position: absolute;
width: 20px;
height: 20px;
top: 4px;
left: 4px;
background-color: #666;
border-radius: 50%;
transform: scale(0);
transition: transform .2s ease-out;
}

这里我们用到了 after 选择器,为什么设置这个属性?就是为了设置如上图所示的小黑点。首先我们设置 content 属性为空,意思就是我们不需要填充任何内容,因为我们只是想设置背景色为黑色,仅此而已。还有,刚开始的时候我们设置 transform 的 scale 值为 0 ,其达到的效果就是将小黑点隐藏。


复制代码
代码如下:

.radio-1 [type="radio"]:checked + label {
background-color: #eeeeee;
transition: background-color .2s ease-in;
}

.radio-1 [type="radio"]:checked + label:after {
transform: scale(1);
transition: transform .2s ease-in;
}

注意这里使用了 + 符号,是什么意思呢?它的学名叫做 相邻同胞选择器,意思就是选择紧接在另一个元素后的元素,而且二者有相同的父元素,在这里的意思就是选中在radio 后出现的 label ,有人要问了,这么设置干嘛,直接设置 label 就是了。想象一下,在一个 非常庞大的系统中,我们可能多次使用到 label 元素,为了避免混淆,这样设置将更加准确。这里我们看到了 transition 属性,这个属性是用于设置过渡效果的。最后,将我们的 radio 隐藏掉,就大功告成了。


复制代码
代码如下:

.radio-1 [type="radio"]{
display: none;
}

这是我们的第二个特效

其实看到这个动画的第一感觉就是,和上一个一模一样,除了将 transform 属性设置成 rotate,下面我就不再解释了,只要你结合上一个例子,就可以很容易做出这么一个效果,我们直接上代码


复制代码
代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Radio</title>
<style>
.radio-2 {
width: 900px;
padding: 3% 0;
margin: 50px auto;
background-color: darkseagreen;
text-align: center;
}

.radio-2 label {
display: inline-block;
width: 28px;
height: 28px;
overflow: hidden;
border: 1px solid #eeeeee;
border-radius: 100%;
margin-right: 10px;
background-color: #ffffff;
position: relative;
cursor: pointer;
}

.radio-2 label:after {
content: "";
position: absolute;
top: 4px;
left: 4px;
width: 20px;
height: 20px;
background-color: #666666;
border-radius: 50%;
transform: rotate(-180deg);
transform-origin: -2px 50%;
transition: transform .2s ease-in;
}

.radio-2 [type="radio"] {
display: none;
}

.radio-2 [type="radio"]:checked + label:after{
transform: rotate(0deg);
transition: transform .2s ease-out;
}
</style>
</head>
<body>
<div class="radio-2">
<input type="radio" name="radio-2" id="radio-2-1" checked="checked">
<label for="radio-2-1"></label>

<input type="radio" name="radio-2" id="radio-2-2">
<label for="radio-2-2"></label>

<input type="radio" name="radio-2" id="radio-2-3">
<label for="radio-2-3"></label>
</div>

</body>
</html>

总结
以上就是这篇文章的全部内容了,希望能对大家的学习或者工作带来一定的帮助。

更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多