Use CSS3 transitions with gradient backgrounds

后端 未结 16 1586
慢半拍i
慢半拍i 2020-11-21 22:18

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

16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 23:22

    For what it's worth, here's a Sass mixin:

    Usage:

    @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

提交回复
热议问题