简体   繁体   中英

Navbar Toggler Bootstrap with VueJS - close on click outside directive

Does anyone know if there is a simple method of closing the Boostrap-VueJS toggle navbar when clicking outside?

I've tried multiple directive codes, tried the vue-click-outside plugin, and many different examples but without luck. It seems that when I try to bypass the vue bootstrap component, the hamburger toggle button stops working.

Here is my code:

<b-navbar toggleable="lg" fixed="top">
    <b-navbar-brand class="header-name" :to="{name: 'homeLink'}">Test</b-navbar-brand>
    <b-navbar-toggle class="custom-toggler" target="nav-collapse"></b-navbar-toggle>
    <b-collapse id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->
      <b-navbar-nav class="ml-auto" >
        <b-nav-form>
        </b-nav-form>
         <b-nav-item :to="{name: 'homeLink'}">Home</b-nav-item>
          <b-nav-item :to="{name: 'test1'}">test</b-nav-item>
         <b-nav-item :to="{name: 'test2'}" >test</b-nav-item>
         <b-nav-item :to="{name: 'test3'}" >test</b-nav-item>
         <b-nav-item :to="{name: 'test4'}" >test</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>

I finally got it to work via using the following mechanism:

  1. Installed the vue-click-outside package
  2. imported and followed the directions from the package page
  3. installed jquery and imported it
  4. toggled the collapse event using a root event
<template>
<div>
  <b-navbar toggleable="lg" fixed="top">
    <b-navbar-brand class="header-name" :to="{name: 'homeLink'}">test</b-navbar-brand>
    <b-navbar-toggle class="custom-toggler" target="nav-collapse" v-click-outside="hide"></b-navbar-toggle>
    <b-collapse id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->
      <b-navbar-nav class="ml-auto" >
        <b-nav-form>
        </b-nav-form>
         <b-nav-item :to="{name: 'homeLink'}">Home</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>
</div> 
</template>

<script>
import ClickOutside from 'vue-click-outside'
import * as $ from 'jquery';


export default {
    name: 'appHeader',
    data() {

    },
    mounted () {
    // prevent click outside event with popupItem.
    this.popupItem = this.$el
  },
    methods:{

        hide(){
            console.log('hiding')
            this.$root.$emit('bv::toggle::collapse', 'nav-collapse')

        }

    },
    directives: {
         ClickOutside
     },

}
</script>

One workaround is to think of it like a modal. Whenever the navbar is toggled, create an invisible scrim behind the navbar, with a click handler to close the navbar.

<div 
  v-if="navOpen" 
  @click="navOpen = false" 
  style="position:fixed;left:0;top:0;right:0;bottom:0;z-index:0">
</div>

Here's a working example of what Erich suggested.

This will create a <div> that covers the entire screen, just under your navbar. Which we can then bind a @click event to, to collapse the navbar.

It's bound to the b-collapse v-model, so that it only shows when the collapse is open.

You can even add a <transition> element around the backdrop if you want some sort of fade for the backdrop. (Couldn't get this working in a Stackoverflow Snippet, but you can see an example here

 new Vue({ el: "#app", data() { return { isNavbarCollapseOpen: false }; } });
 .navbar-backdrop { z-index: 1029; background-color: rgba(0, 0, 0, 0.5); position: fixed; top: 0; left: 0; right: 0; bottom: 0; }
 <link href="https://unpkg.com/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://unpkg.com/bootstrap-vue@2.13.0/dist/bootstrap-vue.css" rel="stylesheet" /> <script src="https://unpkg.com/babel-polyfill/dist/polyfill.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script> <script src="https://unpkg.com/bootstrap-vue@2.13.0/dist/bootstrap-vue.js"></script> <div id="app"> <b-navbar toggleable="xl" fixed="top" variant="dark" type="dark"> <b-navbar-brand class="header-name":to="{name: 'homeLink'}">Test</b-navbar-brand> <b-navbar-toggle class="custom-toggler" target="nav-collapse"></b-navbar-toggle> <b-collapse v-model="isNavbarCollapseOpen" id="nav-collapse" is-nav> <:-- Right aligned nav items --> <b-navbar-nav class="ml-auto"> <b-nav-form> </b-nav-form> <b-nav-item:to="{name: 'homeLink'}">Home</b-nav-item> <b-nav-item:to="{name: 'test1'}">test</b-nav-item> <b-nav-item:to="{name: 'test2'}">test</b-nav-item> <b-nav-item:to="{name: 'test3'}">test</b-nav-item> <b-nav-item:to="{name: 'test4'}">test</b-nav-item> </b-navbar-nav> </b-collapse> </b-navbar> <div v-if="isNavbarCollapseOpen" @click="isNavbarCollapseOpen = false" class="navbar-backdrop"> </div> <p v-for="i in 50">Some content</p> </div>

I figureout using this method: https://stackoverflow.com/a/50174966/12647927

import * as $ from 'jquery'

watch: {
'$route' () {
  $('#navbarCallejeritos').collapse('hide')
  },
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM