我的想法是讓不同的文件(包括.css或.html)包含特定主題的所有規則並加載主題,以動態地改變運行時的Polymer應用程序的外觀&。什麼是動態改變使用聚合物構建的應用程序樣式的最佳方式?
我發現非常有用這裏所描述的聚合物本身using custom style at document level這種方法(使用.html文件)
通常你想在文檔級別定義自定義屬性值,設置主題例如,對於整個應用程序。由於自定義屬性尚未內置到大多數瀏覽器中,因此需要使用特殊的自定義樣式標記來定義聚合物元素之外的自定義屬性。嘗試添加您的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文件中。
然後我就計算器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使瀏覽器重新加載該文件,並應用更改,wherease在我的情況下,changin html導入的href並沒有達到相同的效果。
有沒有辦法讓這個工作,因爲我想?有更好的解決方案來實現我的目標嗎?
嗨,謝謝你的回答。我曾閱讀過有關通過代碼更改CSS屬性並調用updateStyles的方法。但通過這種方式,如果我有一個按鈕「改爲紅色主題」,我將不得不硬編碼的變化(例如:'this.customStyle [' - 我的工具欄顏色'] ='紅';')這是我想避免創建一個包含我所有屬性的my-theme.html。我想從中加載我的新屬性的red-theme.html。不可能做到? – Incio
@Incio以及我不知道是否有可能。我不知道如何解決它,就像你想..:/我現在嘗試了幾個測試用例,但沒有一個是成功的。顯然,對聚合物有更多瞭解的人可以幫忙 –
謝謝庫巴。我可能試圖做一些不可能的事情。改變方法並使用你的方法,我正在考慮在my-theme.html中創建mixins並以編程方式應用它們。通過這種方式,它們仍然會在我的文件中定義,而不是在代碼中進行硬編碼。這可能是一個解決方案嗎? – Incio