背景大小:悬停时覆盖缩放过渡

11

我希望当用户悬停在具有图像背景的链接(div)上时,能够产生缩放效果。

我不知道如何仅对背景而非文本进行缩放!我找不到最好的方法。

这里是一段代码(与我的期望不符):

.light_blue {
  display: block;
  color: #fff;
  background-repeat: no-repeat;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  background-position: center;
  width: 100%;
}
a.light_blue:hover {
  text-decoration: none;
  color: #fcb428;
}
div.wrap {
  height: 33%;
  width: 33%;
  overflow: hidden;
  position: relative;
}
.wrap {
  -moz-transition: all .5s;
  -webkit-transition: all .8s;
  transition: all .8s;
  background-position: center center;
}
.wrap:hover {
  -webkit-background-size: 120%;
  -moz-background-size: 120%;
  -o-background-size: 120%;
  background-size: 120%;
}
.bg_flat {
  position: absolute;
  font-family: "Archivo Narrow";
  background: url(http://hokejfan.pl/hokej2015/kluby/img/black_bg4.png);
  background-size: contain;
  bottom: 0px;
  padding: 5px;
}
.bg_naglowek {
  text-transform: uppercase;
  font-size: 29px;
}
.bg_flat2 {
  position: absolute;
  background: url(../img/black_bg4.png);
  background-size: contain;
  font-family: "Archivo Narrow";
  bottom: 5px;
  width: -webkit-calc(100% - 10px);
  width: calc(100% - 10px);
  padding: 15px;
}
<div class="col-sm-4" style="padding: 5px;">
  <a href="#news" class="light_blue wrap" style="background-image: url('http://hokejfan.pl/hokej2015/kluby/static/images2/48b51db749e3d9d992a61d289bc3c535.jpg'); height: 180px;">
    <div class="bg_flat2">
      <b class="bg_naglowek">Hello World</b>
    </div>
  </a>
</div>

3个回答

24

如果将 background-size 属性设置为 cover,则无法平滑地进行动画。但是,您可以通过代替变换 transform 来模拟它。由于您仅希望缩放图像而不是内容,因此您需要一个专用于显示图像的子元素(在下面的示例中使用伪元素完成)。

.wrap {
  width: 33%;
  padding-bottom: 20%;
  overflow: hidden;
  position: relative;
  transition: all .8s;
  background: url(http://hokejfan.pl/hokej2015/kluby/static/images2/48b51db749e3d9d992a61d289bc3c535.jpg);
  background-position: center center;
  background-size: cover;
}

.wrap::before { 
  content:"";
  position:absolute; top:0;right:0;bottom:0;left:0;
  background:inherit;
  transition:inherit;
}
.wrap:hover::before { 
  transform: scale(1.2);
}

.content {
  position: relative;
}
<div class="wrap">
  <div class="content">Content</div>
</div>


1
我真的不明白这是如何工作的...但它确实能够工作。因为'before'比'.wrap'更大,背景会被缩放? - Calvin Bramlett
2
@CalvinBramlett 没错。请注意,background:inherit 规则应用于 ::before 元素,因此它具有与 .wrap 相同的背景,并通过 transform: scale(1.2); 在悬停时进行缩放。 - Stephen M. Harris

2
要为background-size属性添加动画效果,你需要在正常状态和悬停状态下设置该属性。然后transition就会生效。

.light_blue {
  display: block;
  color: #fff;
  background-repeat: no-repeat;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  background-position: center;
  width: 100%;
}
a.light_blue:hover {
  text-decoration: none;
  color: #fcb428;
}
div.wrap {
  height: 33%;
  width: 33%;
  overflow: hidden;
  position: relative;
}
.wrap {
  -moz-transition: all .5s;
  -webkit-transition: all .8s;
  transition: all .8s;
  background-position: center center;
  background-size: 100%;
}
.wrap:hover {
  -webkit-background-size: 120%;
  -moz-background-size: 120%;
  -o-background-size: 120%;
  background-size: 120%;
}
.bg_flat {
  position: absolute;
  font-family: "Archivo Narrow";
  background: url(http://hokejfan.pl/hokej2015/kluby/img/black_bg4.png);
  background-size: contain;
  bottom: 0px;
  padding: 5px;
}
.bg_naglowek {
  text-transform: uppercase;
  font-size: 29px;
}
.bg_flat2 {
  position: absolute;
  background: url(../img/black_bg4.png);
  background-size: contain;
  font-family: "Archivo Narrow";
  bottom: 5px;
  width: -webkit-calc(100% - 10px);
  width: calc(100% - 10px);
  padding: 15px;
}
<div class="col-sm-4" style="padding: 5px;">
  <a href="#news" class="light_blue wrap" style="background-image: url('http://hokejfan.pl/hokej2015/kluby/static/images2/48b51db749e3d9d992a61d289bc3c535.jpg'); height: 180px;">
    <div class="bg_flat2">
      <b class="bg_naglowek">Hello World</b>
    </div>
  </a>
</div>


看起来不错,但我会使用不同尺寸的图片,如果我使用100%的尺寸而不是覆盖可能会不自然。 - Mariusz Zwierzychowski

0

这段代码实际上用于实现缩放效果...它取决于您设置的比例级别。

wrap:hover {
   -webkit-transform: scale(1.2);
  -ms-transform: scale(1.2);
  transform: scale(1.2);
}

当然可以,但它也会缩放我的文本“Hello World”,而我不想要。只有背景,有什么办法可以做到吗 :)? - Mariusz Zwierzychowski
哪个 div?看起来你把所有的 div 都放在了锚点标签 <a> 内。 - Baezid Mostafa
是的,我想让所有的(背景图片和文本)都可以点击。也许还有其他的方法? - Mariusz Zwierzychowski
好的,所以您只想让背景缩放而不是文本。让我试试...但也许我需要改变结构。 - Baezid Mostafa
你想让所有东西都可以点击。但只有图片会变大,而不是文字。我尝试了一些方法,但问题是当你悬停在文字上时,它不会给你背景图片的缩放效果。并且所有东西都可以点击。图片将是你的列大小。 - Baezid Mostafa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接