【问题标题】:Animate CSS Gradient in Bootstrap nav在 Bootstrap 导航中动画 CSS 渐变
【发布时间】:2015-05-17 07:57:47
【问题描述】:

我正在使用 bootstrap atm 并注意到我的简单 css 导航按钮转换不起作用。

我把它归结为 bootstrap,但随后删除了我的渐变背景,只有纯色背景,它起作用了。

是否有 CSS 渐变过渡的规范,是否存在?有什么办法可以做到这一点吗?如果不是,最好的解决方案是什么?

下面是我用橙色悬停的渐变。

http://jsfiddle.net/MgcDU/10245/

渐变如下,所以它在多个浏览器中都有一定的效果。

    background: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(254, 204, 177, 1)), color-stop(0.5, rgba(241, 116, 50, 1)), color-stop(0.51, rgba(234, 85, 7, 1)), to(rgba(251, 149, 94, 1)));
    background: -webkit-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -moz-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: -o-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    background: linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#feccb1', endColorstr='#fb955e', GradientType=0);

【问题讨论】:

    标签: css twitter-bootstrap css-transitions css-animations


    【解决方案1】:

    linear-gradient 在纯色起作用的情况下失败的原因是因为linear-gradient 实际上创建了一个图像。它对应的普通属性是background-image,而不是background-color

    background-image 不是过渡的,但是使用定位和伪元素,我们可以使用opacity 属性来模拟它。这是一个展示这种技术的简单示例。

    示例:

    li {
        display: inline-block;
        width: 200px;
        height: 200px;
        background: black;
        text-align: center;
        line-height: 200px;
        font-size: 40px;
        /*Must have positioning.*/
        position: relative;
    }
    li a {
        color: white;
        /*Must have positoning.*/
        position: relative;
    }
    li:before {
        /*Make it fill the container.*/
        content: "";
        display: block;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        /*Create gradient.*/
        background: rgb(254, 204, 177);
        background: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(254, 204, 177, 1)), color-stop(0.5, rgba(241, 116, 50, 1)), color-stop(0.51, rgba(234, 85, 7, 1)), to(rgba(251, 149, 94, 1)));
        background: -webkit-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
        background: -moz-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
        background: -o-linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
        background: linear-gradient(rgba(254, 204, 177, 1) 0%, rgba(241, 116, 50, 1) 50%, rgba(234, 85, 7, 1) 51%, rgba(251, 149, 94, 1) 100%);
        /*Transition the opacity.*/
        opacity: 0;
        -webkit-transition: opacity 0.5s ease;
        -moz-transition: opacity 0.5s ease;
        -o-transition: opacity 0.5s ease;
        transition: opacity 0.5s ease;
    }
    
    li:hover:before {
        opacity: 1;
    }
    <li>
        <a href="#">test</a>
    </li>

    【讨论】:

    • 感谢您的解释和解决方案。我发现很难理解为什么它可以为渐变设置动画。干杯!
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多