使用CSS3过渡效果与渐变背景

277

我正在尝试使用CSS在缩略图上悬停时进行过渡,以便在悬停时背景渐变淡入。过渡效果不起作用,但如果我将其简单地更改为rgba()值,它就可以正常工作。渐变不支持吗?我也尝试使用图像,但图像也无法过渡。

我知道这是可能的,因为在另一篇帖子中有人做到了,但我无法弄清楚具体的方法。有人能帮忙吗?以下是一些可供参考的CSS:

#container div a {
  -webkit-transition: background 0.2s linear;
  -moz-transition: background 0.2s linear;
  -o-transition: background 0.2s linear;
  transition: background 0.2s linear;
  position: absolute;
  width: 200px;
  height: 150px;
  border: 1px #000 solid;
  margin: 30px;
  z-index: 2
}

#container div a:hover {
  background: -webkit-gradient(radial, 100 75, 100, 100 75, 0, from(rgba(0, 0, 0, .7)), to(rgba(0, 0, 0, .4)))
}

7
IE10 现在支持渐变过渡,这是一个令人高兴的惊喜! - sirmdawg
@mkprogramming,喔哈,它真的看起来很棒!这里有一个演示(适用于IE10+)。希望其他浏览器也能支持这个酷炫的东西。 - Qtax
这个网站提供了最好的解决方案,对我很有用:http://nimbupani.com/some-css-transition-hacks.html - Shine Cardozo
21个回答

217
现在可以使用@property的解决方法来实现这一点。请参考@Mahozad的回答。
渐变目前还不支持过渡效果(尽管当前规范表示它们应该通过插值来支持类似渐变到渐变的过渡效果)。
如果你想要一个带有背景渐变的淡入效果,你需要在容器元素上设置一个不透明度,并且过渡这个不透明度。
(有一些浏览器版本支持渐变过渡效果,例如IE10。我在2016年测试了IE中的渐变过渡效果,当时它们似乎是可以工作的,但是我的测试代码现在已经无效了。)
2018年10月更新:使用未加前缀的新语法(例如radial-gradient(...))的渐变过渡效果现在在Microsoft Edge 17.17134上已经确认可以工作(再次?)。我不知道这是什么时候添加的。最新的Firefox和Chrome / Windows 10仍然不支持。

1
但是,Edge WebKit 浏览器现在支持新的 Mozilla 遗产渐变规范以及旧的 WebKit 语法。令人困惑的是,它们都有 -webkit 前缀。 - Michael Mullany
4
IE10完全支持渐变过渡效果。 - Niet the Dark Absol
2
这里有一个非常简单好用的解决方法:使用纯CSS实现动态渐变效果 - Dave Lunny
2
现在可以添加一个使用渐变过渡的演示吗?根据您的回答,我似乎无法弄清楚它的用法(并且找不到其他相关信息)。我最后听说的是,您不能在两个背景图像之间进行转换,这就是渐变的情况。 - Chiri Vulpes
1
我也想看一些关于这个的源代码。即使使用了更新的2017语法(据我所知,这意味着只需删除前缀),我仍然无法使其工作。 - Asitis
显示剩余4条评论

123

解决方法之一是通过过渡背景位置来实现渐变效果:

http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/

