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

时间:2017-12-29 16:56:49

标签: polymer polymer-2.x css-calc iron-list

请考虑以下代码,其中<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>的宽度设置为<my-item>来始终显示每行2 width: calc(50% - 32px)(可以拉伸),从而使calc()响应。我注意到CSS <iron-list grid>似乎没有按预期工作。

如何在<div class="star-rating"> <?php if ($average = $product->get_average_rating()) : ?> <?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?> <?php endif; ?> </div> 内设置Polymer组件的响应宽度?

1 个答案:

答案 0 :(得分: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