Use CSS3 transitions with gradient backgrounds

后端 未结 16 1572
慢半拍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 22:57

    Partial workaround for gradient transition is to use inset box shadow - you can transition either the box shadow itself, or the background color - e.g. if you create inset box shadow of the same color as background and than use transition on background color, it creates illusion that plain background is changing to radial gradient

    .button SPAN {
        padding: 10px 30px; 
        border: 1px solid ##009CC5;
    
        -moz-box-shadow: inset 0 0 20px 1px #00a7d1;
        -webkit-box-shadow: inset 0 0 20px 1px#00a7d1;
        box-shadow: inset 0 0 20px 1px #00a7d1; 
    
        background-color: #00a7d1;
        -webkit-transition: background-color 0.5s linear;
        -moz-transition: background-color 0.5s linear;
        -o-transition: background-color 0.5s linear;
        transition: background-color 0.5s linear;
    }
    
    .button SPAN:hover {
        background-color: #00c5f7; 
    }
    
    0 讨论(0)
  • 2020-11-21 22:58

    Gradients don't support transitions yet (although the current spec says they should support like gradient to like gradient transitions via interpolation.).

    If you want a fade-in effect with a background gradient, you have to set an opacity on a container element and 'transition` the opacity.

    (There have been some browser releases that supported transitions on gradients (e.g IE10. I tested gradient transitions in 2016 in IE and they seemed to work at the time, but my test code no longer works.)

    Update: October 2018 Gradient transitions with un-prefixed new syntax [e.g. radial-gradient(...)]now confirmed to work (again?) on Microsoft Edge 17.17134. I don't know when this was added. Still not working on latest Firefox & Chrome / Windows 10.

    0 讨论(0)
  • 2020-11-21 22:58

    One work-around is to transition the background position to give the effect that the gradient is changing: http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/

    CSS3 gradient transition with background-position

    Although you can’t directly animate gradients using the CSS transition property, it is possible to animate the background-position property to achieve a simple gradient animation:

    The code for this is dead simple:

    #DemoGradient{  
        background: -webkit-linear-gradient(#C7D3DC,#5B798E);  
        background: -moz-linear-gradient(#C7D3DC,#5B798E);  
        background: -o-linear-gradient(#C7D3DC,#5B798E);  
        background: linear-gradient(#C7D3DC,#5B798E);  
      
        -webkit-transition: background 1s ease-out;  
        -moz-transition: background 1s ease-out;  
        -o-transition: background 1s ease-out;  
        transition: background 1s ease-out;  
      
        background-size:1px 200px;  
        border-radius: 10px;  
        border: 1px solid #839DB0;  
        cursor:pointer;  
        width: 150px;  
        height: 100px;  
    }  
    #DemoGradient:Hover{  
        background-position:100px;  
    }  
    <div id="DemoGradient"></div>  

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

    A solution is to use background-position to mimic the gradient transition. This solution was used in Twitter Bootstrap a few months ago.

    Update

    http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614

    Here is a quick example:

    Link state

     .btn {
      font-family: "Helvetica Neue", Arial, sans-serif;
      font-size: 12px;
      font-weight: 300;
      position: relative;
      display: inline-block;
      text-decoration: none;
      color: #fff;
      padding: 20px 40px;
      background-image: -moz-linear-gradient(top, #50abdf, #1f78aa);
      background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#50abdf), to(#1f78aa));
      background-image: -webkit-linear-gradient(top, #50abdf, #1f78aa);
      background-image: -o-linear-gradient(top, #50abdf, #1f78aa);
      background-image: linear-gradient(to bottom, #50abdf, #1f78aa);
      background-repeat: repeat-x;
      filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff50abdf', endColorstr='#ff1f78aa', GradientType=0);
      background-repeat: repeat-y;
      background-size: 100% 90px;
      background-position: 0 -30px;
      -webkit-transition: all 0.2s linear;
         -moz-transition: all 0.2s linear;
           -o-transition: all 0.2s linear;
              transition: all 0.2s linear;
    }
    

    Hover state

    .btn:hover {
       background-position: 0 0;
    }
    
    0 讨论(0)
  • 2020-11-21 23:00

    I use this at work :) IE6+ https://gist.github.com/GrzegorzPerko/7183390

    Don't forget about <element class="ahover"><span>Text</span></a> if you use a text element.

    .ahover {
        display: block;
        /** text-indent: -999em; ** if u use only only img **/
        position: relative;
    }
    .ahover:after {
        content: "";
        height: 100%;
        left: 0;
        opacity: 0;
        position: absolute;
        top: 0;
        transition: all 0.5s ease 0s;
        width: 100%;
        z-index: 1;
    }
    .ahover:hover:after {
        opacity: 1;
    }
    .ahover span {
        display: block;
        position: relative;
        z-index: 2;
    }
    
    0 讨论(0)
  • 2020-11-21 23:08

    I know that is old question but mabye someone enjoy my way of solution in pure CSS. Gradient fade from left to right.

    .contener{
      background-image:url('http://www.imgbase.info/images/safe-wallpapers/digital_art/3d_landscape/9655_3d_landscape.jpg');   width:300px;
      height:200px;
      background-size:cover;
      border:solid 2px black;
    }
    .ed {
        width: 0px;
        height: 200px;
        background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
        position: relative;
        opacity:0;
        transition:width 20s, opacity 0.6s;
    }
    
    .contener:hover .ed{
        width: 300px;
        background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
        position: relative;
        opacity:1;
        transition:width 0.4s, opacity 1.1s;
        transition-delay: width 2s;
        
        animation-name: gradient-fade;
        animation-duration: 1.1s;   
        -webkit-animation-name: gradient-fade; /* Chrome, Safari, Opera */
        -webkit-animation-duration: 1.1s; /* Chrome, Safari, Opera */
    }
    
    
    
    
    /* ANIMATION */
    @-webkit-keyframes gradient-fade {
        0%   {background:linear-gradient(to right, rgba(0,0,255,0), rgba(255,0,0,0));}
        2%  {background:linear-gradient(to right, rgba(0,0,255,0.01875), rgba(255,0,0,0));}
        4%  {background:linear-gradient(to right, rgba(0,0,255,0.0375), rgba(255,0,0,0.0));}
        6%  {background:linear-gradient(to right, rgba(0,0,255,0.05625), rgba(255,0,0,0.0));}
        8% {background:linear-gradient(to right, rgba(0,0,255,0.075), rgba(255,0,0,0));}
        10%  {background:linear-gradient(to right, rgba(0,0,255,0.09375), rgba(255,0,0,0));}
        12%   {background:linear-gradient(to right, rgba(0,0,255,0.1125), rgba(255,0,0,0));}
        14%  {background:linear-gradient(to right, rgba(0,0,255,0.13125), rgba(255,0,0,0));}
        16%  {background:linear-gradient(to right, rgba(0,0,255,0.15), rgba(255,0,0,0));}
        18%  {background:linear-gradient(to right, rgba(0,0,255,0.16875), rgba(255,0,0,0));}
        20% {background:linear-gradient(to right, rgba(0,0,255,0.1875), rgba(255,0,0,0));}
        22%  {background:linear-gradient(to right, rgba(0,0,255,0.20625), rgba(255,0,0,0.01875));}
        24%   {background:linear-gradient(to right, rgba(0,0,255,0.225), rgba(255,0,0,0.0375));}
        26%  {background:linear-gradient(to right, rgba(0,0,255,0.24375), rgba(255,0,0,0.05625));}
        28%  {background:linear-gradient(to right, rgba(0,0,255,0.2625), rgba(255,0,0,0.075));}
        30%  {background:linear-gradient(to right, rgba(0,0,255,0.28125), rgba(255,0,0,0.09375));}
        32% {background:linear-gradient(to right, rgba(0,0,255,0.3), rgba(255,0,0,0.1125));}
        34%  {background:linear-gradient(to right, rgba(0,0,255,0.31875), rgba(255,0,0,0.13125));}
        36%   {background:linear-gradient(to right, rgba(0,0,255,0.3375), rgba(255,0,0,0.15));}
        38%  {background:linear-gradient(to right, rgba(0,0,255,0.35625), rgba(255,0,0,0.16875));}
        40%  {background:linear-gradient(to right, rgba(0,0,255,0.375), rgba(255,0,0,0.1875));}
        42%  {background:linear-gradient(to right, rgba(0,0,255,0.39375), rgba(255,0,0,0.20625));}
        44% {background:linear-gradient(to right, rgba(0,0,255,0.4125), rgba(255,0,0,0.225));}
        46%  {background:linear-gradient(to right, rgba(0,0,255,0.43125),rgba(255,0,0,0.24375));}
        48%   {background:linear-gradient(to right, rgba(0,0,255,0.45), rgba(255,0,0,0.2625));}
        50%  {background:linear-gradient(to right, rgba(0,0,255,0.46875), rgba(255,0,0,0.28125));}
        52%  {background:linear-gradient(to right, rgba(0,0,255,0.4875), rgba(255,0,0,0.3));}
        54%   {background:linear-gradient(to right, rgba(0,0,255,0.50625), rgba(255,0,0,0.31875));}
        56%  {background:linear-gradient(to right, rgba(0,0,255,0.525), rgba(255,0,0,0.3375));}
        58%  {background:linear-gradient(to right, rgba(0,0,255,0.54375), rgba(255,0,0,0.35625));}
        60%  {background:linear-gradient(to right, rgba(0,0,255,0.5625), rgba(255,0,0,0.375));}
        62% {background:linear-gradient(to right, rgba(0,0,255,0.58125), rgba(255,0,0,0.39375));}
        64%  {background:linear-gradient(to right,rgba(0,0,255,0.6), rgba(255,0,0,0.4125));}
        66%   {background:linear-gradient(to right, rgba(0,0,255,0.61875), rgba(255,0,0,0.43125));}
        68%  {background:linear-gradient(to right, rgba(0,0,255,0.6375), rgba(255,0,0,0.45));}
        70%  {background:linear-gradient(to right, rgba(0,0,255,0.65625), rgba(255,0,0,0.46875));}
        72%  {background:linear-gradient(to right, rgba(0,0,255,0.675), rgba(255,0,0,0.4875));}
        74% {background:linear-gradient(to right, rgba(0,0,255,0.69375), rgba(255,0,0,0.50625));}
        76%  {background:linear-gradient(to right, rgba(0,0,255,0.7125), rgba(255,0,0,0.525));}
        78%   {background:linear-gradient(to right, rgba(0,0,255,0.73125),,rgba(255,0,0,0.54375));}
        80%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.5625));}
        82%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.58125));}
        84%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.6));}
        86% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.61875));}
        88%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.6375));}
        90%   {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.65625));}
        92%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.675));}
        94%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.69375));}
        96%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.7125));}
        98% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.73125),);}
        100%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));}
    }
    <div class="contener" style="">
      <div class="ed"></div>
    </div>

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