使用具有渐变背景的CSS3过渡

alt*_*alt 206 css gradient css3 css-transitions

我正在尝试使用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)))
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*any 161

渐变不支持转换(尽管当前的规范表明它们应该支持像渐变一样通过插值来实现渐变过渡.).

如果想要使用背景渐变的淡入效果,则必须在容器元素上设置不透明度并"转换"不透明度.

(已经有一些浏览器版本支持渐变转换(例如IE10.我在IE中测试了2016年的渐变过渡,它们似乎在当时工作,但我的测试代码不再有效.)

更新:2018年10月 使用未加前缀的新语法[例如径向渐变(...)]的渐变过渡现在已确认在Microsoft Edge 17.17134上工作(再次?).我不知道什么时候加了.仍未使用最新的Firefox和Chrome/Windows 10.

  • IE10完全支持渐变过渡. (3认同)
  • 我在哪里可以找到更新的状态? (2认同)
  • [非常简单的解决方法](https://medium.com/@dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759) (2认同)
  • 你现在可以添加一个关于使用渐变过渡的演示吗?我似乎无法根据你的答案找出答案(并且找不到关于它的其他新闻).我听到的最后一个,你不能在两个背景图像之间转换,这是渐变的. (2认同)

小智 92

一种解决方法是转换背景位置以产生渐变效果:http: //sapphion.com/2011/10/css3-gradient-transition-with-background-position/

具有背景位置的CSS3渐变过渡

虽然您无法使用CSS过渡属性直接为渐变设置动画,但可以为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;  
}  
Run Code Online (Sandbox Code Playgroud)
<div id="DemoGradient"></div>  
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是,它不适用于颜色停止渐变. (18认同)

vin*_*avi 31

解决方案是使用背景位置来模拟梯度过渡. 几个月前,这个解决方案在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;
}
Run Code Online (Sandbox Code Playgroud)

悬停状态

.btn:hover {
   background-position: 0 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 这里有一个小提琴:http://jsfiddle.net/Volker_E/RksTV/关键是属性`background-size`,你不能在IE8中.http://caniuse.com/#search=background-size除此之外,这是一个很好的解决方案. (2认同)

Sky*_*n R 8

我知道这是一个老问题但是mabye有人在纯CSS中享受我的解决方案.渐变从左向右淡出.

.contener{
  background-image:url('http://www.imgbase.info/images/safe-wallpapers/digital_art/3d_landscape/9655_3d_landscape.jpg');   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 */
}




/* ANIMACJA */
@-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));}
}
Run Code Online (Sandbox Code Playgroud)
<div class="contener" style="">
  <div class="ed"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 你是对的:)这只是它适用于图像的例子。 (2认同)

Ray*_*ond 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;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container"><div><a href="#"><span>Press Me</span></a></div></div>
Run Code Online (Sandbox Code Playgroud)

根据您问题中的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;
}
Run Code Online (Sandbox Code Playgroud)

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


Ric*_*Zea 8

对于它的价值,这里有一个Sass mixin:

用法:

@include gradientAnimation(red, blue, .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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Mah*_*zad 6

2021:现在也可以为渐变设置动画。

随着Chrome 85EdgeOpera添加对@property rule 的支持,现在我们可以在 CSS 中做到这一点:

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

@property --colorSecondary {
  syntax: '<color>';
  initial-value: green;
  inherits: false;
}
Run Code Online (Sandbox Code Playgroud)

其余的是常规 CSS。
为变量设置初始渐变颜色并设置这些变量的过渡:

div {
  /* Optional: change the initial value of variables
    --colorPrimary: #f64;
    --colorSecondary: brown;
  */

  background: radial-gradient(circle, var(--colorPrimary) 0%, var(--colorSecondary) 85%) no-repeat;
  transition: --colorPrimary 3s, --colorSecondary 3s;
}
Run Code Online (Sandbox Code Playgroud)

然后,根据所需规则,为变量设置新值:

div:hover {  
  --colorPrimary: yellow;
  --colorSecondary: #f00;
}
Run Code Online (Sandbox Code Playgroud)

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

@property --colorSecondary {
  syntax: '<color>';
  initial-value: rgb(0, 255, 255);
  inherits: false;
}

div {
  width: 200px;
  height: 100px;
  background: radial-gradient(circle, var(--colorPrimary) 0%, var(--colorSecondary) 85%) no-repeat;
  transition: --colorPrimary 3s, --colorSecondary 3s;
}

div:hover {
  --colorPrimary: red;
  --colorSecondary: #00f;
}
Run Code Online (Sandbox Code Playgroud)
<div>Hover over me</div>
Run Code Online (Sandbox Code Playgroud)

请在此处查看完整示例@property此处参阅.
@property 规则是 CSS Houdini 技术的一部分。有关更多信息,请参阅此处此处


小智 5

渐变过渡的部分解决方法是使用插入框阴影 - 您可以过渡框阴影本身或背景颜色 - 例如,如果您创建与背景颜色相同的插入框阴影而不是在背景颜色上使用过渡,则会产生错觉那个普通的背景正在变成径向渐变

.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; 
}
Run Code Online (Sandbox Code Playgroud)

  • 巧妙的解决方案,插入阴影可以完美地创造渐变错觉 (2认同)

Fel*_*aro 5

::before,CSS 伪元素可以很容易地做到这一点!

您所要做的就是使用::before伪元素,其opacity为零。

:hover 上,将opacity切换为 1,如果您按照几个简单的步骤操作,您的过渡应该可以正常工作。


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

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

.element:hover::before {
  opacity: 1;
}


Run Code Online (Sandbox Code Playgroud)
  1. 定位元素并使用background-image设置它的默认渐变
  2. 针对相同的元素,使用::before将它的下一个渐变设置为背景图像,并且它的不透明度为零
  3. 现在使用:hover::before并将不透明度设置为 1
  4. 回到::before块使用:
    • 绝对位置
    • 内容: ””
    • 一个较低的z索引比默认元件
    • 顶部底部右侧左侧设置为零
    • 设置针对opacity属性的过渡属性
  5. 现在一切都应该完成了,你可以 用你喜欢的任何持续时间/延迟/计时功能来调整你的过渡