Use CSS3 transitions with gradient backgrounds

后端 未结 16 1573
慢半拍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

    0 讨论(0)
  • 2020-11-21 23:23

    I wanted to have a div appear like a 3D sphere and transition through colors. I discovered that gradient background colors don't transition (yet). I placed a radial gradient background in front of the element (using z-index) with a transitioning solid background.

    /* overlay */
    z-index : 1;
    background : radial-gradient( ellipse at 25% 25%, rgba( 255, 255, 255, 0 ) 0%, rgba( 0, 0, 0, 1 ) 100% );
    

    then the div.ball underneath:

    transition : all 1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    

    then changed the background color of the div.ball and voila!

    https://codepen.io/keldon/pen/dzPxZP

    0 讨论(0)
  • 2020-11-21 23:24

    In the following, an anchor tag has a child and a grandchild. The grandchild has the far background gradient. The child in the near background is transparent, but has the gradient to transition to. On hover, the child's opacity is transitioned from 0 to 1, over a period of 1 second.

    Here is the CSS:

    .bkgrndfar {
      position:absolute;
      top:0;
      left:0;
      z-index:-2;
      height:100%;
      width:100%;
      background:linear-gradient(#eee, #aaa);
    }
    
    .bkgrndnear {
      position:absolute;
      top:0;
      left:0;
      height:100%;
      width:100%;
      background:radial-gradient(at 50% 50%, blue 1%, aqua 100%);
      opacity:0;
      transition: opacity 1s;
    }
    
    a.menulnk {
      position:relative;
      text-decoration:none;
      color:#333;
      padding: 0 20px;
      text-align:center;
      line-height:27px;
      float:left;
    }
    
    a.menulnk:hover {
      color:#eee;
      text-decoration:underline;
    }
    
    /* This transitions child opacity on parent hover */
    a.menulnk:hover .bkgrndnear {
      opacity:1;
    }
    

    And, this is the HTML:

    <a href="#" class="menulnk">Transgradient
    <div class="bkgrndfar">
      <div class="bkgrndnear">
      </div>
    </div>
    </a>
    

    The above is only tested in the latest version of Chrome. These are the before hover, halfway on-hover and fully transitioned on-hover images:

    Before Halfway After

    0 讨论(0)
  • 2020-11-21 23:24

    You can FAKE transitions between gradients, using transitions in the opacity of a few stacked gradients, as described in a few of the answers here:

    CSS3 animation with gradients.

    You can also transition the position instead, as described here:

    CSS3 gradient transition with background-position.

    Some more techniques here:

    Animating CSS3 Gradients.

    0 讨论(0)
提交回复
热议问题