#DemoGradient{  
    background: -webkit-linear-gradient(#C7D3DC,#5B798E);  
    background: -moz-linear-gradient(#C7D3DC,#5B798E);  
    background: -o-linear-gradient(#C7D3DC,#5B798E);  
    background: linear-gradient(#C7D3DC,#5B798E);  
  
    -webkit-transition: background 1s ease-out;  
    -moz-transition: background 1s ease-out;  
    -o-transition: background 1s ease-out;  
    transition: background 1s ease-out;  
  
    background-size:1px 200px;  
    border-radius: 10px;  
    border: 1px solid #839DB0;  
    cursor:pointer;  
    width: 150px;  
    height: 100px;  
}  
#DemoGradient:Hover{  
    background-position:100px;  
}  
<div id="DemoGradient"></div>  


70
现在可以使用CSS中的@property来实现渐变动画。
@property --myColor1 {
  syntax: '<color>';
  initial-value: magenta;
  inherits: false;
}

@property --myColor2 {
  syntax: '<color>';
  initial-value: green;
  inherits: false;
}

剩下的部分是常规的CSS。 将变量设置为初始渐变颜色,并设置这些变量的过渡效果。
div {
  /* Optional: change initial value of the variables */
  /* --myColor1: #f64; --myColor2: brown; */

  background: linear-gradient(var(--myColor1), var(--myColor2));
  transition: --myColor1 3s, --myColor2 3s;
}

然后,在所需的规则上,为变量设置新值:
div:hover {  
  --myColor1: #f00;
  --myColor2: yellow;
}

@property --myColor1 {
  syntax: '<color>';
  initial-value: #0f0;
  inherits: false;
}

@property --myColor2 {
  syntax: '<color>';
  initial-value: rgb(40, 190, 145);
  inherits: false;
}

div {
  width: 200px;
  height: 100px;
  background: linear-gradient(var(--myColor1), var(--myColor2));
  transition: --myColor1 3s, --myColor2 3s;
}

div:hover {
  --myColor1: red;
  --myColor2: #E1AF2F;
}
<div>Hover over me</div>

查看这里的完整描述和示例,并参考这里的@property规范
@property规则是CSS Houdini技术的一部分。有关更多信息,请参考这里这里,并观看这个视频

你能否用Javascript更改CSS变量?我在尝试中无法实现渐变效果,它立即切换。 - Verminous
@Verminous 是的,你可以。你可以在这里看到一个例子:https://frazer.github.io/cssVariables/index.html,并且代码在这里:https://github.com/Frazer/cssVariables/blob/master/index.html - Frazer Kirkman

36

一种解决方法是使用背景位置(background-position)来模拟渐变过渡效果。这种方法在几个月前被用于 Twitter Bootstrap。

更新

http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614

下面是一个快速的示例:

链接状态

 .btn {
  font-family: "Helvetica Neue", Arial, sans-serif;
  font-size: 12px;
  font-weight: 300;
  position: relative;
  display: inline-block;
  text-decoration: none;
  color: #fff;
  padding: 20px 40px;
  background-image: -moz-linear-gradient(top, #50abdf, #1f78aa);
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#50abdf), to(#1f78aa));
  background-image: -webkit-linear-gradient(top, #50abdf, #1f78aa);
  background-image: -o-linear-gradient(top, #50abdf, #1f78aa);
  background-image: linear-gradient(to bottom, #50abdf, #1f78aa);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff50abdf', endColorstr='#ff1f78aa', GradientType=0);
  background-repeat: repeat-y;
  background-size: 100% 90px;
  background-position: 0 -30px;
  -webkit-transition: all 0.2s linear;
     -moz-transition: all 0.2s linear;
       -o-transition: all 0.2s linear;
          transition: all 0.2s linear;
}

悬停状态

.btn:hover {
   background-position: 0 0;
}

4
这是相关的jsfiddle链接:http://jsfiddle.net/Volker_E/RksTV/ 关键在于"background-size"属性,但IE8不支持。可以参考该链接查询浏览器兼容性:http://caniuse.com/#search=background-size 。除此之外,这是一个很好的解决方案。 - Volker E.

27

说到这里,这是一个 Sass mixin:

用法:

@include gradientAnimation(red, blue, .6s);

Mixin:

@mixin gradientAnimation( $start, $end, $transTime ){
    background-size: 100%;
    background-image: linear-gradient($start, $end);
    position: relative;
    z-index: 100;
    &:before {
        background-image: linear-gradient($end, $start);
        content: "";
        display: block;
        height: 100%;
        position: absolute;
        top: 0; left: 0;
        opacity: 0;
        width: 100%;
        z-index: -100;
        transition: opacity $transTime;
    }
    &:hover {
        &:before {
            opacity: 1;
        }
    }
}

这篇很棒的文章来自Dave Lunny在Medium上发布的: https://medium.com/@dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759


21

::before,CSS伪元素可以轻松搞定!

.element {
  position: relative;
  width: 200px;
  height: 150px;
  background-image: linear-gradient(45deg, blue, aqua);
  z-index: 2;
}

.element::before {
  position: absolute;
  content: "";
  inset: 0; /* same as { top: 0; right: 0; bottom: 0; left: 0; } */
  background-image: linear-gradient(to bottom, red, orange);
  z-index: 1;
  opacity: 0;
  transition: opacity 0.25s linear;
}

.element:hover::before {
  opacity: 1;
}
<body>
  <div class="element"></div>
</body>

只需要使用带有零不透明度的::before伪元素即可。

:hover时,将::before 的不透明度切换为1,并按照以下几个简单步骤进行操作即可使过渡效果生效:

  1. 使用 background-image 设置元素的背景渐变
  2. 使用带有opacity 为0的 ::before 伪元素来设置下一个渐变
  3. :hover::before中将 opacity 设为 1
  4. 确保您的::before 具有:
    • 绝对位置
    • content: ""
    • 低于默认元素的z-index
    • 零的topbottomleftright(或者直接使用inset: 0
    • 过渡目标是opacity,时间间隔由您自己决定。

就是这样!现在你可以根据自己的喜好调整过渡效果的duration / delay / timing-function


如果你的初始背景是透明的,你可以将“重叠”的背景的z-index设置为-1,这样你的字体就不会被覆盖。 - Artur Müller Romanov
请注意,对于跨越多行的内联元素,这可能无法产生预期效果。例如,当尝试在换行的链接下划线时,这将无法正常工作。 - Andre Brdoch

17

我知道这是个老问题,但也许有人会喜欢我的纯CSS解决方案。从左到右渐变淡出。

.contener{   
  width:300px;
  height:200px;
  background-size:cover;
  border:solid 2px black;
}
.ed {
    width: 0px;
    height: 200px;
    background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
    position: relative;
    opacity:0;
    transition:width 20s, opacity 0.6s;
}

.contener:hover .ed{
    width: 300px;
    background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
    position: relative;
    opacity:1;
    transition:width 0.4s, opacity 1.1s;
    transition-delay: width 2s;
    
    animation-name: gradient-fade;
    animation-duration: 1.1s;   
    -webkit-animation-name: gradient-fade; /* Chrome, Safari, Opera */
    -webkit-animation-duration: 1.1s; /* Chrome, Safari, Opera */
}




/* ANIMATION */
@-webkit-keyframes gradient-fade {
    0%   {background:linear-gradient(to right, rgba(0,0,255,0), rgba(255,0,0,0));}
    2%  {background:linear-gradient(to right, rgba(0,0,255,0.01875), rgba(255,0,0,0));}
    4%  {background:linear-gradient(to right, rgba(0,0,255,0.0375), rgba(255,0,0,0.0));}
    6%  {background:linear-gradient(to right, rgba(0,0,255,0.05625), rgba(255,0,0,0.0));}
    8% {background:linear-gradient(to right, rgba(0,0,255,0.075), rgba(255,0,0,0));}
    10%  {background:linear-gradient(to right, rgba(0,0,255,0.09375), rgba(255,0,0,0));}
    12%   {background:linear-gradient(to right, rgba(0,0,255,0.1125), rgba(255,0,0,0));}
    14%  {background:linear-gradient(to right, rgba(0,0,255,0.13125), rgba(255,0,0,0));}
    16%  {background:linear-gradient(to right, rgba(0,0,255,0.15), rgba(255,0,0,0));}
    18%  {background:linear-gradient(to right, rgba(0,0,255,0.16875), rgba(255,0,0,0));}
    20% {background:linear-gradient(to right, rgba(0,0,255,0.1875), rgba(255,0,0,0));}
    22%  {background:linear-gradient(to right, rgba(0,0,255,0.20625), rgba(255,0,0,0.01875));}
    24%   {background:linear-gradient(to right, rgba(0,0,255,0.225), rgba(255,0,0,0.0375));}
    26%  {background:linear-gradient(to right, rgba(0,0,255,0.24375), rgba(255,0,0,0.05625));}
    28%  {background:linear-gradient(to right, rgba(0,0,255,0.2625), rgba(255,0,0,0.075));}
    30%  {background:linear-gradient(to right, rgba(0,0,255,0.28125), rgba(255,0,0,0.09375));}
    32% {background:linear-gradient(to right, rgba(0,0,255,0.3), rgba(255,0,0,0.1125));}
    34%  {background:linear-gradient(to right, rgba(0,0,255,0.31875), rgba(255,0,0,0.13125));}
    36%   {background:linear-gradient(to right, rgba(0,0,255,0.3375), rgba(255,0,0,0.15));}
    38%  {background:linear-gradient(to right, rgba(0,0,255,0.35625), rgba(255,0,0,0.16875));}
    40%  {background:linear-gradient(to right, rgba(0,0,255,0.375), rgba(255,0,0,0.1875));}
    42%  {background:linear-gradient(to right, rgba(0,0,255,0.39375), rgba(255,0,0,0.20625));}
    44% {background:linear-gradient(to right, rgba(0,0,255,0.4125), rgba(255,0,0,0.225));}
    46%  {background:linear-gradient(to right, rgba(0,0,255,0.43125),rgba(255,0,0,0.24375));}
    48%   {background:linear-gradient(to right, rgba(0,0,255,0.45), rgba(255,0,0,0.2625));}
    50%  {background:linear-gradient(to right, rgba(0,0,255,0.46875), rgba(255,0,0,0.28125));}
    52%  {background:linear-gradient(to right, rgba(0,0,255,0.4875), rgba(255,0,0,0.3));}
    54%   {background:linear-gradient(to right, rgba(0,0,255,0.50625), rgba(255,0,0,0.31875));}
    56%  {background:linear-gradient(to right, rgba(0,0,255,0.525), rgba(255,0,0,0.3375));}
    58%  {background:linear-gradient(to right, rgba(0,0,255,0.54375), rgba(255,0,0,0.35625));}
    60%  {background:linear-gradient(to right, rgba(0,0,255,0.5625), rgba(255,0,0,0.375));}
    62% {background:linear-gradient(to right, rgba(0,0,255,0.58125), rgba(255,0,0,0.39375));}
    64%  {background:linear-gradient(to right,rgba(0,0,255,0.6), rgba(255,0,0,0.4125));}
    66%   {background:linear-gradient(to right, rgba(0,0,255,0.61875), rgba(255,0,0,0.43125));}
    68%  {background:linear-gradient(to right, rgba(0,0,255,0.6375), rgba(255,0,0,0.45));}
    70%  {background:linear-gradient(to right, rgba(0,0,255,0.65625), rgba(255,0,0,0.46875));}
    72%  {background:linear-gradient(to right, rgba(0,0,255,0.675), rgba(255,0,0,0.4875));}
    74% {background:linear-gradient(to right, rgba(0,0,255,0.69375), rgba(255,0,0,0.50625));}
    76%  {background:linear-gradient(to right, rgba(0,0,255,0.7125), rgba(255,0,0,0.525));}
    78%   {background:linear-gradient(to right, rgba(0,0,255,0.73125),,rgba(255,0,0,0.54375));}
    80%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.5625));}
    82%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.58125));}
    84%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.6));}
    86% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.61875));}
    88%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.6375));}
    90%   {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.65625));}
    92%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.675));}
    94%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.69375));}
    96%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.7125));}
    98% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.73125),);}
    100%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));}
}
<div class="contener" style="">
  <div class="ed"></div>
