通过Javascript更新自定义CSS属性

通过Javascript更新自定义CSS属性,javascript,css,polymer,polymer-1.0,Javascript,Css,Polymer,Polymer 1.0,我正在使用纸质卷轴标题面板和自定义CSS属性 paper-scroll-header-panel { --paper-scroll-header-panel-full-header: { background-image: url(images/abcd.jpg); }; } 设置全尺寸标题的样式。我的问题是,这个自定义组件的标题图像应该根据从加载该组件的页面传递的图像url而有所不同。因此,我定义了一个属性,并试图通过计算属性分配它,但这似

我正在使用纸质卷轴标题面板和自定义CSS属性

 paper-scroll-header-panel {
   --paper-scroll-header-panel-full-header: {
            background-image: url(images/abcd.jpg);
          };
}
设置全尺寸标题的样式。我的问题是,这个自定义组件的标题图像应该根据从加载该组件的页面传递的图像url而有所不同。因此,我定义了一个属性,并试图通过计算属性分配它,但这似乎不起作用。这就是我所做的

<paper-scroll-header-panel style$={{backgroundCover}}>
但这是行不通的。在使用自定义CSS组件时,如何使用因不同组件实例而异的背景图像? 更新:当前代码如下所示。仍然不起作用

<dom-module id="recipe-layout">

  <link rel="import" type="css" href="recipe-layout.css">
  <style is="custom-style">

    paper-scroll-header-panel {
      position: absolute;
      /* background for toolbar when it is at its full size */
      --paper-scroll-header-panel-full-header: {
        background-image: url();
      };
      /* background for toolbar when it is condensed */
      --paper-scroll-header-panel-condensed-header: {
        background-color: #00bcd4;
      };
    }
    paper-toolbar {
      height: 400px;
      background-color: transparent;
    }
</style>
<template>
 <paper-scroll-header-panel condenses condensed-header-height="56" id="scroller">

        <!-- Main Toolbar -->
        <paper-toolbar>
          <paper-icon-button icon="arrow-back" onclick="javascript:history.go(-1)"></paper-icon-button>
          <div class="flex"></div>
          <paper-icon-button icon="more-vert"></paper-icon-button>
          <div class="bottom title"><content select=".cover-title"></content></div>
        </paper-toolbar>

        <div class="content">
            <content select=".main-content"></content>
        </div>

      </paper-scroll-header-panel>
