html - 给背景大小 : cover effect via percentage value

标签 html css transition

如何才能达到与 background-size: cover; 相同的效果,但使用百分比值。 我试过了

background-size: auto 100%;

但这并不相似。现在你可能会问,为什么我不简单地使用 background-size: cover; 然后,这是因为我想为背景图像设置动画,而你不能从 设置动画覆盖 % 值。

非常感谢任何帮助。

最佳答案

百分比 + 自动在某些情况下有效,但并非全部。 cover自动宽度或高度设置为 100%,以便其他尺寸适合或溢出容器,同时保持纵横比。使用 percentage + auto 你需要弄清楚哪一侧适合自己:

div {
  display: inline-block;
  background-position: center center;
  background-repeat: no-repeat;
  border: 1px solid #CCC;
}
.box-port {
  width: 300px;
  height: 150px;
}
.box-land {
  width: 150px;
  height: 300px;
}
.img-port {
  background-image: url(http://dummyimage.com/400x200/CCC/000);
}
.img-land {
  background-image: url(http://dummyimage.com/200x400/CCC/000);
}
.size-full-width {
  background-size: 100% auto;
}
.size-full-height {
  background-size: auto 100%;
}
<p>Using <code>background-size: auto 100%</code> for:</p>
<div class="img-port box-port size-full-height">portraint inside portrait (pass)</div>
<div class="img-land box-port size-full-height">landscape inside portrait (fail)</div>

<p>Using <code>background-size: 100% auto</code> for:</p>
<div class="img-port box-land size-full-width">portraint inside landscape (fail)</div>
<div class="img-land box-land size-full-width">landscape inside landscape (pass)</div>

我能想到的唯一解决方案是比较容器与背景图像的纵横比,然后使用 background-size: 100% autobackground-size : 自动 100%

关于html - 给背景大小 : cover effect via percentage value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401023/

相关文章:

css - 打印时确保 CSS 精确大小

javascript - knockout 双向左侧和顶部绑定(bind)

javascript - 每第二次点击 JQuery FadeIn

javascript - 用javascript中的按钮显示所有投资组合元素

javascript - 使用 jquery 将另一个 div 悬停时移动 div

javascript - 创建对象并将其附加到动态创建的对象

java - 如何在 Android 应用程序中一次更改所有 Activity 转换?

html - 价格行为的滚动过渡动画。如何?

javascript - 在不反转顶部的情况下反转 HTML 表格的行

html - 从 SVG 三 Angular 形剪辑平滑过渡到圆形