将变换:缩放应用于背景图像

时间:2017-11-10 04:38:31

标签: html css css-transforms

我正在尝试使用background:scale在div的背景图像上实现优雅的放大效果:cover。

我面临的问题是,当背景图像放大时,比例似乎会放大div。

有没有办法只将效果应用到背景图像?或者我做错了什么?



.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">
  &nbsp;
</div>
<!-- despre-noi-image -->
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

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%;
}

答案 1 :(得分:0)

&#13;
&#13;
/* 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);
    }

}
&#13;
<div class="container">
  <div class="despre-noi-image">
  &nbsp;
  </div><!-- despre-noi-image -->

</div>
&#13;
&#13;
&#13;

这是你想要的吗?

您只需使用overflow: hidden

答案 2 :(得分:0)

background-size:cover 不适合你。

.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%; 
    }

}

更新代码