材质设计lite导航选项卡自动滑动

时间:2018-08-03 15:52:32

标签: tabs material-design material-design-lite

我使用此库https://getmdl.io/components/index.html#layout-section/tabs来显示“可滚动标签”。 我的问题是,当我选择一个位于箭头(向右或向左)下方的标签时,该标签被选中,但我们无法读取所有单词。在本机应用程序上,我看到选项卡栏自动滚动以显示完整的所选选项卡。 您知道使用该库执行此操作的方法吗?

您可以在此处查看示例:http://jsfiddle.net/fzngbjcw/2/

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
    <script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
</head>
<body>





    <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
        <header class="mdl-layout__header">
            <!-- Tabs -->
            <div class="mdl-layout__tab-bar mdl-js-ripple-effect">
                    <a href="#scroll-tab-1" class="mdl-layout__tab is-active">My tab label 1</a>
                    <a href="#scroll-tab-2" class="mdl-layout__tab">My tabulation label 2</a>
                    <a href="#scroll-tab-3" class="mdl-layout__tab">My tabulation label 3</a>
                    <a href="#scroll-tab-4" class="mdl-layout__tab">My tabulation label 4</a>
                    <a href="#scroll-tab-5" class="mdl-layout__tab">My tabulation label 5</a>
                    <a href="#scroll-tab-6" class="mdl-layout__tab">My tabulation label 6</a>

            </div>
        </header>
        <main class="mdl-layout__content">


        <section class="mdl-layout__tab-panel" id="scroll-tab-1">
            <div class="page-content">
            </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-2">
            <div class="page-content">
            </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-3">
            <div class="page-content">
            </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-4">
            <div class="page-content">
            </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-5">
            <div class="page-content">
            </div>
        </section>


        <section class="mdl-layout__tab-panel" id="scroll-tab-6">
            <div class="page-content">
            </div>
        </section>


    </main>
    </div>


</body>
</html>

谢谢

1 个答案:

答案 0 :(得分:0)

我不认为Material Design Lite对视口外的标签页具有自动滚动功能(您可能已经在Material Components for the web的标签页演示中看到了该功能,开发人员现在就在此处使用该功能)集中精力,将MDL置于“有限支持”中。)

也就是说,当您使用一些js单击部分位于视口之外的标签时,可以在MDL中实现一些简单的自动滚动,以检查被单击的标签是否部分位于视口之外,然后仅触发对相应视图的单击MDL滚动按钮(因此您不必重新发明滚动行为)。请记住,MDL动态创建了一些html元素,因此在选择这些元素之前,您必须先听mdl-componentupgraded事件。以下是一个有效的示例:

const tabs = document.querySelectorAll('.mdl-layout__tab')
let left, right;

document.addEventListener('mdl-componentupgraded', function(event) {
  left = document.querySelector('.mdl-layout__tab-bar-left-button');
  right = document.querySelector('.mdl-layout__tab-bar-right-button');
});

for (let tab of tabs) {
  tab.addEventListener('click', function(event) {
    let bounding = event.target.getBoundingClientRect();
    if (bounding.left < 0) {
      left.click();
    } else if (bounding.right > window.innerWidth) {
      right.click()
    }
  });
}
<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
    <script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
  </head>

  <body>

    <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">

      <header class="mdl-layout__header">
        <!-- Tabs -->
        <div class="mdl-layout__tab-bar mdl-js-ripple-effect">
          <a href="#scroll-tab-1" class="mdl-layout__tab is-active">My tab label 1</a>
          <a href="#scroll-tab-2" class="mdl-layout__tab">My tabulation label 2</a>
          <a href="#scroll-tab-3" class="mdl-layout__tab">My tabulation label 3</a>
          <a href="#scroll-tab-4" class="mdl-layout__tab">My tabulation label 4</a>
          <a href="#scroll-tab-5" class="mdl-layout__tab">My tabulation label 5</a>
          <a href="#scroll-tab-6" class="mdl-layout__tab">My tabulation label 6</a>

        </div>
      </header>

      <main class="mdl-layout__content">
        <section class="mdl-layout__tab-panel" id="scroll-tab-1">
          <div class="page-content">
          </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-2">
          <div class="page-content">
          </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-3">
          <div class="page-content">
          </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-4">
          <div class="page-content">
          </div>
        </section>

        <section class="mdl-layout__tab-panel" id="scroll-tab-5">
          <div class="page-content">
          </div>
        </section>


        <section class="mdl-layout__tab-panel" id="scroll-tab-6">
          <div class="page-content">
          </div>
        </section>
      </main>

    </div>
    
  </body>

</html>