背景大小转换在Chrome中不起作用

时间:2016-06-17 10:26:58

标签: css css3 css-transitions transition background-size

我正在尝试转换background-sizebackground-color

  • Chrome background-size无法正常转换
  • Firefox :两者都运行良好

我还创建了一个fiddle

.highlight {
  display: block;
  position: relative;
  /*min-height: 800px;*/
  min-height: 200px;
  background-position: center center;
  background-repeat: no-repeat;
  /*padding-top: 200px;*/
  padding-top: 80px;
  /*background-size: cover;*/
}
.highlight:before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .25);
  content: "";
}
.highlight {
  position: relative;
  height: 200px;
  cursor: pointer;
  background-size: auto 110%;
  background-position: center center;
  -moz-transition: background-size 3s ease;
  -webkit-transition: background-size 3s ease;
  transition: background-size 3s ease;
}
.highlight:hover {
  background-size: auto 130%;
  background-color: rgba(0, 0, 0, 0.4);
  -moz-transition: background-size 3s ease;
  -webkit-transition: background-size 3s ease;
  transition: background-size 3s ease;
}
.highlight:before {
  content: ' ';
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  -moz-transition: background-color 3s ease;
  -webkit-transition: background-color 3s ease;
  transition: background-color 3s ease;
}
.highlight:hover:before {
  background-color: rgba(0, 0, 0, 0.8);
  -moz-transition: background-color 3s ease;
  -webkit-transition: background-color 3s ease;
  transition: background-color 3s ease;
}
<div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
</div>

有人帮我这个吗?或者可以认为这是在促成这些过渡?

由于

4 个答案:

答案 0 :(得分:5)

这不起作用的原因是background-size在使用covercontain或{等关键字时无法制作动画(或至少不应该是) {1}}。

MDN进一步解释:

  

动画:是,作为可重复列表,一个简单的列表,长度,百分比或calc();当两个值都是长度时,它们被插值为长度;当两个值都是百分比时,它们被插值为百分比;否则,两个值都被转换为calc()函数,该函数是长度和百分比之和(每个可能为零),并且这些calc()函数将每一半内插为实数。 这表示关键字值不可动画

因此,将原始/最终值调整为实际数字(或更准确地引用的长度),然后您将解决此问题。

答案 1 :(得分:2)

您可以使用transform: scale(1.3)transition: all来实现背景尺寸转换效果。

更新了fiddle

注意:我还添加了包装器来隐藏溢出。

.container {
  overflow: hidden;
}
.highlight {
  display: block;
  position: relative;
  /*min-height: 800px;*/
  min-height: 200px;
  background-position: center center;
  background-repeat: no-repeat;
  /*padding-top: 200px;*/
  padding-top: 80px;
  /*background-size: cover;*/
}
.highlight:before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .25);
  content: "";
}
.highlight {
  position: relative;
  height: 200px;
  cursor: pointer;
  background-size: auto 110%;
  background-position: center center;
  -moz-transition: all 3s ease;
  -webkit-transition: all 3s ease;
  transition: all 3s ease;
}
.highlight:hover {
  transform: scale(1.3);
  background-color: rgba(0, 0, 0, 0.4);
  -moz-transition: all 3s ease;
  -webkit-transition: all 3s ease;
  transition: all 3s ease;
}
.highlight:before {
  content: ' ';
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  -moz-transition: background-color 3s ease;
  -webkit-transition: background-color 3s ease;
  transition: background-color 3s ease;
}
.highlight:hover:before {
  background-color: rgba(0, 0, 0, 0.8);
  -moz-transition: background-color 3s ease;
  -webkit-transition: background-color 3s ease;
  transition: background-color 3s ease;
}
<div class="container">
  <div class="highlight" style="background-image:url(http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg);">
  </div>
</div>

答案 2 :(得分:1)

这是另一种解决方案,适用于Chrome和&amp;火狐:

HTML code:

<div class="highlight">
    <div class="filter">
    </div>
    <img src="http://cdn2.stillgalaxy.com/ori/2015/06/06/female-doctors-stock-photos-2331433563559.jpg" alt="image" />
</div>

<强>的CSS:

.highlight {
  display: block;
  position: relative;
  text-align: center;
  overflow: hidden;
}
.highlight .filter {
    background-color:rgba(0,0,0,0.4);
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 2;
    transition: background-color 3s ease;
}
.highlight:hover > img{
  transform: scale(1.3);
}
.highlight:hover > .filter {
    background-color:rgba(0,0,0,0.8);
}


.highlight img {
  height: 280px;
  transition: transform 3s ease;
}

答案 3 :(得分:0)

In background-size you have to give width otherwise it wont work. 
Check this fiddle i have modified your code : 

https://jsfiddle.net/shriharim/26Lq1oxx/