问题
I am trying to achieve a classy zoom in effect on a div's background image with background-size:cover using transform:scale.
The problem I am facing is that the scale seems to enlarge the div as the background-image zooms in.
Is there a way to only apply the effect to the background image? Or am I doing something wrong?
.despre-noi-image {
width: 40%;
height: 500px;
background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
text-align: left;
background-position-x: 50%;
background-position-y: 0%;
-webkit-animation: zoomin 10s linear;
animation: zoomin 10s linear;
animation-fill-mode: forwards;
}
@-webkit-keyframes zoomin {
0% {
-webkit-transform: scale(1);
transform: scale(1);
}
100% {
-webkit-transform: scale(2);
transform: scale(2);
}
}
<div class="despre-noi-image">
</div>
<!-- despre-noi-image -->
回答1:
CSS is very simple. You can change background size with a transition on hover instead of scaling.
.despre-noi-image {
width: 40%;
height: 500px;
background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
transition: all .7s;
}
.despre-noi-image:hover {
transition: all 1s;
background-size: 110%;
}
回答2:
background-size:cover
won't work for you.
.despre-noi-image{
width:100%;
height: 500px;
background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg') no-repeat center center;
background-size: 100%;
text-align: left;
background-position-x: 50%;
background-position-y: 50%;
-webkit-animation: zoomin 10s linear;
animation: zoomin 5s linear;
animation-fill-mode: forwards;
background-origin:center;
}
@-webkit-keyframes zoomin {
0% {
background-size: 100%;
}
100% {
background-size: 150%;
}
}
Updated Code
回答3:
/* Add new element */
.container{
width: 40%;
height: 500px;
overflow: hidden;
}
.despre-noi-image{
width:100%; /*change */
height: 500px;
background: url('https://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2017/04/18/16/wine-evening.jpg') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
text-align: left;
background-position-x: 50%;
background-position-y: 0%;
-webkit-animation: zoomin 10s linear;
animation: zoomin 10s linear;
animation-fill-mode: forwards;
}
@-webkit-keyframes zoomin {
0% {
-webkit-transform: scale(1);
transform: scale(1);
}
100% {
-webkit-transform: scale(2);
transform: scale(2);
}
}
<div class="container">
<div class="despre-noi-image">
</div><!-- despre-noi-image -->
</div>
Is this what you want?
You just use overflow: hidden
来源:https://stackoverflow.com/questions/47215762/applying-transformscale-to-just-the-background-image