iframe CSS注入加载优先级
我试图将一些CSS注入一个iframe,而且效果很好,除了在新的CSS生效之前有一个延迟,因此您将获得一个加载一个主题然后更改颜色的页面。它只能看到一秒钟,但很烦人。有什么方法可以将iFrame渲染延迟到应用CSS?我在CSS之后添加了一个脚本,但似乎由于我将其注入DOM,因此会渲染然后重新租赁。我已经尝试使用ReadyState Interactive
和完成
在加载前将其获取,但没有运气,也不确定如何延迟它。还尝试了addeventListener(“ load”)
,但在加载射击之前似乎会发生变化。
const frame_doc = document.getElementById("iframe").contentDocument;
function setFrameClass(light) {
if (light) {
frame_doc.body.classList.remove("dark")
} else {
frame_doc.body.classList.add("dark")
}
}
themeSwitch.addEventListener('change', function () {
setFrameClass(this.checked, frame_doc.body)
});
frame_doc.onreadystatechange = function () {
if (frame_doc.readyState === 'interactive') {
let link = frame_doc.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = new URL('rootstatic/css/dark.css', window.location.origin).href;
link.media = 'all';
frame_doc.head.appendChild(link);
let s = frame_doc.createElement("script");
s.type = "text/javascript";
s.src = new URL('rootstatic/js/dark-mode.js', window.location.origin).href;
frame_doc.head.appendChild(s);
setFrameClass(themeSwitch.checked)
}
}
I'm trying to inject some CSS into an iFrame and it works fine except there is a delay before the new CSS takes effect, so you get a page that loads one theme and then changes color. It's only visible for a second, but it's annoying. Is there any way to delay the iFrame render until that CSS is applied? I add a script after the CSS, but seems that since I'm injecting this into the DOM, it renders then re-renders. I've tried using readyState interactive
and complete
to get it before load, but no luck and not sure how to delay it. Also tried addEventListener("load")
, but it appears to change before the load fires.
const frame_doc = document.getElementById("iframe").contentDocument;
function setFrameClass(light) {
if (light) {
frame_doc.body.classList.remove("dark")
} else {
frame_doc.body.classList.add("dark")
}
}
themeSwitch.addEventListener('change', function () {
setFrameClass(this.checked, frame_doc.body)
});
frame_doc.onreadystatechange = function () {
if (frame_doc.readyState === 'interactive') {
let link = frame_doc.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = new URL('rootstatic/css/dark.css', window.location.origin).href;
link.media = 'all';
frame_doc.head.appendChild(link);
let s = frame_doc.createElement("script");
s.type = "text/javascript";
s.src = new URL('rootstatic/js/dark-mode.js', window.location.origin).href;
frame_doc.head.appendChild(s);
setFrameClass(themeSwitch.checked)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论