【问题标题】:animating one color of a gradient continuously with css用css连续动画渐变的一种颜色
【发布时间】:2014-04-23 04:47:16
【问题描述】:

在这里看到了一些类似的问题,但似乎无法特别解决这个问题。

我有一个顶部为黑色,底部为蓝色的渐变。我希望蓝色在一个连续的循环中褪色为几种不同的颜色,但黑色保持原位。像这样:http://i.stack.imgur.com/6k9EN.gif

看到了一些演示,例如this one,其中渐变连续动画,但它只是一个大渐变与关键帧垂直移动,这对我不起作用。想知道是否有人可以推荐一个解决方案。

这是到目前为止的非动画代码:http://jsfiddle.net/ssccdd/9qA3L/

body{
      background: -moz-linear-gradient(top, #000000 80%, #3300FF 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(100%,#7db9e8));
      background: -webkit-linear-gradient(top,  #1e5799 80%,#3300FF 100%);
      background: -o-linear-gradient(top,  #000000 80%,#3300FF 100%);
      background: -ms-linear-gradient(top,  #000000 80%,#3300FF 100%);
      background: linear-gradient(to bottom,  #000000 80%,#3300FF 100%);
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#3300FF',GradientType=0 );
      background-attachment: fixed;

}

【问题讨论】:

标签: css css-transitions gradient animated


【解决方案1】:

您可能无法为渐变颜色设置动画,但您可以为背景颜色设置动画。

因此,在您的情况下,具有从黑色到透明的不变线性渐变。然后使用 CSS 过渡更改背景颜色。

这是一个工作示例:http://jsfiddle.net/qSJa8/

我必须包含 javascript 才能从一种颜色切换到另一种颜色:

var classes = [ "stop1", "stop2", "stop3", "stop4" ];

var stopIndex = 0;
var lastClass = classes[0];
window.setInterval(function() {
    ++stopIndex;
    if(stopIndex >= classes.length) {
        stopIndex = 0;
    }
    var newClass = classes[stopIndex];
    $('#sampleDiv').removeClass(lastClass).addClass(newClass);
    lastClass = newClass;

}, 2000);

还有css:

#sampleDiv {
    height:300px;   
    background-image: linear-gradient(180deg, black, black 50%, transparent);
    transition: background-color 2s;
}

.stop1 {
    background-color:blue; 
}

.stop2 {
    background-color:purple;
}

.stop3 {
    background-color:green;
}

.stop4 {
    background-color:yellow;
}

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2016-10-10
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2015-06-30
    • 1970-01-01
    相关资源
    最近更新 更多