Animating a CSS gradient background on the hover event

前端 未结 3 1950
南旧
南旧 2021-01-07 06:08

I have some menu items that are styled using a background gradient on hover using the following styling:

#sidebar ul          


        
相关标签:
3条回答
  • 2021-01-07 06:17

    Animation on gradients aren't supported yet. However this site provides a pleasing approach for a animated kind of feel on hover-

    http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/

    Sample css:-

    #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;  
    } 
    
    0 讨论(0)
  • 2021-01-07 06:22

    Try this as a hack:

    <div class="background-animate">
      <div class="content">Hi im content</div>
    </div>
    

    And style it

    .background-animate {
        position: relative;
        z-index: 10;
        display: block;
        background: transparent;
    }
    .background-animate:before {
        content: "";
        position: absolute;
        transition: opacity .35s ease-in-out;
       -moz-transition: opacity .35s ease-in-out;
       -webkit-transition: opacity .35s ease-in-out;
        top:0; left: 0; right: 0; bottom: 0; z-index:-1;
        background: -moz-radial-gradient(center, ellipse cover, #ffffff 40%, #e9eae9 100%); /* FF3.6-15 */
        background: -webkit-radial-gradient(center, ellipse cover, #ffffff 40%,#e9eae9 100%); /* Chrome10-25,Safari5.1-6 */
        background: radial-gradient(ellipse at center, #ffffff 40%,#e9eae9 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    }
    .background-animate:hover:before {
        opacity: 0;
    }
    .background-animate:after {
        content: ""; opacity: 0;
        transition: opacity .35s ease-in-out;
       -moz-transition: opacity .35s ease-in-out;
       -webkit-transition: opacity .35s ease-in-out;
        position: absolute;
        top:0; left: 0; right: 0; bottom: 0; z-index:-1;
        background: -moz-radial-gradient(center, ellipse cover, #ffffff 80%, #e9eae9 100%); /* FF3.6-15 */
        background: -webkit-radial-gradient(center, ellipse cover, #ffffff 80%,#e9eae9 100%); /* Chrome10-25,Safari5.1-6 */
        background: radial-gradient(ellipse at center, #ffffff 80%,#e9eae9 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    }
    .background-animate:hover:after {
        opacity: 1;
    }
    

    It basically does an opacity switch between to gradients. Demo found here https://codepen.io/anon/pen/eWOEoR

    0 讨论(0)
  • 2021-01-07 06:30

    It seems gradients don't support transitions yet (still):

    Use CSS3 transitions with gradient backgrounds

    If you use a background image rather than a css3 gradient, then you can use css transition to animate it in and out.

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