在许多网站上,例如http://www.clearleft.com,您会注意到当链接被悬停时,它们会逐渐淡出变成不同的颜色,而不是立即转换为默认操作。
我猜想JavaScript用于创建此效果,是否有人知道如何做到这一点?
在许多网站上,例如http://www.clearleft.com,您会注意到当链接被悬停时,它们会逐渐淡出变成不同的颜色,而不是立即转换为默认操作。
我猜想JavaScript用于创建此效果,是否有人知道如何做到这一点?
a {
color:blue;
/* First we need to help some browsers along for this to work.
Just because a vendor prefix is there, doesn't mean it will
work in a browser made by that vendor either, it's just for
future-proofing purposes I guess. */
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
/* ...and now for the proper property */
transition:.5s;
}
a:hover { color:red; }
您还可以使用逗号分隔每个声明,以不同的时间和缓动函数过渡特定的CSS属性,例如:
a {
color:blue; background:white;
-o-transition:color .2s ease-out, background 1s ease-in;
-ms-transition:color .2s ease-out, background 1s ease-in;
-moz-transition:color .2s ease-out, background 1s ease-in;
-webkit-transition:color .2s ease-out, background 1s ease-in;
/* ...and now override with proper CSS property */
transition:color .2s ease-out, background 1s ease-in;
}
a:hover { color:red; background:yellow; }
我知道你在问题中提到了“我假设JavaScript用于创建此效果”,但CSS也可以使用,下面是一个示例。
CSS
.fancy-link {
color: #333333;
text-decoration: none;
transition: color 0.3s linear;
-webkit-transition: color 0.3s linear;
-moz-transition: color 0.3s linear;
}
.fancy-link:hover {
color: #F44336;
}
HTML
<a class="fancy-link" href="#">My Link</a>
以下是上面代码的JSFIDDLE链接!
在回答中,Marcel指出您可以“转换多个CSS属性”,您还可以使用“all”来影响具有所有:hover样式的元素,如下所示。
CSS
.fancy-link {
color: #333333;
text-decoration: none;
transition: all 0.3s linear;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
}
.fancy-link:hover {
color: #F44336;
padding-left: 10px;
}
HTML
<a class="fancy-link" href="#">My Link</a>
这里有一个与“全部”相关的JSFIDDLE示例!
$('a').mouseenter(function(){
$(this).animate({
color: '#ff0000'
}, 1000);
}).mouseout(function(){
$(this).animate({
color: '#000000'
}, 1000);
});
.a {
transition: color 0.3s ease-in-out;
}
.a {
color:turquoise;
}
.a:hover {
color: #454545;
}