html - Bulma 导航响应已关闭

标签 html css user-interface bulma

JSFiddle:https://jsfiddle.net/ycLumo3k/

我使用的代码与我在 Bulma documentation 中看到的完全相同

但是,如您所见,汉堡菜单没有按预期工作。

我想知道我的代码和原来的代码有什么区别?

有人可以看一下并帮我弄清楚吗?

为了方便,我把代码片段也放在这里:

<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css" rel="stylesheet"/>
    <div class="container">
      <nav class="navbar" role="navigation" aria-label="main navigation">
        <div class="navbar-brand">
          <a class="navbar-item" href="https://bulma.io">
            <img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
          </a>

          <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
          </a>
        </div>

        <div id="navbarBasicExample" class="navbar-menu">
          <div class="navbar-start">
            <a class="navbar-item">
              Home
            </a>

            <a class="navbar-item">
              Documentation
            </a>

            <div class="navbar-item has-dropdown is-hoverable">
              <a class="navbar-link">
                More
              </a>

              <div class="navbar-dropdown">
                <a class="navbar-item">
                  About
                </a>
                <a class="navbar-item">
                  Jobs
                </a>
                <a class="navbar-item">
                  Contact
                </a>
                <hr class="navbar-divider">
                <a class="navbar-item">
                  Report an issue
                </a>
              </div>
            </div>
          </div>

          <div class="navbar-end">
            <div class="navbar-item">
              <div class="buttons">
                <a class="button is-primary">
                  <strong>Sign up</strong>
                </a>
                <a class="button is-light">
                  Log in
                </a>
              </div>
            </div>
          </div>
        </div>
      </nav>
    </div>

最佳答案

在 Bulma 文档页面的下方,它说:

The Bulma package does not come with any JavaScript. You will need to implement the necessary javascript yourself.

这是一些提供必要的导航栏功能的示例代码:

document.addEventListener('DOMContentLoaded', () => {

  // Get all "navbar-burger" elements
  const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);

  // Check if there are any navbar burgers
  if ($navbarBurgers.length > 0) {

    // Add a click event on each of them
    $navbarBurgers.forEach( el => {
      el.addEventListener('click', () => {

        // Get the target from the "data-target" attribute
        const target = el.dataset.target;
        const $target = document.getElementById(target);

        // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
        el.classList.toggle('is-active');
        $target.classList.toggle('is-active');

      });
    });
  }

});

关于html - Bulma 导航响应已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171024/

相关文章:

javascript - 使用 jQuery 定位特定类

javascript - 创建一个将文本转换为 Json 文件的网站

html - ngshow/hide angular 使用 ng-click

css - JSX 中内联样式的正确方法

java - 如何在单击时动态地将 swing 组件添加到 gui 并使其永久化

html - CSS:li 不会在 ul 内居中

Javascript访问childNode

css - 如何使 Divs 在水平导航和页脚之间使用 100% 高度

windows - 在 Windows 上开发 GUI 的最佳语言/工具

java - 如何将所有控制台输出重定向到 GUI 文本框?