9

我的 a 标签上的 CSS 过渡没有淡入淡出,就像根本没有过渡一样!我也尝试过使用transition: background 300ms ease-in-out;但仍然没有运气

CSS:

.FeatureRow a{
    padding: 10px;
    display: block;
    width: 260px;
    height: auto;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-box-shadow: 0px 4px 8px #f5f5f5;
    -moz-box-shadow: 0px 4px 8px #f5f5f5;
    box-shadow: 0px 4px 8px #f5f5f5;
    background-image: -moz-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#fbfbfb),to(#ffffff));
    background-image: -webkit-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: -o-linear-gradient(top,#fbfbfb,#ffffff);
    background-image: linear-gradient(to bottom,#fbfbfb,#ffffff);
    background-repeat: repeat-x;
    transition: all 300ms ease-in-out;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    -ms-transition: all 300ms ease-in-out;
    -o-transition: all 300ms ease-in-out;
}
.FeatureRow a:hover{
    background-image: -moz-linear-gradient(top,#C00,#C0F);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#C00),to(#C0F));
    background-image: -webkit-linear-gradient(top,#C00,#C0F);
    background-image: -o-linear-gradient(top,#C00,#C0F);
    background-image: linear-gradient(to bottom,#C00,#C0F);
}
4

4 回答 4

11

正如 Adrift 所说,这不受支持。你必须模拟它。

这是CSS

.FeatureRow {
    padding: 10px;
    display: block;
    position: relative;
    width: 260px;
    height: 70px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    background-image: linear-gradient(0deg,white,gray);
}

.FeatureRow:after {
    content: '';
    position: absolute;
    left: 0px;
    top: 0px;
    right: 0px;
    bottom: 0px;
    background-image: linear-gradient(0deg,red,magenta);
    opacity: 0;
    transition: all 3s ease-in-out;
}

.FeatureRow:hover:after {
    opacity: 1;
}

我们正在用伪元素覆盖您的 div。每个都有一个渐变。伪元素的不透明度设置为 0;当您将其悬停时,您将不透明度更改为 1;并且可以转换此属性。

演示

于 2013-07-31T17:32:30.197 回答
7

As noted in other answers, it is not possible to animate a gradient. It is however possible to animate background-color. Knowing this, you can layer a transparent gradient on top of a background color, which will allow you to animate one of the colors in your gradient.

To do this, choose the color you want to animate, set it as the background-color, and put rgba(0,0,0,0) (transparent) in its place.

Then all you have to do is on :hover or with javascript change background-color to your desired value

This will only work to transition one color of your gradient. But I think it's still a decent option to transition a gradient

.squareTile {
	background: radial-gradient(#203244eb, rgba(0,0,0,0), #203244eb);
	background-color: #3596ff;
	border-radius: 10px;
  width: 100px;
	height: 100px;
	border: 1px solid black;
	cursor: pointer;
	transition: 1s;
}

.squareTile:hover {
	background-color: #77c577;
}
<div class="squareTile">
</div>

于 2017-11-27T15:40:13.493 回答
4

遗憾的是,目前任何浏览器都不支持对动画(或过渡)使用渐变。查看过渡模块中的此列表,以获取所有当前动画属性的列表

于 2013-07-30T16:30:48.160 回答
0

如前所述,可以为背景渐变设置动画,并且目前有一个非常好的工具可以让您使用该功能:

https://www.gradient-animator.com/

示例输出:

.css-selector {
    background: linear-gradient(270deg, #8ccebd, #146e57);
    background-size: 400% 400%;

    animation: AnimationName 5s ease infinite;
}

@keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
于 2020-05-24T03:02:54.260 回答