19

I am trying to make a semi-resuseable widget but I am running into a problem. I am trying to encapsulate a some CSS code inside a shadow root so that it does not affect the rest of the webpage but this CSS is used across multiple widgets so I am trying to include a remote stylesheet. None of the examples I have found use a remote style sheet and I was wondering if this was possible.

EX:

<template id="templateContent">
    <head>
        <link rel="stylesheet" href="css/generalStyle1.css">
    </head>
    <body>
        <div class="affectedByGeneralStyle1"></div>
    </body>
</template>

script to include template:

<div id="host"></div>
<script>
    var importedData = (html_import_element).import.getElementById("templateContent");
    var shadow = document.querySelector('#host').createShadowRoot();
    var clone = document.importNode(importedData.content, true);
    shadow.appendChild(clone);
</script>
4

4 回答 4

24

我最近遇到了同样的问题,我最终做的是使用:

<template id="templateContent">
     <style> @import "css/generalStyle.css"; </style>
</template>

附加信息:这工作得很好,除了,现在我遇到了一些缓存问题,因为 Chrome 在硬重新加载后似乎没有重新加载这些资源。

于 2014-04-25T07:31:13.090 回答
17

让我们补充一下答案。现在 shadow dom 支持直接标签。

你可以直接使用

<link rel="stylesheet" href="yourcss1.css">
<link href="yourcss2.css" rel="stylesheet" type="text/css">  

检查它们是否已由 whatwg 和 W3C 更新

在 shadow dom 中使用 css 的有用链接。

https://w3c.github.io/webcomponents/spec/shadow/#inertness-of-html-elements-in-a-shadow-tree https://github.com/whatwg/html/commit/43c57866c2bbc20dc0deb15a721a28cbaad2140c

https://github.com/w3c/webcomponents/issues/628

直接 css 链接可以在 shadow dom 中使用

谢谢。

于 2018-01-11T07:58:20.793 回答
2

我以这种方式将样式表的链接元素直接添加到影子根:

    let link = document.createElement('link');
    link.setAttribute('rel', 'stylesheet');
    link.setAttribute('href', 'whatever.css');
    this.shadowRoot.appendChild(link);

它似乎工作正常。(我从组件的构造函数中调用了它。)

于 2019-03-20T19:15:57.247 回答
1

实际上,聚合物有一个内部实用程序来加载 css 链接,我已经实现了一个使用聚合物内部 css 处理器的 javascript 函数,所以如果你想在运行时添加 css 链接,你可以使用它:

Polymer('my-element', {           
        ready: function () {
           this.importCss("path/myfile.css");
        },
        importCss: function (path) {
             var $shadow = $(this.shadowRoot);
            var $head = $("<div></div>");
            var $link = $("<link rel='stylesheet' type='text/css'>");
            $link.attr("href", path);
            $head.append($link);
            var head = $head[0];
            this.copySheetAttributes = Polymer.api.declaration.styles.copySheetAttributes;
            Polymer.api.declaration.styles.convertSheetsToStyles.call(this, head);
            var styles = Polymer.api.declaration.styles.findLoadableStyles(head);
            if (styles.length) {
                var templateUrl = this.baseURI;
                Polymer.styleResolver.loadStyles(styles, templateUrl, function () {
                    var $style = $shadow.find("style");
                    if ($style.length > 0){
                        $shadow.find("style").append($head.find("style").html());
                    }else{
                        $shadow.append($head.html());
                    }
                });
            }
        }
});

注意: 此代码需要 jquery 才能运行

于 2015-04-02T13:25:07.107 回答