【问题标题】:Applying Polymer Property within elements Shadow Root styles在元素阴影根样式中应用聚合物属性
【发布时间】:2019-02-21 17:51:27
【问题描述】:

所以动态设置样式很容易,我的问题是基于媒体查询中的动态样式,所以在 max-width: 1000px 之间,我希望样式是基于属性或一些计算的 JS 的东西,比如总轮播的宽度取决于组件的数量。

无论如何,这里有一段代码 sn-p,它不起作用,但显示了我对如何应用这些属性的想法:-P

    <link rel="import" href="../../bower_components/polymer/polymer-element.html">

    <dom-module id="hello-eggs">
      <template>
        <style>
          :host {
            display: block;
            background: [[prop2]];
          }

          @media screen and (max-width: 1000px) {
            background: [[prop2]]
          }
        </style>
        <span>[[prop1]] are here</span>
      </template>

      <script>
        /**
        * @customElement
        * @polymer
        */
        class HelloEggs extends Polymer.Element {
          static get is() { return 'hello-eggs'; }
          static get properties() {
            return {
              prop1: {
                type: String,
                value: 'Hello Eggs'
              },
              prop2: {
                type: String,
                value: '#fc0'
              }
            };
          }
        }

        window.customElements.define(HelloEggs.is, HelloEggs);
      </script>
    </dom-module>

提前谢谢你

【问题讨论】:

    标签: css polymer-2.x shadow-dom


    【解决方案1】:

    没关系,我想出了一个让我快乐的方法,并希望能帮助像我这样的其他人:-D

    基本上,我得到样式表并为新的媒体查询插入一条规则,让我可以设置我想要的内容。我还将宽度更改为 500px

        <link rel="import" href="../../bower_components/polymer/polymer-element.html">
    
        <dom-module id="hello-eggs">
          <template>
            <style>
              :host {
                display: block;
                background: #eee;
                margin-bottom: 10px;
              }
            </style>
            <span>[[prop1]] are here</span>
          </template>
    
          <script>
            /**
            * @customElement
            * @polymer
            */
            class HelloEggs extends Polymer.Element {
              static get is() { return 'hello-eggs'; }
              static get properties() {
                return {
                  prop1: {
                    type: String,
                    value: 'Hello Eggs'
                  },
                  prop2: {
                    type: String,
                    value: '#fc0'
                  }
                };
              }
    
              connectedCallback() {
                super.connectedCallback();
    
                let sheet = this.shadowRoot.styleSheets[0];
                sheet.insertRule(`@media screen and (max-width: 500px) { span { background: ${this.prop2}; } }`, 1);
              }
            }
    
            window.customElements.define(HelloEggs.is, HelloEggs);
          </script>
        </dom-module>
    

    我仍然认为能够在极端情况下将属性放在样式区域中会很棒,但是如果我将所有样式应用到 insertRule 中,这仍然会给我这个,这对于宽度和高度等非常有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      相关资源
      最近更新 更多