Use CSS3 transitions with gradient backgrounds

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

    As stated. Gradients aren't currently supported with CSS Transitions. But you could work around it in some cases by setting one of the colors to transparent, so that the background-color of some other wrapping element shines through, and transition that instead.

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

    Try use :before and :after (ie9+)

    #wrapper{
        width:400px;
        height:400px;
        margin:0 auto;
        border: 1px #000 solid;
        position:relative;}
    #wrapper:after,
    #wrapper:before{
        position:absolute;
        top:0;
        left:0;
        width:100%;
        height:100%;
        content:'';
        background: #1e5799;
        background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8));
        background: -webkit-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
        background: -o-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
        background: -ms-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
        background: linear-gradient(to bottom, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
        opacity:1;
        z-index:-1;
        -webkit-transition: all 2s ease-out;
        -moz-transition: all 2s ease-out;
        -ms-transition: all 2s ease-out;
        -o-transition: all 2s ease-out;
        transition: all 2s ease-out;
    }
    #wrapper:after{
        opacity:0;
        background: #87e0fd;
        background: -moz-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#87e0fd), color-stop(40%,#53cbf1), color-stop(100%,#05abe0));
        background: -webkit-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
        background: -o-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
        background: -ms-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
        background: linear-gradient(to bottom, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
    }
    #wrapper:hover:before{opacity:0;}
    #wrapper:hover:after{opacity:1;}
    
    0 讨论(0)
  • 2020-11-21 23:18

    With Chrome 85 (and also Edge) adding support for @property rule, now we can do this in CSS:

    @property --colorPrimary {
      syntax: '<color>';
      initial-value: magenta;
      inherits: false;
    }
    
    @property --colorSecondary {
      syntax: '<color>';
      initial-value: green;
      inherits: false;
    }
    

    The rest is normal CSS.
    Set initial gradient colors to the variables and also set the transition of those variables:

    div {
      /* Optional: change the initial value of variables
        --colorPrimary: #f64;
        --colorSecondary: brown;
      */
    
      background: radial-gradient(circle, var(--colorPrimary) 0%, var(--colorSecondary) 85%) no-repeat;
      transition: --colorPrimary 3s, --colorSecondary 3s;
    }
    

    Then, on the desired rule, set the new values for variables:

    div:hover {  
    --colorPrimary: yellow;
    --colorSecondary: #f00;
    }
    

    @property --colorPrimary {
      syntax: '<color>';
      initial-value: #0f0;
      inherits: false;
    }
    
    @property --colorSecondary {
      syntax: '<color>';
      initial-value: rgb(0, 255, 255);
      inherits: false;
    }
    
    div {
      width: 200px;
      height: 100px;
      background: radial-gradient(circle, var(--colorPrimary) 0%, var(--colorSecondary) 85%) no-repeat;
      transition: --colorPrimary 3s, --colorSecondary 3s;
    }
    
    div:hover {
      --colorPrimary: red;
      --colorSecondary: #00f;
    }
    <div>Hover over me</div>

    See the full example here and refer here for @property support status.
    The @property rule is part of the CSS Houdini technology. For more info refer here and here.

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

    Can't hurt to post another view since there's still not an official way to do this. Wrote a lightweight jQuery plugin with which you can define a background radial gradient and a transition speed. This basic usage will then let it fade in, optimised with requestAnimationFrame (very smooth) :

    $('#element').gradientFade({
    
        duration: 2000,
        from: '(20,20,20,1)',
        to: '(120,120,120,0)'
    });
    

    http://codepen.io/Shikkediel/pen/xbRaZz?editors=001

    Keeps original background and all properties intact. Also has highlight tracking as a setting :

    http://codepen.io/Shikkediel/pen/VYRZZY?editors=001

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

    Found a nice hack on codepen that modifies the opacity property but achieves that fade from one gradient to another by leveraging pseudo-elements. What he does is he sets an :after so that when you change the opacity of the actual element, the :after element shows up so it looks as if it were a fade. Thought it'd be useful to share.

    Original codepen: http://codepen.io/sashtown/pen/DfdHh

    .button {
      display: inline-block;
      margin-top: 10%;
      padding: 1em 2em;
      font-size: 2em;
      color: #fff;
      font-family: arial, sans-serif;
      text-decoration: none;
      border-radius: 0.3em;
      position: relative;
      background-color: #ccc;
      background-image: linear-gradient(to top, #6d8aa0, #8ba2b4);
      -webkit-backface-visibility: hidden;
      z-index: 1;
    }
    .button:after {
      position: absolute;
      content: '';
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 0.3em;
      background-image: linear-gradient(to top, #ca5f5e, #d68584);
      transition: opacity 0.5s ease-out;
      z-index: 2;
      opacity: 0;
    }
    .button:hover:after {
      opacity: 1;
    }
    .button span {
      position: relative;
      z-index: 3;
    }
    body {
      text-align: center;
      background: #ffffd;
    }
    <a class="button" href="#"><span>BUTTON</span></a>

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

    Based on the css code in your question, I have try code as follows and it works for me (run the code snippet), and please try by yourself :

    #container div a {
      display: inline-block;
      margin-top: 10%;
      padding: 1em 2em;
      font-size: 2em;
      color: #fff;
      font-family: arial, sans-serif;
      text-decoration: none;
      border-radius: 0.3em;
      position: relative;
      background-color: #ccc;
      background-image: linear-gradient(to top, #C0357E, #EE5840);
      -webkit-backface-visibility: hidden;
      z-index: 1;
    }
         
    #container div a:after {
      position: absolute;
      content: '';
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 0.3em;
      background-image: linear-gradient(to top, #6d8aa0, #343436);
      transition: opacity 0.5s ease-out;
      z-index: 2;
      opacity: 0;
    }
        
    #container div a:hover:after {
      opacity: 1;
    }
    #container div a span {
      position: relative;
      z-index: 3;
    }
    <div id="container"><div><a href="#"><span>Press Me</span></a></div></div>

    Based on the css code in your question, I have try code as follows and it works for me, and please try by yourself :

        #container div a {
      display: inline-block;
      margin-top: 10%;
      padding: 1em 2em;
      font-size: 2em;
      color: #fff;
      font-family: arial, sans-serif;
      text-decoration: none;
      border-radius: 0.3em;
      position: relative;
      background-color: #ccc;
      background-image: linear-gradient(to top, #C0357E, #EE5840);
      -webkit-backface-visibility: hidden;
      z-index: 1;
    }
    
    #container div a:after {
      position: absolute;
      content: '';
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 0.3em;
      background-image: linear-gradient(to top, #6d8aa0, #343436);
      transition: opacity 0.5s ease-out;
      z-index: 2;
      opacity: 0;
    }
    
    #container div a:hover:after {
      opacity: 1;
    }
    #container div a span {
      position: relative;
      z-index: 3;
    }
    

    Does it works for you? Change the color based on your need :)

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