如何在`内部设置Polymer组件的响应宽度 `

问题描述 投票:1回答:1

考虑以下代码,其中<my-item>200px中始终具有固定宽度<iron-list grid>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Polymer Element Test Case</title>
  <!-- Load webcomponents-loader.js to check and load any polyfills your browser needs -->
  <script src="bower_components/webcomponentsjs/webcomponents-loader.js"></script>
  <link rel="import" href="bower_components/polymer/polymer-element.html">

  <link rel="import" href="bower_components/iron-list/iron-list.html">
</head>
<body>

  <h1>iron-list-grid-calc-issue</h1>
  <!-- Test case HTML goes here -->

  <test-case>
  </test-case>

  <dom-module id="test-case">
    <template>
      <iron-list items="[[items]]" grid>
        <template>
          <div>
            <!-- value: [[item.n]] -->
            <my-item data="[[item]]"></my-item>
          </div>
        </template>
      </iron-list>
    </template>
  </dom-module>

  <dom-module id="my-item">

    <template>
      <style>
        .content {
          width: calc(50% - 32px); /* Not working */
          width: 200px;
          height: 200px;
          line-height: 200px;
          text-align: center;
          border: 1px solid grey;
          box-sizing: border-box;
        }
      </style>

      <div class="container">
        <div class="content">
          data: [[data.n]]
        </div>
      </div>
    </template>
  </dom-module>

  <script>
    window.addEventListener('WebComponentsReady', function() {
      class TestCase extends Polymer.Element {
        static get is() {
          return 'test-case';
        }

        static get properties() {
          return {
            items: {
              type: Array,
              value: function() {
                let items = [];
                for (let i=0; i < 10; i++) {
                  items.push({
                    n: i,
                  });
                }

                return items;
              }
            },
          };
        }
      }

      class MyItem extends Polymer.Element {
        static get is() {
          return 'my-item';
        }

        static get properties() {
          return {
            data: Object,
          };
        }
      }

      window.customElements.define(TestCase.is, TestCase);
      window.customElements.define(MyItem.is, MyItem);
    });
  </script>
</body>
</html>

我打算通过使用<my-item>设置<my-item>的宽度,每行显示2个<my-item>s(可以拉伸)来使width: calc(50% - 32px)响应。我注意到CSS calc()似乎没有按预期工作。

如何在<iron-list grid>中设置Polymer组件的响应宽度?

polymer polymer-2.x css-calc iron-list
1个回答
0
投票

设置项模板的根容器元素的大小(在本例中为<div>)。

<iron-list items="[[items]]" grid>
  <template>
    <div style="width: calc(50% - 32px); height: 200px"> <!-- root container element -->
      <my-item data="[[item]]"></my-item>
    </div>
  </template>
</iron-list>

<head>
  <base href="https://polygit.org/polymer+v2.3.1/components/">
  <script src="webcomponentsjs/webcomponents-loader.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-list/iron-list.html">
</head>
<body>
  <h1>iron-list-grid-calc-issue</h1>
  <test-case></test-case>

  <dom-module id="test-case">
    <template>
      <style>
        .item {
          width: calc(50% - 32px);
          height: 200px;
        }
      </style>
      <iron-list items="[[items]]" grid>
        <template>
          <!-- Set the size of the item template's root container,
               which is this `div` element. -->
          <div class="item">
            <my-item data="[[item]]"></my-item>
          </div>
        </template>
      </iron-list>
    </template>
  </dom-module>

  <dom-module id="my-item">

    <template>
      <style>
        .content {
          /* NOTE: The item size is set in the item template
             in `iron-list` */

          /*width: calc(50% - 32px);*/
          /*width: 200px;*/
          /*height: 200px;*/
          line-height: 200px;
          text-align: center;
          border: 1px solid grey;
          box-sizing: border-box;
        }
      </style>

      <div class="container">
        <div class="content">
          data: [[data.n]]
        </div>
      </div>
    </template>
  </dom-module>

  <script>
    window.addEventListener('WebComponentsReady', function() {
      class TestCase extends Polymer.Element {
        static get is() {
          return 'test-case';
        }

        static get properties() {
          return {
            items: {
              type: Array,
              value: function() {
                let items = [];
                for (let i=0; i < 10; i++) {
                  items.push({
                    n: i,
                  });
                }

                return items;
              }
            },
          };
        }
      }

      class MyItem extends Polymer.Element {
        static get is() {
          return 'my-item';
        }

        static get properties() {
          return {
            data: Object,
          };
        }
      }

      window.customElements.define(TestCase.is, TestCase);
      window.customElements.define(MyItem.is, MyItem);
    });
  </script>
</body>

codepen

© www.soinside.com 2019 - 2024. All rights reserved.