【问题标题】:CSS transform works on Firefox but not on ChromeCSS 转换适用于 Firefox,但不适用于 Chrome
【发布时间】:2021-08-08 22:13:42
【问题描述】:

我正在 CSS 中尝试一个东西,其中文本的背景是图像,当我们将鼠标悬停在文本上时,它会缩放到一个非常大的数字,用作字体颜色的图像完全显示出来。它在 Mozilla Firefox 上完美运行,但 CSS 的 transform 属性在 Chrome 上似乎被破坏了,文本在 Chrome 上悬停时消失了。

/* font */

@import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap');

/* reset */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Rubik', sans-serif;
}

body,
html {
  min-height: 100vh;
  width: 100vw;
  overflow-x: hidden;
  background: #999;
}


/* styling */

.container {
  background: url("https://picsum.photos/900/800");
  background-repeat: no-repeat;
  background-size: cover;
  color: transparent;
  -webkit-background-clip: text;
  background-clip: text;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.container .image-text {
  font-size: 5em;
  font-weight: normal;
  text-align: center;
  transition: transform 2s linear;
}

.container .image-text:hover {
  transform: scale(200);
}
<div class="container">
  <div class="image-text">
    Hover
  </div>
</div>

【问题讨论】:

标签: html css google-chrome css-transforms


【解决方案1】:

问题是任何类型的转换都会创建一个新的堆叠上下文,因此剪辑文本无效。 [这是 CSS 属性 scale 为您所做的事情之一 - 将其从转换中移除 - 但 Chrome/Edge 不支持它]。

如果我们取消比例变换,而是增加字体大小和过渡,你会得到我认为你想要的效果:

/* font */

@import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap');

/* reset */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Rubik', sans-serif;
}

body,
html {
  min-height: 100vh;
  width: 100vw;
  overflow-x: hidden;
  background: #999;
}


/* styling */

.container {
  background: url("https://picsum.photos/900/800");
  background-repeat: no-repeat;
  background-size: cover;
  color: transparent;
  -webkit-background-clip: text;
  background-clip: text;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.container .image-text {
  font-size: 5em;
  font-weight: normal;
  text-align: center;
  transition: font-size 2s linear;
}

.container .image-text:hover {
  font-size: 200em;
}
<div class="container">
  <div class="image-text">
    Hover
  </div>
</div>

【讨论】:

  • 谢谢!这似乎在任何地方都可以正常工作,但我正在考虑缩放,因为我可能遇到container 是带有图像和文本的复杂卡片的情况,并且我在它的确切中心插入了一个文本。因此,当我们将鼠标悬停在卡片上时,卡片及其所有内容都会按比例放大,以保持中心文本作为比例点。
  • 了解,仅更改字体大小是一种特殊的、有限的情况。如果 Chrome 等实现单独的 scale 属性会很好。
  • 是的。我注意到关于字体大小的另一件事,在某个点之后它停止增长。我的字体大小增加到一百万 em(没有理智的人会这样做),但图像的某些部分仍然保留。
【解决方案2】:

Chrome 似乎无法正确呈现它。确实如此,当您使用zoom 时,您只会失去过渡...

@import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap');


.container {
  font-family: 'Rubik', sans-serif;
  background: url("https://picsum.photos/900/800");
  background-repeat: no-repeat;
  background-size: cover;
  color: transparent;
  -webkit-background-clip: text;
  background-clip: text;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.container .image-text {
  font-size: 5em;
  font-weight: normal;
  text-align: center;
  transition: transform 2s linear;
}

.container .image-text:hover {
  -moz-transform: scale(200);
  zoom: 200;
}
<div class="container">
  <div class="image-text">
    Hover
  </div>
</div>

【讨论】:

  • zoom 属性在 CSS 中是否不可动画化?我提到了MDN,其中有zoom
  • @deekeh 嗯,很有趣。我认为它是非标准的,以及 Chrome 的行为。我无法访问 Microsoft 浏览器,我认为 zoom 的来源。
  • 我尝试了transition: zoom 1s;,但没有成功。好像坏了。
猜你喜欢
  • 2018-05-05
  • 2015-02-20
  • 1970-01-01
  • 2016-05-01
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多