Setting the height of core-pages elements?

痴心易碎 提交于 2019-12-13 19:22:23

问题


I'm having a lot of trouble getting a core-pages element to have a non-zero height within my custom element. What is the best practice for having the core-pages height be the same as its selected content. Here's a trivial example which clearly breaks:

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Polymer</title>
  </head>
  <body>
    <script src="http://www.polymer-project.org/platform.js"></script>
    <link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
        <link rel="import" href="http://www.polymer-project.org/components/core-pages/core-pages.html">


    <polymer-element name="x-foo">
      <template>
        <core-pages id="pages" selected="{{selected}}">
          <content></content>
        </core-pages>
      </template>
      <script>
        Polymer('x-foo', {
          ready: function() {
            this.selected = 0;
          }
        });
      </script>
    </polymer-element>

    <polymer-element name="x-bar">
      <template>
          <div><content></content></div>
      </template>
      <script>
        Polymer('x-bar', {});
      </script>
    </polymer-element>


    <p>BEFORE</p>
    <x-foo>
      <x-bar>some text here</x-bar>
      <x-bar>some other text here</x-bar>
    </x-foo>
    <p>AFTER</p>
  </body>
</html>

And the jsbin to see the results: http://jsbin.com/xowoxakuwu/1/edit (notice how the core pages content overlaps with the next element)

This example shows a core-pages element within a custom element. The content that gets injected into the core-pages are also custom elements.

Whats the best practice here?


回答1:


You can apply a style to the currently selected page in the x-foo element which sets display: block and position: relative so x-bar will inherit the height of it's content.

I've also added the "block" attribute to the x-foo element so it too inherits the height of the selected page. Other general attributes are here -> https://www.polymer-project.org/docs/polymer/layout-attrs.html#general-purpose-attributes

<script src="http://www.polymer-project.org/platform.js"></script>
<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
<link rel="import" href="http://www.polymer-project.org/components/core-pages/core-pages.html">

<polymer-element name="x-foo" block>
  <template>

    <style>
      ::content > .core-selected {
        position: relative;
        display: block;
      }
    </style>

    <core-pages id="pages" selected="{{selected}}">
      <content></content>
    </core-pages>
  </template>
  <script>
    Polymer('x-foo', {
      ready: function() {
        this.selected = 0;
      }
    });
  </script>
</polymer-element>

<polymer-element name="x-bar">
  <template>
    <div>
      <content></content>
    </div>
  </template>
  <script>
    Polymer('x-bar', {});
  </script>
</polymer-element>

<p>BEFORE</p>
<x-foo>
  <x-bar>some text here</x-bar>
  <x-bar>some other text here</x-bar>
</x-foo>
<p>AFTER</p>


来源:https://stackoverflow.com/questions/27855639/setting-the-height-of-core-pages-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!