【发布时间】:2017-04-07 08:40:11
【问题描述】:
我的想法是让不同的文件(.css 或 .html)包含特定主题的所有规则,并在运行时动态更改 Polymer 应用程序的外观和加载主题。
我发现 Polymer 自己在此处描述的这种方法非常有用 using custom style at document level(使用 .html 文件)
您经常希望在文档级别定义自定义属性值,例如为整个应用程序设置主题。因为自定义属性尚未内置到大多数浏览器中,所以您需要使用特殊的自定义样式标签来定义 Polymer 元素之外的自定义属性。尝试在 index.html 文件的标记中添加以下代码
基本上我们定义了一个my-theme.html,它定义了应用程序样式中使用的变量,看起来像这样:
<style is="custom-style">
/* Define a document-wide default—will not override a :host rule in icon-toggle-demo */
:root {
--icon-toggle-outline-color: red;
}
/* Override the value specified in icon-toggle-demo */
icon-toggle-demo {
--icon-toggle-pressed-color: blue;
}
/* This rule does not work! */
body {
--icon-toggle-color: purple;
}
</style>
然后我将它导入到我的 index.html 文件中。
然后我在stackoverflow上找到this方法来动态改变样式。
HTML
<link type="text/css" rel="stylesheet" media="all" href="../green.css" id="theme_css" />
JS
document.getElementById('buttonID').onclick = function () {
document.getElementById('theme_css').href = '../red.css';
};
这里的问题是文件是rel: stylesheet,更改其href会导致浏览器重新加载文件并应用更改,而在我的情况下,更改html导入的href不会产生相同的效果.
有没有办法让它按我的意愿工作?是否有更好的解决方案来实现我的目标?
【问题讨论】:
标签: javascript html css polymer