查看原文
查看在线演示Demo和更多原文内容教程:http://www.ibloger.net/article/1793.html
默认的超链接下划线动画十分的生硬,我们可以通过一些简单的处理,来制作出带平滑过渡效果的超链接下划线动画效果。
众所周知,超链接<a>
元素在默认情况下鼠标滑过时会出现一条下划线。默认的超链接下划线动画十分的生硬,我们可以通过一些简单的处理,来制作出带平滑过渡效果的超链接下划线动画效果。
先来体验一下这个超链接动画的效果,用鼠标滑过下面的链接试试。
下面我们开始来制作这个效果。首先第一步是要关闭超链的text-decoration
属性,并将超链接的定位设置为相对定位。所以超链接的初始样式为:
a {
position: relative;
color: #000;
text-decoration: none;
}
a:hover {
color: #000;
}
然后我们需要在超链接下方添加一条线条,开始时通过transform
属性来将它隐藏。我们可以通过超链接的:before
伪元素来制作这个线条,然后通过scaleX()
函数将它缩小到0来隐藏它。为了回退(兼容旧的浏览器),我们可以为不支持CSS3
动画的浏览器设置visibility: hidden
将其隐藏。
a:before {
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: #000;
visibility: hidden;
-webkit-transform: scaleX(0);
-o-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
我们为超链接的所有变化都设置了0.3秒的ease-in-out
动画过渡效果。现在我们只需要在鼠标滑过超链接时,使用scaleX()
函数将线条还原即可。
a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
-o-transform: scaleX(1);
transform: scaleX(1);
}
到这里一个与众不同的鼠标滑过超链接效果就制作完成了,是不是非常简单呢?
全文代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content="X-rapido">
<title>超链接下划线特效</title>
<style type="text/css">
a {
font-family: "微软雅黑","Open Sans","Helvetica Neue",Arial,sans-serif;
position: relative;
line-height: 36px;
color: #22a7f0;
text-decoration: none;
}
a:hover {
color: #22a7f0;
}
a:before {
content: "";
position: absolute;
width: 100%;
height: 2px;
bottom:-5px;
left: 0;
background-color: #019875;
visibility: hidden;
-webkit-transform: scaleX(0);
-o-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
-o-transform: scaleX(1);
transform: scaleX(1);
}
</style>
</head>
<body>
<a href="#">超链接下划线动画例子:用鼠标滑过试试!</a>
</body>
</html>