</div>


8

基于你提供的CSS代码,我已经尝试了以下代码,并且测试通过(请运行这段代码),请自行尝试:

#container div a {
  display: inline-block;
  margin-top: 10%;
  padding: 1em 2em;
  font-size: 2em;
  color: #fff;
  font-family: arial, sans-serif;
  text-decoration: none;
  border-radius: 0.3em;
  position: relative;
  background-color: #ccc;
  background-image: linear-gradient(to top, #C0357E, #EE5840);
  -webkit-backface-visibility: hidden;
  z-index: 1;
}
     
#container div a:after {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 0.3em;
  background-image: linear-gradient(to top, #6d8aa0, #343436);
  transition: opacity 0.5s ease-out;
  z-index: 2;
  opacity: 0;
}
    
#container div a:hover:after {
  opacity: 1;
}
#container div a span {
  position: relative;
  z-index: 3;
}
<div id="container"><div><a href="#"><span>Press Me</span></a></div></div>

根据您提供的 CSS 代码,我已经尝试了以下代码并且它对我有效,请自行尝试:
    #container div a {
  display: inline-block;
  margin-top: 10%;
  padding: 1em 2em;
  font-size: 2em;
  color: #fff;
  font-family: arial, sans-serif;
  text-decoration: none;
  border-radius: 0.3em;
  position: relative;
  background-color: #ccc;
  background-image: linear-gradient(to top, #C0357E, #EE5840);
  -webkit-backface-visibility: hidden;
  z-index: 1;
}