</template>

  <script>

    Polymer({

      is: 'dummy-layout',

      properties: {
        cover: {
            type: String,
            observer: '_updateBg'
        },
      },

      _updateBg: function(cover) {
        this.async(function() { this.subupdateBg(cover); }, 100);
      },

      subupdateBg: function(cover) {
        var scrollerBg = this.$.scroller;
        console.dir(scrollerBg);
        var newStyle = 'background-image: url('+ cover + ');';
        scrollerBg.customStyle['--paper-scroll-header-panel-full-header'] = newStyle;
        scrollerBg.updateStyles();
    }
  </script>

</dom-module>

纸卷轴头面板{
位置:绝对位置;
/*工具栏处于全尺寸时的背景*/
--纸张滚动页眉面板完整页眉:{
背景图片:url();
};
/*压缩工具栏时工具栏的背景*/
--纸张卷轴标题面板压缩标题:{
背景色:#00bcd4;
};
}
纸张工具栏{
高度:400px;
背景色:透明;
}
聚合物({
是:‘虚拟布局’,
特性:{
封面:{
类型:字符串,
观察者:''u updateBg'
},
},
_updateBg:函数(封面){
this.async(function(){this.subupdateBg(cover);},100);
},
subupdateBg:函数(封面){
var scrollerBg=this.$.scroller;
console.dir(scrollerBg);
var newStyle='背景图像:url('+cover+');';
scrollerBg.customStyle['--纸张滚动页眉面板完整页眉']=newStyle;
scrollerBg.updateStyles();
}

您无法更改style属性中的自定义css属性,因为它仅在元素创建时计算。您必须在元素中的customStyle属性中应用它们,然后调用updateStyles方法


源:

您无法更改style属性中的自定义css属性,因为它仅在元素创建时计算。您必须在元素中的customStyle属性中应用它们,然后调用updateStyles方法


源:

您无法更改style属性中的自定义css属性,因为它仅在元素创建时计算。您必须在元素中的customStyle属性中应用它们,然后调用updateStyles方法


源:

您无法更改style属性中的自定义css属性,因为它仅在元素创建时计算。您必须在元素中的customStyle属性中应用它们,然后调用updateStyles方法


来源:

@Neil是对的。这个
--paper scroll header面板full header
是一种CSS变量,在这里我演示了如何操作它们:

<dom-module id="dummy-layout">

  <style>
    :host {
      --bg: {
        background-color: red;
      }
    }

    .test {
      @apply(--bg);
    }
  }
  </style>

    <template>
      <div class="test">Hello world</div>
      <button type="button" on-click=btn>Click</button>
    </template>

  <script>
    Polymer({
      is: "dummy-layout",
      properties: {
        data: {
          type: String,
          observer: '_updateStyle'
        },
      },
      btn: function () {
        this.set('data', Math.random());
      },
      _updateStyle: function () {
        var colors = ['blue', 'green'];
        this.i = (this.i || 0) + 1;
        var newStyle = 'background-color: '+colors[this.i%2]+';';
        this.customStyle['--bg'] = newStyle;
        this.updateStyles();
      }
    });
  </script>

</dom-module>

:主持人{
--背景:{
背景色:红色;
}
}
.测试{
@应用(--bg);
}
}
你好,世界
点击
聚合物({
是:“虚拟布局”,
特性:{
数据:{
类型:字符串,
观察者:“_updateStyle”
},
},
btn:函数(){
this.set('data',Math.random());
},
_updateStyle:function(){
变量颜色=['蓝色','绿色'];
this.i=(this.i | | 0)+1;
var newStyle='background color:'+colors[this.i%2]+';';
this.customStyle['--bg']=newStyle;
this.updateStyles();
}
});

您的代码几乎可以正常工作。实际发生的是,您的样式更改代码从未被调用,因为封面从未更改。如果您添加更改它的按钮或其他任何操作,它都可以正常工作。以下是您的代码,只需稍加修改:

@Neil是对的。此
--纸张滚动页眉面板完整页眉
是一种CSS变量,where我演示如何操作它们:

<dom-module id="dummy-layout">

  <style>
    :host {
      --bg: {
        background-color: red;
      }
    }

    .test {
      @apply(--bg);
    }
  }
  </style>

    <template>
      <div class="test">Hello world</div>
      <button type="button" on-click=btn>Click</button>
    </template>

  <script>
    Polymer({
      is: "dummy-layout",
      properties: {
        data: {
          type: String,
          observer: '_updateStyle'
        },
      },
      btn: function () {
        this.set('data', Math.random());
      },
      _updateStyle: function () {
        var colors = ['blue', 'green'];
        this.i = (this.i || 0) + 1;
        var newStyle = 'background-color: '+colors[this.i%2]+';';
        this.customStyle['--bg'] = newStyle;
        this.updateStyles();
      }
    });
  </script>

</dom-module>

:主持人{
--背景:{
背景色:红色;
}
}
.测试{
@应用(--bg);
}
}
你好,世界
点击
聚合物({
是:“虚拟布局”,
特性:{
数据:{
类型:字符串,
观察者:“_updateStyle”
},
},
btn:函数(){
this.set('data',Math.random());
},
_updateStyle:function(){
变量颜色=['蓝色','绿色'];
this.i=(this.i | | 0)+1;
var newStyle='background color:'+colors[this.i%2]+';';
this.customStyle['--bg']=newStyle;
this.updateStyles();
}
});

您的代码几乎可以正常工作。实际发生的是,您的样式更改代码从未被调用,因为封面从未更改。如果您添加更改它的按钮或其他任何操作,它都可以正常工作。以下是您的代码,只需稍加修改:

@Neil是对的。此
--纸张滚动页眉面板完整页眉
是一种CSS变量,where我演示如何操作它们:

<dom-module id="dummy-layout">

  <style>
    :host {
      --bg: {
        background-color: red;
      }
    }

    .test {
      @apply(--bg);
    }
  }
  </style>

    <template>
      <div class="test">Hello world</div>
      <button type="button" on-click=btn>Click</button>
    </template>

  <script>
    Polymer({
      is: "dummy-layout",
      properties: {
        data: {
          type: String,
          observer: '_updateStyle'
        },
      },
      btn: function () {
        this.set('data', Math.random());
      },
      _updateStyle: function () {
        var colors = ['blue', 'green'];
        this.i = (this.i || 0) + 1;
        var newStyle = 'background-color: '+colors[this.i%2]+';';
        this.customStyle['--bg'] = newStyle;
        this.updateStyles();
      }
    });
  </script>

</dom-module>

:主持人{
--背景:{
背景色:红色;
}
}
.测试{
@应用(--bg);
}
}
你好,世界
点击
聚合物({
是:“虚拟布局”,
特性:{
数据:{
类型:字符串,
观察者:“_updateStyle”
},
},
btn:函数(){
this.set('data',Math.random());
},
_updateStyle:function(){
变量颜色=['蓝色','绿色'];
this.i=(this.i | | 0)+1;
var newStyle='background color:'+colors[this.i%2]+';';
this.customStyle['--bg']=newStyle;
this.updateStyles();
}
});
您的代码几乎可以正常工作。实际发生的是,您的样式更改代码从未被调用,因为封面从未更改。如果您添加更改它的按钮或
    this.$.scroller.$.headerBg.style.backgroundImage = "url(path/to/image)";