如何从引导程序的文档中单击外部以关闭此自定义offcanvas导航?

时间:2019-07-29 17:54:49

标签: jquery twitter-bootstrap

我正在使用Bootstrap 4文档中的offcanvas实验代码,但是当您在菜单外单击时,它不是设计为关闭的,这是我真的希望这样做。

我已经尝试了Web上的多个jquery代码段,但是它们利用了bootstrap的.collapse,当我编辑它们以包含自定义.offcanvas-collapse类时,它们不起作用。

<nav class="navbar fixed-top navbar-dark bg-dark">
      <button class="navbar-toggler p-0 border-0" type="button" data-toggle="offcanvas" data-target="#offcanvasNav" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <i data-feather="menu"></i>
          </button>
            <a class="navbar-brand" href="#">Sleight of Word</a>
            <button class="navbar-toggler p-0 border-0" type="button" data-toggle="searchBar">
              <i data-feather="search"></i>
            </button>
            <form class="form-inline my-2 my-lg-0 d-none">
              <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
              <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>

          <div class="navbar-collapse offcanvas-collapse" id="offcanvasNav">
            <ul class="navbar-nav mr-auto">
              <li class="nav-item active">
                <a class="nav-link" href="#">Dashboard <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Notifications</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Profile</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Switch account</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Settings</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
            </ul>
            <form class="form-inline my-2 my-lg-0">
              <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
              <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
          </div>
        </nav>

.offcanvas-collapse {
    position: fixed;
    top: 56px; /* Height of navbar */
    bottom: 0;
    right: 100%;
    width: 240px;
    padding-right: 1rem;
    padding-left: 1rem;
    overflow-y: auto;
    visibility: hidden;
    background-color: purple;
    transition: visibility .2s ease-in-out, -webkit-transform .2s ease-in-out;
    transition: transform .2s ease-in-out, visibility .2s ease-in-out;
    transition: transform .2s ease-in-out, visibility .2s ease-in-out, -webkit-transform .2s ease-in-out;
  }

  .offcanvas-collapse.open {
    visibility: visible;
    -webkit-transform: translateX(100%);
    transform: translateX(100%);
  }


$(function () {
  'use strict'

  $('[data-toggle="offcanvas"]').on('click', function () {
    $('.offcanvas-collapse').toggleClass('open')
  })
})

$(document).on('click',function(){
    $('.offcanvas-collapse').offcanvas-collapse('hide');
})

我希望通过菜单按钮使offcanvas导航可切换(打开和关闭),并在菜单外部单击时使导航关闭,但是在导航外部单击则无济于事。

1 个答案:

答案 0 :(得分:1)

$(document).on('click',function(){
  $('.offcanvas-collapse').toggleClass('open');
})

单独,将在文档中所有具有类open的元素上切换类offcanvas-collapse。如果元素具有类,则将其删除,否则将添加它。

如果将以上内容与其他代码段结合使用

$(function () {
  'use strict'

  $('[data-toggle="offcanvas"]').on('click', function () {
    $('.offcanvas-collapse').toggleClass('open')
  })
})

...,每当您单击[data-toggle="offcanvas"]时,两个代码段都在运行(因为您单击了[data-toggle="offcanvas"],但是您也单击了document(因为click事件一直冒到document为止,除非被阻止),因此类offcanvas-collapse被切换了两次,因此看起来好像不起作用。

那显然不是你所追求的。

您可能想要:

  • 在单击open时在.offcanvas-collapse元素上切换类[data-toggle="offcanvas"] ,允许该click事件传播到document
  • 当在文档中的任何位置单击时,
  • open元素中删除(而不是切换)类.offcanvas-collapse。如果您使用toggle,则clickdocument中的$(function () { 'use strict' $(document) .on('click', '[data-toggle="offcanvas"]', function(e) { $('.offcanvas-collapse').toggleClass('open'); // toggle `.open` e.stopPropagation(); // and stop propagation }) .on('click', function() { $('.offcanvas-collapse').removeClass('open'); // remove `.open` }); }) 将在关闭cancanvas时打开,这可能不是您想要的。

话虽如此,这是实际的操作方式:

.on()

编辑:根据评论请求,要仅在导航栏外部单击时关闭画布,请将第二个.on('click', function(e) { if (!$(e.target).closest('nav.fixed-top').is('nav')) { $('.offcanvas-collapse').removeClass('open'); // remove `.open` } }); 替换为此:

<nav>

检查目标是否位于fixed-top类的C = np.array([[1,1,0,1,1,0,0,0,0],[0,1,1,0,1,1,0,0,0],[0,0,0,1,1,0,1,1,0],[0,0,0,0,1,1,0,1,1]]) H = C*0.25 dimension_y,dimension_x = H.shape observation_based_state_graph = np.dot(np.transpose(H),H) for i in range(dimension_x): observation_based_state_graph[i,i] = 0 G_state=nx.from_numpy_matrix(observation_based_state_graph) communities_generator = community.asyn_fluidc(G_state,2) edges = G_state.edges() weights = [G_state[u][v]['weight'] for u,v in edges] community_list_x = [[0,1,2,3],[5,6,7,8],[4]] labels = {} labels[0] = '0' labels[1] = '1' labels[2] = '2' labels[3] = '3' labels[4] = '4' labels[5] = '5' labels[6] = '6' labels[7] = '7' labels[8] = '8' pos=nx.spring_layout(G_state) nx.draw_networkx_nodes(G_state,pos, nodelist=community_list_x[0], node_color='r', node_size=500, alpha=0.8) nx.draw_networkx_nodes(G_state,pos, nodelist=community_list_x[1], node_color='b', node_size=500, alpha=0.8) nx.draw_networkx_nodes(G_state,pos, nodelist=community_list_x[2], node_color='g', node_size=500, alpha=0.8) nx.draw_networkx_edges(G_state,pos,width=weights,alpha=0.5) nx.draw_networkx_labels(G_state, pos, labels, font_size=16) labels = nx.get_edge_attributes(G_state,'weight') nx.draw_networkx_edge_labels(G_state,pos,edge_labels=labels) plt.show() 内部。