我想知道如何动态地将font-size设置为<paper-textarea> (即从JS而不是CSS设置font-size )。
例如,<paper-textarea>的DOM树如下所示:
<paper-textarea> <div><div><div><textarea> <--这是实际的文本区域
有没有任何方法可以通过JS锁定shadowDOM中的子节点并直接设置它的样式?
举个例子,这当然是行不通的。
fontSize绑定到<paper-textarea>fontSize,所以它无法工作。
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<link href="paper-input/paper-textarea.html" rel="import">
<link href="paper-button/paper-button.html" rel="import">
<dom-module id="x-example">
<style>
:host {
display: block;
max-width: 320px;
}
</style>
<template>
<paper-textarea label="Textarea" value="{{inputData}}" font-size="{{fontSize}}"></paper-textarea>
<paper-button class="primary" on-tap="incrementFontSize">Increment Font Size</paper-button>
</template>
<script>
HTMLImports.whenReady(function() {
"use strict";
Polymer({
is: "x-example",
properties: {
inputData: {
type: String,
value: "Hello World"
},
fontSize: {
type: Number,
value: 16
}
},
incrementFontSize: function() {
this.set("fontSize", this.fontSize + 4);
}
});
});
</script>
</dom-module>
<x-example></x-example>
理想情况下,我应该设置一个fontSize观察者,并且必须针对<paper-textarea>的子节点,以便在fontSize更改时设置它的样式。
我怎样才能瞄准这些节点?
注:我更喜欢“正式”的方式,如果它存在的话。聚合物规范经常发生变化,脆弱的黑客在更新图书馆时往往会崩溃。
发布于 2016-06-30 15:32:45
这里有一种方法:
<dom-module id="dynamic-style">
<template>
<style>
:host{
--myfont:10px;
}
paper-textarea{
--paper-input-container-input:{
font-size:var(--my-font);
};
}
</style>
<paper-textarea on-tap="updateStyle"></paper-textarea>
</template>
</dom-module>
<script>
Polymer({
is:"dynamic-style",
properties:{
fontSize:{
type:Number,
value:10
}
},
updateStyle:function(){
var style={'--my-font':this.computeFont()+'px'}
this.updateStyles(style);
},
computeFont:function(){
return this.fontSize+=4;
}
})
</script>动态样式是Polymer当前的一个问题,根据推荐,updateStyles是它工作的唯一方法。
下面是一个基于您的示例的工作片段:
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<link href="paper-input/paper-textarea.html" rel="import">
<link href="paper-button/paper-button.html" rel="import">
<dom-module id="x-example">
<style is="custom-style">
:host {
display: block;
max-width: 320px;
}
:root {
--font-size: 12px;
--paper-input-container-input: {
font-size: var(--font-size);
}
}
</style>
<template>
<paper-textarea label="Textarea" value="{{inputData}}"></paper-textarea>
<paper-button class="primary" on-tap="incrementFontSize">Increment Font Size</paper-button>
</template>
<script>
HTMLImports.whenReady(function() {
"use strict";
Polymer({
is: "x-example",
properties: {
inputData: {
type: String,
value: "Hello World"
},
fontSize: {
type: Number,
value: 16
}
},
incrementFontSize: function() {
var style = {
'--font-size': this.computeFont() + 'px'
}
this.updateStyles(style);
},
computeFont: function() {
return this.fontSize += 4;
}
});
});
</script>
</dom-module>
<x-example></x-example>
发布于 2016-06-30 11:56:52
你可以在你的聚合物中使用普通的javascript,只需写
this.style.fontSize = Number(this.fontSize + 4)+"px"https://stackoverflow.com/questions/38122206
复制相似问题