CSS3 background image transition

前端 未结 13 2193
梦如初夏
梦如初夏 2020-11-22 07:07

I\'m trying to make a \"fade-in fade-out\" effect using the CSS transition. But I can\'t get this to work with the background image...

The CSS:



        
相关标签:
13条回答
  • 2020-11-22 07:59

    I've figured out a solution that worked for me...

    If you have a list item (or div) containing only the link, and let's say this is for social links on your page to facebook, twitter, ect. and you're using a sprite image you can do this:

    <li id="facebook"><a href="facebook.com"></a></li>
    

    Make the "li"s background your button image

    #facebook {
       width:30px;
       height:30px;
       background:url(images/social) no-repeat 0px 0px;
    }
    

    Then make the link's background image the hover state of the button. Also add the opacity attribute to this and set it to 0.

    #facebook a {
       display:inline-block;
       background:url(images/social) no-repeat 0px -30px;
       opacity:0;
    }
    

    Now all you need is "opacity" under "a:hover" and set this to 1.

    #facebook a:hover {
       opacity:1;
    }
    

    Add the opacity transition attributes for each browser to "a" and "a:hover" so the the final css will look something like this:

    #facebook {
       width:30px;
       height:30px;
       background:url(images/social) no-repeat 0px 0px;
    }
    #facebook a {
       display:inline-block;
       background:url(images/social) no-repeat 0px -30px;
       opacity:0;
       -webkit-transition: opacity 200ms linear;
       -moz-transition: opacity 200ms linear;
       -o-transition: opacity 200ms linear;
       -ms-transition: opacity 200ms linear;
       transition: opacity 200ms linear;
    }
    #facebook a:hover {
       opacity:1;
       -webkit-transition: opacity 200ms linear;
       -moz-transition: opacity 200ms linear;
       -o-transition: opacity 200ms linear;
       -ms-transition: opacity 200ms linear;
       transition: opacity 200ms linear;
    }
    

    If I explained it correctly that should let you have a fading background image button, hope it helps at least!

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