2015-06-24 123 views

回答

1

有不同的方法可以做到這一點,但一個簡單的答案是在改變你的類後使用Polymer.updateStyles()方法。

例如,假設你的風格是:

<style> 
.yellow x-example { 
    --light-primary-color: #fdd85f; 
} 
.red x-example { 
    --light-primary-color: red; 
} 
</style> 

,你想使組件使用的樣式在.red類。您只需像在JavaScript中一樣添加它,然後確保也可以使用此功能在頁面上實際更新它。

<div class="yellow" onclick="this.className='red'; Polymer.updateStyles()"> 
    <x-example></x-example> 
</div> 
1

是的,首先獲取您的自定義元素的對象。然後獲取customStyle對象。爲該對象添加樣式。然後運行element.updateStyles();

 t.clickListener= function(e) { 
      var t = Polymer.dom(e).localTarget; //retarget if needed 
      t.customStyle['--the-color-etc'] = 'pink'; 
      t.updateStyles(); // mandatory for the CSS variables shim 
     }; 

the docs