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

时间:2016-09-20 21:01:13

标签: html css background-size

当用户悬停带有图像背景的链接(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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:6)

如果background-size属性设置为cover,则无法顺利为其设置动画。但你可以通过动画transform来伪造它。由于您只想缩放图像而不是内容,因此您需要一个专门用于显示图像的子元素(在下面的示例中使用伪元素完成)。

&#13;
&#13;
.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;
}
&#13;
<div class="wrap">
  <div class="content">Content</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这个代码实际上是用于缩放效果..这取决于你设置缩放级别。

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

答案 2 :(得分:0)

要为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>