聚合物纸对话框背景不透明?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聚合物纸对话框背景不透明?相关的知识,希望对你有一定的参考价值。

我的Polymer元素中有一个纸质对话框。我想让背景不透明,现在它是半透明的。我也想改变颜色。

有谁知道怎么做。我已经在自定义元素中尝试了这个css:

<style is="custom-style">
--iron-overlay-backdrop-opacity:1;
</style>

<paper-dialog modal></paper-dialog>

但它没有效果。

我也试过了

<style is="custom-style">
:host {
  --iron-overlay-backdrop-opacity:1;
}
</style>

<paper-dialog modal></paper-dialog>
答案

iron-overlay-backdrop直接附加到文档正文中,位于自定义元素之外,并且由于Polymer的CSS封装,您无法使用元素内的<style>为背景设置样式。

但是,您的元素可以使用Polymer.updateStyles(...)对背景进行必要的样式设置,iron-overlay-backdrop会更新所有自定义元素的样式,包括元素外部的Polymer.updateStyles({ '--iron-overlay-backdrop-background-color': 'red', '--iron-overlay-backdrop-opacity': '1' });

<head>
  <base href="https://polygit.org/polymer+1.11.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="paper-button/paper-button.html">
  <link rel="import" href="paper-dialog/paper-dialog.html">
</head>
<body>

  <div>Click button for dialog</div>
  <x-demo></x-demo>

  <dom-module id="x-demo">
    <template>
      <x-dialog id="blue" backdrop-color="blue" backdrop-opacity="1"></x-dialog>
      <x-dialog id="red" backdrop-color="red" backdrop-opacity="0.2"></x-dialog>
      <x-dialog id="green" backdrop-color="green" backdrop-opacity="0.5"></x-dialog>
      <paper-button on-tap="_openDialog" data-dialog="blue">Blue (opacity 100%)</paper-button>
      <paper-button on-tap="_openDialog" data-dialog="red">Red (opacity 20%)</paper-button>
      <paper-button on-tap="_openDialog" data-dialog="green">Green (opacity 50%)</paper-button>
    </template>
    <script>
      HTMLImports.whenReady(function() {
        Polymer({
          is: 'x-demo',
          _openDialog: function(e) {
            var dialog = e.target.dataset.dialog;
            this.$[dialog].opened = true;
          }
        })
      });
    </script>
  </dom-module>

  <dom-module id="x-dialog">
    <template>
      <paper-dialog modal with-backdrop opened="{{opened}}">
        <div class="buttons">
          <paper-button dialog-dismiss>Close</paper-button>
        </div>
      </paper-dialog>
    </template>
    <script>
      HTMLImports.whenReady(function() {
        Polymer({
          is: 'x-dialog',
          properties: {
            backdropColor: {
              type: String,
              value: 'green'
            },
            backdropOpacity: {
              type: String,
              value: '0.6'
            },
            opened: {
              type: Boolean,
              value: false
            }
          },

          observers: ['_updateBackdrop(opened, backdropColor, backdropOpacity)'],

          _updateBackdrop: function(opened, color, opacity) {
            if (opened && color && opacity) {
              Polymer.RenderStatus.afterNextRender(this, function() {
              	this._setBackdropStyles(color, opacity);
              });
            }
          },
          _setBackdropStyles: function(color, opacity) {
            Polymer.updateStyles({
              '--iron-overlay-backdrop-background-color': color,
              '--iron-overlay-backdrop-opacity': opacity
            });
          }
        });
      });
    </script>
  </dom-module>
</body>