我试图在悬停时用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)))
}

当前回答

2021年:现在可以动画渐变了(Firefox和Safari还不行)

随着Chrome 85, Edge和Opera添加了对@property规则的支持,现在我们可以在CSS中这样做:

@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 { 语法:“< >颜色”; 初值:# 0 f0; 继承:错误; } @property——myColor2 { 语法:“< >颜色”; 初始值:rgb(40,190,145); 继承:错误; } div { 宽度:200 px; 身高:100 px; background: linear-gradient(var(——myColor1), var(——myColor2)); ——myColor1 3s,——myColor2 3s; } div:{徘徊 ——myColor1:红色; ——myColor2: # E1AF2F; } <div>悬停在我上方</div>

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

其他回答

我知道这是一个老问题,但也许有人喜欢我在纯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>

不管怎样,这里有一个Sass mixin:

用法:

@include gradientAnimation(红色,蓝色,.6s);

混合:

@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/ animing-css -gradients-using-only-css-d2fd7671e759

基于你的问题中的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;
}

这对你有用吗? 根据您的需要更改颜色:)

一个更简洁的解决方案是设置背景色并使用蒙版图像。

#container div a {
  background-color: blue;
  transition: background 0.2s linear;
  width: 200px;
  height: 150px;
  mask-image: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, .7), rgba(0, 0, 0, .4));
}

#container div a:hover { 
  background-color: red;
}

一种解决方案是使用背景位置来模拟梯度过渡。 几个月前,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;
}