gpt4 book ai didi

javascript - 从 shadowDOM 内部加载链接时不加载外部字体

转载 作者:行者123 更新时间:2023-12-01 16:25:40 27 4
gpt4 key购买 nike

一开始我想说我知道这个问题类似于:

How to let imported css font / icons have effects on elements in the shadow dom?

事实并非如此,也无济于事。

问题:
我最近决定使用 ShadowDOM 来封装我的元素中的样式。一开始我认为它按预期工作,但我注意到来自外部 CSS 文件的一些图标已经消失。
重要的是要注意这些样式是 外部 并且做出改变的可能性是有限的。

我已经准备好代码来演示这个问题(请看下面的代码片段)。

除了 之外,一切似乎都运行良好@font-face
如您所见,包含带有图标的外部 CSS 文件的 HTML 在 ShadowDOM 之外按预期工作。我也想在 shadowDOM 中使用它。

我怎样才能做到这一点?

注意:如果您检查开发工具,网络选项卡中的 CSS 路径存在问题,但这是 SO 片段问题。如果您在本地运行代码段,则网络中一切正常。

const body = document.getElementsByTagName('body')[0];
const wrapper = document.querySelector('.wrapper')

const handleAddToShadowClick = (param) => {
const host = document.querySelector('#shadowHost');
if(param === 'insideShadow') {
const shadowRoot = host.attachShadow({mode: 'open'});
shadowRoot.innerHTML = firstComponent
} else {
const shadowRoot = host;
wrapper !== null ? body.removeChild(wrapper): ''
shadowRoot.innerHTML = firstComponent
}
}

const firstComponent = `
<div class="wrapper">
<div class="icon login">Bla bla</div>
<div style="font-family: testFont;">Sample String od text</div>
<link rel="stylesheet" href="./style.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous">
</div>
`
.wrapper {
font-family: agGridBalham;
background-color: aquamarine;
color: black;
}

.balham:before {
content: "\F11F";
}

.login::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f007";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
Refresh the page after each button click
</p>
<button onclick="handleAddToShadowClick('outside')">Add outside shadow</button>
<button onclick="handleAddToShadowClick('insideShadow')">Add inside shadow</button>
<div>
<div id="shadowHost"> </div>
</div>

<script src="./script.js"></script>
</body>
</html>

最佳答案

问题中提到的行为是 Chrome 中存在的一个错误(它也存在于 Gecko 中,也不确定它是否已修复)。

Here is the link for the bug reported at chromium related to this issue, which still not resolved by them. At, present i feel this is the only workaround which will work.



该问题主要与@font-face 的范围有关。当前,当它们仅包含在 shadow DOM 中时,您不能使用令人敬畏的字体。所以为了使用字体,字体 css 必须存在于 light DOM 和 shadow DOM 中。所以你需要在 shadow dom 和 light dom 中都导入字体 css。

这是 Working Plunker这解决了你的问题。
<html>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous">
</head>

<body>
Refresh the page after each button click
</p>
<button onclick="handleAddToShadowClick('outside')">Add outside shadow</button>
<button onclick="handleAddToShadowClick('insideShadow')">Add inside shadow</button>
<div>
<div id="shadowHost"> </div>
</div>
<script src="lib/script.js"></script>
</body>
</html>

正如您在上面看到的那样,我们在 light DOM 和 Shadow Dom 中都包含了令人敬畏的字体 css。它按预期工作正常。

关于javascript - 从 shadowDOM 内部加载链接时不加载外部字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62129243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com