#container div a:after {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 0.3em;
  background-image: linear-gradient(to top, #6d8aa0, #343436);
  transition: opacity 0.5s ease-out;
  z-index: 2;
  opacity: 0;
}

#container div a:hover:after {
  opacity: 1;
}
#container div a span {
  position: relative;
  z-index: 3;
}

它是否适合你?根据您的需要更改颜色 :)


4

渐变过渡的部分解决方法是使用内嵌的盒子阴影 - 您可以过渡盒子阴影本身或背景颜色 - 例如,如果您创建与背景相同颜色的内嵌盒子阴影,然后在背景颜色上使用过渡,则会产生平面背景变成径向渐变的假象。

.button SPAN {
    padding: 10px 30px; 
    border: 1px solid ##009CC5;

    -moz-box-shadow: inset 0 0 20px 1px #00a7d1;
    -webkit-box-shadow: inset 0 0 20px 1px#00a7d1;
    box-shadow: inset 0 0 20px 1px #00a7d1; 

    background-color: #00a7d1;
    -webkit-transition: background-color 0.5s linear;
    -moz-transition: background-color 0.5s linear;
    -o-transition: background-color 0.5s linear;
    transition: background-color 0.5s linear;
}

.button SPAN:hover {
    background-color: #00c5f7; 
}

2
巧妙的解决方案,内嵌阴影可以完美地创建渐变效果。 - Aziz

3
我想让一个div看起来像一个3D球,并通过颜色过渡。 我发现渐变背景颜色还不能过渡。 我在元素前面(使用z-index)放置了一个径向渐变背景,带有过渡的实心背景。
/* overlay */
z-index : 1;
background : radial-gradient( ellipse at 25% 25%, rgba( 255, 255, 255, 0 ) 0%, rgba( 0, 0, 0, 1 ) 100% );

然后是下面的
:
transition : all 1s cubic-bezier(0.25, 0.46, 0.45, 0.94);

然后改变了 div.ball 的背景颜色,就完成了! https://codepen.io/keldon/pen/dzPxZP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接