google-chrome - SVG 掩码最初未在 Chrome 中加载
问题描述
处理在主页英雄组件上使用 SVG 蒙版的项目,但由于某种原因,蒙版中的文本仅在我调整窗口大小时显示。它没有显示在初始页面加载中。这仅在 Chrome(最新版本)中发生。
您可以在此处查看有关 STAGING 环境的示例:https ://staging.erwconstructions.com.au/
HTML标记是:
<section data-component="mask-hero" class="in-view mask-hero">
<picture>
<img src="https://staging.erwconstructions.com.au/wp-content/uploads/2020/07/home-hero.jpg" alt="Home">
<svg width="100%" height="100%" viewBox="0 0 100% 100%">
<defs>
<linearGradient id="gradient">
<stop offset="100%" stop-color="black" />
</linearGradient>
<mask id="mask">
<rect width="100%" height="100%" fill="#fff" />
<text x="0" y="50%">
<tspan x="5vw" dy="-.1em">Building</tspan>
<tspan x="5vw" dy="1em">Redefined</tspan>
</text>
</mask>
</defs>
<rect width="100%" height="100%" fill="url(#gradient)" fill-opacity="0.9" mask="url(#mask)" />
</svg>
</picture>
</section>
CSS是:
[data-component="mask-hero"] {
height: 100vh;
padding: 0;
background: none;
& picture {
display: block;
height: 100vh;
& img {
height: 100%;
object-fit: cover;
object-position: center;
position: fixed;
will-change: transform;
width: 100%;
z-index: -1;
}
& svg {
display: block;
height: 100%;
}
& text {
font-family: var(--defaultFont);
font-weight: var(--bold);
font-size: 3em;
@media (--app-breakpoint-2) {
font-size: 6em;
}
@media (--app-breakpoint-4) {
font-size: 8em;
}
@media (--app-breakpoint-5) {
font-size: 14em;
}
}
}
}
出于某种原因,即使在 SVG 上设置了正确的属性,也存在有关 viewBox 的控制台错误:
Error: <svg> attribute viewBox: Expected number, "0 0 100% 100%".
任何帮助将不胜感激。
谢谢,戴恩
解决方案
根据给定的错误,我猜问题出在viewBox
. 它应该以 px 为单位给出一个绝对数字,而不是百分比。喜欢<svg viewBox="0 0 100 100">
推荐阅读
- angular - 即使发现错误,Observable 也会停止触发
- python - Scrapy.Spider:覆盖的 start_requests 中的回调不会跳转到另一个自定义函数
- php - 为什么我的数据上传不起作用并且没有丢弃任何错误?
- python - 运行 SQL Alchemy 迁移命令时,两个 Python Flask App 共享相同的数据库删除表问题
- c++ - 构造函数中定义的类函数的默认参数
- javascript - 在 nextjs 上处理延迟加载数据列表
- c++ - 复制后删除原始数组指针将前 3 个字节设置为 0
- c++ - 在 Qt 项目中独立于路径包含外部库
- python - `PIL.Image.Show()` 临时存储图像在哪里,之后会被删除吗?
- python - 使用 Google Docs API 在不影响格式的情况下更改整个文档的字体