Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

html - Applying transform:scale to just the background image

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">
  &nbsp;
</div>
<!-- despre-noi-image -->
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...