CSS backgroudn过渡,这里有什么问题?

时间:2015-06-10 15:50:25

标签: css css3

我觉得诅咒永远不会让背景颜色过渡正确,这让我觉得不合适......

它不会在背景颜色之间转换,只是" blips"从一个到另一个没有任何过渡。我做错了什么?

<div class="flowItem">
    Test
</div>

.flowItem { 
    -webkit-transition: background 1000ms linear;
    -moz-transition: background 1000ms linear;
    -ms-transition: background 1000ms linear;
    -o-transition: background 1000ms linear;
    transition: background 1000ms linear;
}
.flowItem:hover {
    background-image: -moz-linear-gradient(top, #E0FFFF, #87CEEB);
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.00, #E0FFFF), color-stop(1.0, #87CEEB));
}

JSFiddle

2 个答案:

答案 0 :(得分:1)

background-image is not an animatable property.你需要找到其他方法;例如,具有opacity过渡的元素作为背景。

.flowItem {
    position: relative;
    z-index: 0;
}

.flowItem::before {
    background-image: linear-gradient(#E0FFFF, #87CEEB);
    bottom: 0;
    content: '';
    left: 0;
    opacity: 0;
    position: absolute;
    right: 0;
    top: 0;
    transition: opacity 1s linear;
    z-index: -1;
}

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

Updated fiddle

答案 1 :(得分:1)

背景图片不能像其他属性一样“过渡”:在过渡期间应该是background-image的中间值?

作为一种变通方法,您可以将背景应用于伪元素,position: absolute和否定z-index。然后,您可以通过设置其opacity属性的动画来显示伪元素,如此

https://jsfiddle.net/45vdd8fL/4/

.flowItem {
    position: relative;
}

.flowItem::before {
    -webkit-transition: opacity 1s linear;
    -moz-transition: opacity 1s linear;
    -ms-transition: opacity 1s linear;
    -o-transition: opacity 1s linear;
    transition: opacity 1s linear;

    content: "";
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;   
    background-image: -moz-linear-gradient(top, #E0FFFF, #87CEEB);
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.00, #E0FFFF), color-stop(1.0, #87CEEB));

}

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