html - 如何让我的 Web 组件一直滚动到底部?

标签 html css polymer web-component

我正在使用网络组件和 polymer 创建网站。

我正在构建一个响应式布局,我的大部分组件都应该位于其父组件的范围内。例如,我的主页是顶部的固定菜单,该菜单停留在那里,页面上的任何内容都在其下方滚动。

这几乎行得通,但我似乎无法让子组件选择其父组件的正确大小,而且一个问题是我无法将子组件一直滚动到其内容的底部。

有很多答案涉及知道固定菜单的大小是多少,并补偿子项的边距,但 Web 组件的整体理念是独立于它们附加到您的应用程序的位置来构建它们.

关于如何让此示例中的“lorem ipsum”文本滚动到底部而无需硬编码其位置的任何想法?无论它是应用程序的顶层还是向下嵌入的 8 层,我都需要它才能工作。正确答案是什么?

演示问题的完整插件在这里:http://plnkr.co/edit/OlOeCP?p=preview

这是 index.html:

<!doctype html>
<html lang="en">

<head>
  <script src="http://polygit.org/components/webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="top-menu.html">

  <style>
    html {
      overflow: hidden;
    }

    body {
      font-family: 'Roboto', 'Noto', sans-serif;
      font-size: 14px;
      overflow: hidden;
    }

    body,
    html {
      height: 100vh;
      width: 100vw;
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <top-menu></top-menu>
</body>

</html>

顶级菜单组件:

<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
<link rel="import" href="http://polygit.org/components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="lorem-ipsum.html">

<dom-module id="top-menu">
  <template>
    <style>
      :host {
        display: block;
        overflow: hidden;
        background-color: pink;
      }

      .container {
        display: block;
        flex-direction: column;
        width: 100%;
      }

      .header {}

      .content {
        flex: 1 1 auto;
        /*min-height: 0;*/
        background-color: orange;
      }
    </style>

    <div class="container">
      <div class="header">
        <paper-toolbar>
          <div class="title">Test</div>
        </paper-toolbar>
      </div>
      <div class="content">
        <lorem-ipsum></lorem-ipsum>
      </div>
    </div>

  </template>

  <script>
    Polymer({
      is: 'top-menu',
    });
  </script>
</dom-module>

还有子组件,它只显示足够的文本来强制滚动:

<link rel="import" href="http://polygit.org/components/polymer/polymer.html">

<dom-module id="lorem-ipsum">
  <template>
    <style>
      :host {
        display: block;
        height: 100vh;
        overflow: auto;
        background-color: green;
      }

      .lorem {
        padding: 10px;
      }
    </style>

    <div class="lorem">
      <h4>The standard Lorem Ipsum passage, used since the 1500s</h4> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

      <h4>Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</h4> "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
      quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius
      modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit
      qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

      <h4>1914 translation by H. Rackham</h4> "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder
      of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who
      loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical
      exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"

      <h4>Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</h4> "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt
      mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas
      assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus,
      ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."

      <h4>1914 translation by H. Rackham</h4> "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue;
      and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice
      is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently
      occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse
      pains."

      <h4>The standard Lorem Ipsum passage, used since the 1500s</h4> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

      <h4>Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</h4> "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
      quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius
      modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit
      qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

      <h4>1914 translation by H. Rackham</h4> "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder
      of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who
      loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical
      exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"

      <h4>Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</h4> "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt
      mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas
      assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus,
      ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."

      <h4>1914 translation by H. Rackham</h4> "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue;
      and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice
      is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently
      occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse
      pains."

      <h4>WHERE IS THIS LINE?</h4>
    </div>
  </template>

  <script>
    Polymer({
      is: 'lorem-ipsum',

      properties: {
        title: {
          type: String
        }
      }
    });
  </script>
</dom-module>

最佳答案

好的,我设法让它工作了。我的解决方案基于这篇文章:

https://blogs.msdn.microsoft.com/kurlak/2015/02/20/filling-the-remaining-height-of-a-container-while-handling-overflow-in-css-ie8-firefox-chrome-safari/

它使用 display:table 来控制高度。我还发现我缺少一些 height:100% 注释,这些注释是将父项的高度传播给子项所必需的。

这个技巧包括子级中的第二个 div 层,它是一个表格单元格。我的顶级菜单组件的模板现在看起来像:

  <template>
    <style>
      :host {
        display: block;
        height: 100%;
      }
      .container {
        display: table;
        height: 100%;
      }
      .header {
        display: table-row;
      }
      .content {
        display: table-row;
        height: 100%;
      }
      .content-inner-wrapper {
        display: table-cell;
        height: 100%;
      }

    </style>

    <div class="container">
      <div class="header">
        <paper-toolbar>
          <div class="title">Test</div>
        </paper-toolbar>
      </div>
      <div class="content">
        <div class="content-inner-wrapper">
            <lorem-ipsum></lorem-ipsum>
        </div>
      </div>
    </div>

  </template>

这是我最终得到的一个 plunker。我确信它可以更干净,但它让我畅通无阻,并且对维度传递的方式有了更好的了解。

http://plnkr.co/edit/J71E3G?p=preview

关于html - 如何让我的 Web 组件一直滚动到底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39672756/

相关文章:

html - 显示 : table-cell problems in chrome while working fine in other browsers

php - Woocommerce 不在目录页面上显示产品描述,但仍显示在单个产品页面中

html - 表单 [input] 宽度在 IE 中不一样

javascript - Twitter、Instagram 或 Facebook 等网络平台如何允许其他网站嵌入其内容?

javascript - polymer 组件 Shady DOM 样式只是对生产的注释

javascript - 如何在 JS 中将毫秒转换为日期?

javascript - 使用 Javascript 的背景颜色自动循环

css - 将 calc() 与 $variables 一起使用时的意外术语

javascript - Web 组件 : Defining a function in class, 内联访问

javascript - 在另一个自定义标签内动态创建 polymer 元素