【问题标题】:Background-size transition not work in Chrome背景尺寸转换在 Chrome 中不起作用
【发布时间】:2016-10-19 03:40:35
【问题描述】:

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

  • Chromebackground-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>

有人帮我解决这个问题吗?或者可以确定正在破坏这些过渡?

谢谢

【问题讨论】:

  • 我怀疑这是因为 bg 图像不在 CSS 中……而是内联样式。
  • 当然是:) 你可以在firefox中查看sn-p。
  • @Paulie_D 我也尝试在 css 中使用 background-size,但在 Chrome 中不起作用,无论如何我需要在代码中使用 background-img
  • 你不能为auto制作动画(至少你应该不能)...你必须使用一个值。
  • 你可以使用transform:scale(),在chrome中效果更好

标签: css css-transitions transition background-size


【解决方案1】:

可以使用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】:

    这是另一个在 Chrome 和 Firefox 上都能正常工作的解决方案:

    HTML 代码:

    <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】:

      这不起作用的原因是background-size 在使用诸如covercontainauto 之类的关键字时不可动画化(或至少不应该如此)。

      MDN 进一步解释:

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

      因此,将原始/最终值调整为实际数字(或更恰当地称为长度),您将解决这个问题。

      【讨论】:

      • 你是对的 :) 我前段时间有这个工作,也许最新的 Chrome 更新改变了他们的行为
      【解决方案4】:
      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/

      【讨论】:

      • 始终欢迎提供指向潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接是Why and how are some answers deleted? 的一个可能原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 2016-10-15
      • 2021-08-14
      • 2013-03-23
      相关资源
      最近更新 更多