CSS3 background image transition

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

    Try this, will make the background animated worked on web but hybrid mobile app not working

    @-webkit-keyframes breath {
     0%   {  background-size: 110% auto; }
     50%  {  background-size: 140% auto; }
     100% {  background-size: 110% auto; }      
    }
    body {
       -webkit-animation: breath 15s linear infinite;
       background-image: url(images/login.png);
        background-size: cover;
    }
    
    0 讨论(0)
  • 2020-11-22 07:50

    You can transition background-image. Use the CSS below on the img element:

    -webkit-transition: background-image 0.2s ease-in-out;
    transition: background-image 0.2s ease-in-out;
    

    This is supported natively by Chrome, Opera and Safari. Firefox hasn't implemented it yet (bugzil.la). Not sure about IE.

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

    The solution (that I found by myself) is a ninja trick, I can offer you two ways:

    first you need to make a "container" for the <img>, it will contain normal and hover states at the same time:

    <div class="images-container">
        <img src="http://lorempixel.com/400/200/animals/9/">
        <img src="http://lorempixel.com/400/200/animals/10/">
    </div>
    
    • with CSS3 selectors http://jsfiddle.net/eD2zL/1/ (if you use this one, "normal" state will be first child your container, or change the nth-child() order)

    • CSS2 solution http://jsfiddle.net/eD2zL/2/ (differences between are just a few selectors)

    Basically, you need to hide "normal" state and show their "hover" when you hover it

    and that's it, I hope somebody find it useful.

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

    You can use pseudo element to get the effect you want like I did in that Fiddle.

    CSS:

    .title a {
        display: block;
        width: 340px;
        height: 338px;
        color: black;
        position: relative;
    }
    .title a:after {
        background: url(https://lh3.googleusercontent.com/-p1nr1fkWKUo/T0zUp5CLO3I/AAAAAAAAAWg/jDiQ0cUBuKA/s800/red-pattern.png) repeat;
        content: "";
        opacity: 0;
        width: inherit;
        height: inherit;
        position: absolute;
        top: 0;
        left: 0;
        /* TRANSISITION */
        transition: opacity 1s ease-in-out;
        -webkit-transition: opacity 1s ease-in-out;
        -moz-transition: opacity 1s ease-in-out;
        -o-transition: opacity 1s ease-in-out;
    }
    .title a:hover:after{   
        opacity: 1;
    }
    

    HTML:

    <div class="title">
        <a href="#">HYPERLINK</a>
    </div>
    
    0 讨论(0)
  • 2020-11-22 07:53

    Considering background-images can't be animated, I created a little SCSS mixin allowing to transition between 2 different background-images using pseudo selectors before and after. They are at different z-index layers. The one that is ahead starts with opacity 0 and becomes visible with hover.

    You can use it the same approach for creating animations with linear-gradients too.

    scss

    @mixin bkg-img-transition( $bkg1, $bkg2, $transTime:0.5s ){  
      position: relative;  
      z-index: 100; 
      &:before, &:after {
        background-size: cover;  
        content: '';    
        display: block;
        height: 100%;
        position: absolute;
        top: 0; left: 0;    
        width: 100%;    
        transition: opacity $transTime;
      }
      &:before {    
        z-index: -101;
        background-image: url("#{$bkg1}");    
      }
      &:after {    
        z-index: -100;
        opacity: 0;
        background-image: url("#{$bkg2}");    
      }
      &:hover {
         &:after{
           opacity: 1; 
         }
      }  
    }
    

    Now you can simply use it with

    @include bkg-img-transition("https://picsum.photos/300/300/?random","https://picsum.photos/g/300/300");
    

    You can check it out here: https://jsfiddle.net/pablosgpacheco/01rmg0qL/

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

    If animating opacity is not an option, you can also animate background-size.

    For example, I used this CSS to set a backgound-image with a delay.

    .before {
      background-size: 0;
    }
    
    .after {
      transition: background 0.1s step-end;
      background-image: $path-to-image;
      background-size: 20px 20px;
    }
    
    0 讨论(0)
提交回复
热议问题