CSS3 background image transition

前端 未结 13 2191
梦如初夏
梦如初夏 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:34

    I was struggling with this for a bit, I first used a stack of images on top of each other and every three seconds, I was trying to animate to the next image in the stack and throwing the current image to the bottom of the stack. At the same time I was using animations as shown above. I couldn't get it to work for the life of me.

    You can use this library which allows for **dynamically-resized, slideshow-capable background image ** using jquery-backstretch.

    https://github.com/jquery-backstretch/jquery-backstretch

    0 讨论(0)
  • 2020-11-22 07:35

    With Chris's inspiring post here:

    https://css-tricks.com/different-transitions-for-hover-on-hover-off/

    I managed to come up with this:

    #banner
    {
        display:block;
        width:100%;
        background-repeat:no-repeat;
        background-position:center bottom;
        background-image:url(../images/image1.jpg);
        /* HOVER OFF */
        @include transition(background-image 0.5s ease-in-out); 
    
        &:hover
        {
            background-image:url(../images/image2.jpg);
            /* HOVER ON */
            @include transition(background-image 0.5s ease-in-out); 
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:38

    Salam, this answer works only in Chrome, cause IE and FF support color transition.

    There is no need to make your HTML elements opacity:0, cause some times they contain text, and no need to double your elements!.

    The question with link to an example in jsfiddle needed a small change, that is to put an empty image in .title a like background:url(link to an empty image); same as you put it in .title a:hover but make it empty image, and the code will work.

    .title a {
    display: block;
    width: 340px;
    height: 338px;
    color: black;
    background: url(https://upload.wikimedia.org/wikipedia/commons/5/59/Empty.png) repeat;
    /* TRANSISITION */
    transition: background 1s;
    -webkit-transition: background 1s;
    -moz-transition: background 1s;
    -o-transition: background 1s;
      }
      .title a:hover{   background: transparent;
       background: url(https://lh3.googleusercontent.com/-p1nr1fkWKUo/T0zUp5CLO3I/AAAAAAAAAWg/jDiQ0cUBuKA/s800/red-pattern.png) repeat;
    /* TRANSISITION */
    transition: background 1s;
    -webkit-transition: background 1s;
    -moz-transition: background 1s;
    -o-transition: background 1s;
    }
    

    Check this out https://jsfiddle.net/Tobasi/vv8q9hum/

    0 讨论(0)
  • 2020-11-22 07:41

    This can be achieved with greater cross-browser support than the accepted answer by using pseudo-elements as exemplified by this answer: https://stackoverflow.com/a/19818268/2602816

    0 讨论(0)
  • Unfortunately you can't use transition on background-image, see the w3c list of animatable properties.

    You may want to do some tricks with background-position.

    0 讨论(0)
  • 2020-11-22 07:48

    If you can use jQuery, you can try BgSwitcher plugin to switch the background-image with effects, it's very easy to use.

    For example :

    $('.bgSwitch').bgswitcher({
            images: ["style/img/bg0.jpg","style/img/bg1.jpg","style/img/bg2.jpg"],
            effect: "fade",
            interval: 10000
    });
    

    And add your own effect, see adding an effect types

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