为什么视频标签未在此视频播放器 Web 组件中加载?
我正在尝试构建一个视频播放器 Web 组件,但我似乎无法正确渲染 video
和 source
元素。
customElements.define('video-player',
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById('video-player-template').content;
console.log(template)
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(template.cloneNode(true));
}
}
);
<template id="video-player-template">
<video controls width="720" height="380" muted autoplay>
<slot name="video-src" />
</video>
<slot></slot>
</template>
<video-player>
<h1>Video player web component</h1>
<source slot="video-src" src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm" />
</video-player>
为什么 video
元素不渲染?
I'm trying to build a video player Web Component, but I can't seem to get the video
and source
elements to render properly.
customElements.define('video-player',
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById('video-player-template').content;
console.log(template)
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(template.cloneNode(true));
}
}
);
<template id="video-player-template">
<video controls width="720" height="380" muted autoplay>
<slot name="video-src" />
</video>
<slot></slot>
</template>
<video-player>
<h1>Video player web component</h1>
<source slot="video-src" src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm" />
</video-player>
Why doesn't the video
element render?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅文档: https://developer.mozilla.org/ en-US/docs/Web/HTML/Element/video
想要一个
元素作为立即 孩子,因此你不能在那里使用
。(
也是如此,它不能有
)您从
Web 组件,src
中提取src
>然后创建该
标记。我已经添加了完整示例的所有 ShadowDOM 样式选项
ShadowDOM 的样式如下:
可继承样式
https://lamplightdev.com /blog/2019/03/26/why-is-my-web-component-inheriting-styles/
(级联)CSS 属性
shadowParts(和主题)
https://meowni.ca/posts/part-theme-explainer/< /p>
被反映,它们不是由 ShadowDOM 设计样式,而是由其容器设计。请参阅:::分槽内容
(2022 年 2 月)可构造样式表仍然是仅限 Chromium 的一方
https://caniuse.com/mdn-api_cssstylesheet_cssstylesheet
see the documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
<video>
wants a<source>
element as immediate child,thus you can't use a
<slot>
there.(the same is true for
<table>
, which can't have<slot>
)You extract the
src
from your<video-player src="...">
Web Component,then create that
<source>
tag.I have added all shadowDOM styling options for a complete example
ShadowDOM is styled by:
<style>
within shadowDOMInheritable styles
https://lamplightdev.com/blog/2019/03/26/why-is-my-web-component-inheriting-styles/
(cascading) CSS properties
shadowParts (and Themes)
https://meowni.ca/posts/part-theme-explainer/
<slot>
are reflected, they are NOT styled by the shadowDOM, but by its container.See: ::slotted content
(feb 2022) Constructible StyleSheets is still a Chromium only party
https://caniuse.com/mdn-api_cssstylesheet_cssstylesheet
使用 observedAttributes 提取设置值的方法略有不同......
逻辑如下:
标签
标签标签中提取的。
的标签设置代码。
中提取的值动态创建标记对象。
这是一些可测试的代码:
A slightly different approach using observedAttributes to extract setup values...
The logic is like this:
<template id="video-player-template"> </template>
tag<video-player>
tag<video-player>
's tag setup code.<video>
tag object with extracted values from<video-player>
.Here is some testable code: