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

当前回答

尝试使用:before和:after (ie9+)

#wrapper{
    width:400px;
    height:400px;
    margin:0 auto;
    border: 1px #000 solid;
    position:relative;}
#wrapper:after,
#wrapper:before{
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    content:'';
    background: #1e5799;
    background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8));
    background: -webkit-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
    background: -o-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
    background: -ms-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
    background: linear-gradient(to bottom, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
    opacity:1;
    z-index:-1;
    -webkit-transition: all 2s ease-out;
    -moz-transition: all 2s ease-out;
    -ms-transition: all 2s ease-out;
    -o-transition: all 2s ease-out;
    transition: all 2s ease-out;
}
#wrapper:after{
    opacity:0;
    background: #87e0fd;
    background: -moz-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#87e0fd), color-stop(40%,#53cbf1), color-stop(100%,#05abe0));
    background: -webkit-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
    background: -o-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
    background: -ms-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
    background: linear-gradient(to bottom, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
}
#wrapper:hover:before{opacity:0;}
#wrapper:hover:after{opacity:1;}

其他回答

你可以伪造渐变之间的过渡,在一些堆叠渐变的不透明度中使用过渡,正如这里的一些答案所描述的:

CSS3动画与梯度。

你也可以转换位置,如下所述:

CSS3渐变过渡与背景位置。

这里还有一些技巧:

动画CSS3渐变。

如上所述。渐变目前不支持CSS过渡。但在某些情况下,你可以通过将其中一种颜色设置为透明来解决这个问题,这样其他一些包装元素的背景色就会穿透,并进行过渡。

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

非常感谢

.element { 位置:相对; 宽度:200 px; 身高:150 px; 背景图像:线性梯度(45度,蓝色,水); z - index: 2; } {前.element:: 位置:绝对的; 内容:“”; 插图:0;/*与{top: 0相同;右:0;底部:0;左:0;}* / 背景-图像:线性梯度(底部,红色,橙色); z - index: 1; 透明度:0; 过渡:不透明度0.25s线性; } {前.element:徘徊:: 透明度:1; } 身体< > < div class = "元素" > < / div > 身体< / >

在下面的例子中,一个锚标记有一个子标记和一个孙子标记。孙子有远背景梯度。近背景中的子元素是透明的,但是有渐变要过渡。在悬停时,子物体的不透明度在1秒内从0过渡到1。

下面是CSS:

.bkgrndfar {
  position:absolute;
  top:0;
  left:0;
  z-index:-2;
  height:100%;
  width:100%;
  background:linear-gradient(#eee, #aaa);
}

.bkgrndnear {
  position:absolute;
  top:0;
  left:0;
  height:100%;
  width:100%;
  background:radial-gradient(at 50% 50%, blue 1%, aqua 100%);
  opacity:0;
  transition: opacity 1s;
}

a.menulnk {
  position:relative;
  text-decoration:none;
  color:#333;
  padding: 0 20px;
  text-align:center;
  line-height:27px;
  float:left;
}

a.menulnk:hover {
  color:#eee;
  text-decoration:underline;
}

/* This transitions child opacity on parent hover */
a.menulnk:hover .bkgrndnear {
  opacity:1;
}

这是HTML:

<a href="#" class="menulnk">Transgradient
<div class="bkgrndfar">
  <div class="bkgrndnear">
  </div>
</div>
</a>

以上仅在最新版本的Chrome中进行测试。这些是悬停前,悬停中途和完全过渡的悬停图像: