Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css3 background-size cover to percentage animation zoom

Im trying to make a zoom effect with background-size. The issue I have is I need to animate from background-size:cover to something like background-size: 105%.

When I try this it with a "transition: 0.1s background-size linear" it animates from 0 to 104%.

Is there anyway I can animate from cover to that percentage with out going back to 0.

(im using cover because I don't know the size of the image but I have a fixed size div to display it in.)

Thanks Pete

like image 272
Peter J Harrison Avatar asked Jul 05 '13 16:07

Peter J Harrison


People also ask

How do I resize my background image to fit CSS?

The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image. By doing so, you can scale the image upward or downward as desired.

What is CSS background size?

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.

What is cover background size?

Pretty sure background-size: cover; means the image will fill the element while maintaining its aspect ratio, while background-size: 100%; will just make the image fill 100% width of the element.

What is the difference between background size cover and contain?

cover tells the browser to make sure the image always covers the entire container, even if it has to stretch the image or cut a little bit off one of the edges. contain , on the other hand, says to always show the whole image, even if that leaves a little space to the sides or bottom.


1 Answers

One posibility is to have the background set in a pseudo element, and then do the zoom in the base element. (thru transform property, for instance)

.test { 
    width: 300px;
    height: 300px;
    position: relative;
    left: 30px;
    top: 30px;
    transition: all 1s;
}

.test:hover {
    -webkit-transform: scale(1.05, 1.05);
    transform: scale(1.05, 1.05);
}

.test:after {
    content: '';
    position: absolute;
    left: 0px;
    top: 0px;
    right: 0px;
    bottom: 0px;
    background: url("http://lorempixel.com/600/400");
    background-size: cover;
}
<div class="test">
  </div>

Demo

like image 65
vals Avatar answered Nov 14 '22 03:11

vals