CSS 使用带有渐变背景的 CSS3 过渡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6542212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use CSS3 transitions with gradient backgrounds
提问by alt
I'm trying to transition on hover with css over a thumbnail so that on hover, the background gradient fades in. The transition isn't working, but if I simply change it to an rgba()
value, it works fine. Are gradients not supported? I tried using an image too, it won't transition the image either.
我正在尝试使用 css 在缩略图上进行悬停过渡,以便在悬停时背景渐变淡入。过渡不起作用,但如果我只是将其更改为一个rgba()
值,它就可以正常工作。不支持渐变吗?我也尝试使用图像,它也不会转换图像。
I know it's possible, as in another post someone did it, but I can't figure out how exactly. Any help> Here's some CSS to work with:
我知道这是可能的,因为在另一篇文章中有人做到了,但我不知道具体是怎么做的。任何帮助> 这里有一些 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)))
}
采纳答案by Michael Mullany
Gradients don't support transitions yet (although the current spec says they should support like gradient to like gradient transitions via interpolation.).
渐变还不支持过渡(尽管当前的规范说它们应该通过插值支持类似渐变到类似渐变的过渡。)。
If you want a fade-in effect with a background gradient, you have to set an opacityon a container element and 'transition` the opacity.
如果您想要带有背景渐变的淡入效果,则必须在容器元素上设置不透明度并“过渡”不透明度。
(There have been some browser releases that supported transitions on gradients (e.g IE10. I tested gradient transitions in 2016 in IE and they seemed to work at the time, but my test code no longer works.)
(已经有一些浏览器版本支持渐变过渡(例如 IE10。我在 2016 年在 IE 中测试了渐变过渡,当时它们似乎可以工作,但我的测试代码不再有效。)
Update: October 2018Gradient transitions with un-prefixed new syntax [e.g. radial-gradient(...)]now confirmed to work (again?) on Microsoft Edge 17.17134. I don't know when this was added. Still not working on latest Firefox & Chrome / Windows 10.
更新:2018 年 10 月带有无前缀新语法的渐变过渡 [例如径向渐变(...)] 现在确认(再次?)在 Microsoft Edge 17.17134 上工作。不知道什么时候加的 仍然无法在最新的 Firefox 和 Chrome / Windows 10 上工作。
回答by Alan
One work-around is to transition the background position to give the effect that the gradient is changing: http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/
一种解决方法是转换背景位置以产生渐变变化的效果:http: //sapphion.com/2011/10/css3-gradient-transition-with-background-position/
CSS3 gradient transition with background-position
Although you can't directly animate gradients using the CSS transition property, it is possible to animate the background-position property to achieve a simple gradient animation:
The code for this is dead simple:
带有背景位置的 CSS3 渐变过渡
虽然你不能直接使用 CSS transition 属性设置渐变动画,但是可以通过设置 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>
回答by vinzcelavi
A solution is to use background-position to mimic the gradient transition.This solution was used in Twitter Bootstrap a few months ago.
一个解决方案是使用background-position 来模拟渐变过渡。几个月前,这个解决方案在 Twitter Bootstrap 中使用过。
Update
更新
http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614
http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614
Here is a quick example:
这是一个快速示例:
Link state
链接状态
.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;
}
Hover state
悬停状态
.btn:hover {
background-position: 0 0;
}
回答by Ricardo Zea
For what it's worth, here's a Sass mixin:
对于它的价值,这是一个 Sass 混合:
Usage:
用法:
@include gradientAnimation(red, blue, .6s);
@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;
}
}
}
Taken from this awesome post on Medium from Dave Lunny: https://medium.com/@dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759
摘自 Dave Lunny 在 Medium 上的这篇很棒的帖子:https://medium.com/@dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759
回答by Skylin R
I know that is old question but mabye someone enjoy my way of solution in pure CSS. Gradient fade from left to right.
我知道这是个老问题,但也许有人喜欢我在纯 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 */
}
/* 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>
回答by PaulQ
In the following, an anchor tag has a child and a grandchild. The grandchild has the far background gradient. The child in the near background is transparent, but has the gradient to transition to. On hover, the child's opacity is transitioned from 0 to 1, over a period of 1 second.
下面,一个锚标签有一个子标签和一个孙标签。孙子具有远背景渐变。近背景中的孩子是透明的,但有渐变过渡到。在悬停时,子项的不透明度在 1 秒内从 0 过渡到 1。
Here is the CSS:
这是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;
}
And, this is the HTML:
而且,这是 HTML:
<a href="#" class="menulnk">Transgradient
<div class="bkgrndfar">
<div class="bkgrndnear">
</div>
</div>
</a>
The above is only tested in the latest version of Chrome. These are the before hover, halfway on-hover and fully transitioned on-hover images:
以上仅在最新版本的 Chrome 中进行了测试。这些是悬停前、悬停中途和完全过渡的悬停图像:
回答by Frantisek Were Bouska
Partial workaround for gradient transition is to use inset box shadow - you can transition either the box shadow itself, or the background color - e.g. if you create inset box shadow of the same color as background and than use transition on background color, it creates illusion that plain background is changing to radial gradient
渐变过渡的部分解决方法是使用插入框阴影 - 您可以过渡框阴影本身或背景颜色 - 例如,如果您创建与背景颜色相同的插入框阴影而不是在背景颜色上使用过渡,则会产生错觉那个普通的背景正在变成径向渐变
.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;
}
回答by JohnK
You can FAKE transitions between gradients, using transitions in the opacity of a few stacked gradients, as described in a few of the answers here:
您可以伪造渐变之间的过渡,使用几个堆叠渐变的不透明度中的过渡,如这里的一些答案中所述:
CSS3 animation with gradients.
You can also transition the position instead, as described here:
您也可以改为转换位置,如下所述:
CSS3 gradient transition with background-position.
带有 background-position 的 CSS3 渐变过渡。
Some more techniques here:
这里还有一些技巧:
回答by aug
Found a nice hack on codepen that modifies the opacity
property but achieves that fade from one gradient to another by leveraging pseudo-elements. What he does is he sets an :after
so that when you change the opacity of the actual element, the :after
element shows up so it looks as if it were a fade. Thought it'd be useful to share.
在 codepen 上发现了一个不错的 hack,它修改了opacity
属性,但通过利用伪元素实现了从一个渐变到另一个渐变的渐变。他所做的是设置一个,:after
以便当您更改实际元素的不透明度时,该:after
元素会显示出来,看起来就像是淡入淡出。觉得分享有用。
Original codepen: http://codepen.io/sashtown/pen/DfdHh
原始代码笔:http://codepen.io/sashtown/pen/DfdHh
.button {
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, #6d8aa0, #8ba2b4);
-webkit-backface-visibility: hidden;
z-index: 1;
}
.button:after {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 0.3em;
background-image: linear-gradient(to top, #ca5f5e, #d68584);
transition: opacity 0.5s ease-out;
z-index: 2;
opacity: 0;
}
.button:hover:after {
opacity: 1;
}
.button span {
position: relative;
z-index: 3;
}
body {
text-align: center;
background: #ddd;
}
<a class="button" href="#"><span>BUTTON</span></a>
回答by Raymond
Based on the css code in your question, I have try code as follows and it works for me (run the code snippet), and please try by yourself :
根据您问题中的 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>
Based on the css code in your question, I have try code as follows and it works for me, and please try by yourself :
根据您问题中的 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;
}
Does it works for you? Change the color based on your need :)
它对你有用吗?根据您的需要更改颜色:)