简体   繁体   English

CSS背景图片动画

[英]css background image animation

Is there a way to use transform: scale() on only the background image? 有没有一种方法可以只在背景图像上使用transform: scale()

What I want to do is on hover make the background scale up 我想做的是将hover使背景放大

html HTML

<div class="square">
    //Content in here
</div>

css CSS

.square{
background: url('image goes here');
background-size: cover;
height: 50vw;
width: 50vw;

so I don't want the content to scale only the background image on hover 所以我不希望内容仅在悬停时缩放背景图像

If anyone knows a way I can do this would be great 如果有人知道我可以做到的话,那就太好了

Thanks 谢谢

If it's just the background-size and you're not relying on "cover" or "contain" for the initial value, then changing the background-size on hover should be enough. 如果只是背景大小,并且您不依赖于“ cover”或“ contain”作为初始值,则在悬停时更改背景大小就足够了。

If not, stacking a pseudo-element with the background while keeping your element background transparent, then using the transforms on the pseudo-element will do the trick 如果不是这样,则在保持元素背景透明的同时,将伪元素与背景堆叠在一起,然后对伪元素使用转换即可

edit: Adding example :) 编辑:添加示例:)

<style>
.container{
  overflow:hidden;
  position:relative;  
}

.scalable-bg::before{
  content:"";
  background:url('http://lorempixel.com/600/400');
  background-size:cover;
  position:absolute; top:0; left:0;
  width:100%; height:100%;
  z-index:-1;
  transition:all 1s ease;
}

.scalable-bg{
  padding:2em;
  color:white; text-shadow: 2px 2px 0 black;
}

.scalable-bg:hover::before {
  transform: scale(2);
}
</style>
<div class="container">
  <div class="scalable-bg">
    <h1>
      Something
    </h1>
    <p>
    Something, something, something, darkside. Something, something, complete
    </p>
  </div>
</div>

https://jsfiddle.net/2kzk1acr/3/ https://jsfiddle.net/2kzk1acr/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM