background-transition with background-size: cover

前端 未结 2 907
深忆病人
深忆病人 2020-12-19 02:07

This may already be answered somewhere but I haven\'t found yet after a bit of searching.

I have a series of divs with background-images. The size is set to backgrou

2条回答
  •  感动是毒
    2020-12-19 02:32

    You can't use keywords (such as cover) when using CSS animations for background-size.

    More info here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties

    Relevant text:

    background-size - yes, as a repeatable list of a simple list of a length, percentage or calc(); when both values are lengths, they are interpolated as lengths; when both values are percentages, they are interpolated as percentages; otherwise, both values are converted into a calc() function that is the sum of a length and a percentage (each possibly zero), and these calc() functions have each half interpolated as real numbers. . This means keyword values are not animatable.

    One approach to get this effect is to place element with the background image in a wrapping element with overflow hidden and apply a scale transform.

    .wrapper { 
      width:300px;
      height:400px;
      overflow:hidden;
    }
    .image {
      background:url("http://placekitten.com/g/500/500");
      background-size:cover;
      width:100%;
      height:100%;
      transition: transform 2s;
    }
    
    .image:hover { transform:scale(1.1) }

提交回复
热议问题