vuejs 中带有侧边栏的响应式导航栏( bootstrap 或 bootstrap-vue )

Posted

技术标签:

【中文标题】vuejs 中带有侧边栏的响应式导航栏( bootstrap 或 bootstrap-vue )【英文标题】:Responsive navbar with sidebar in vuejs ( bootstrap or bootstrap-vue ) 【发布时间】:2021-03-14 05:29:00 【问题描述】:

我在我的项目中使用 Vue.js,我已经制作了一个仪表板部分,但我想要一些响应式的东西,应该使用 bootstrap 或 bootstrap-vue 或纯 html、CSS。 附上我想要的仪表板图片->

这是我关注的帖子 vuebootstrap b-collapse: when sidebar collapse, change margin-left content div

我的代码---

  <div>
<b-collapse v-model="visible" id="collapse-4">
  <div class="sidebar scrollable-menu">
    <div style="height: 36%">
      <img
        src="../../../public/images/profile.png"
        style="
          width: 70px;
          height: 70px;
          border-radius: 50%;
          margin-top: 40px;
        "
      />
      <h3
        style="
          font-weight: bold;
          font-size: 16px;
          margin-top: 15px;
          color: white;
        "
      >
        Hello name
      </h3>
      <button
        class="mr-2 mt-2 btn"
        style="
          background-color: #595cf5;
          color: white;
          border-radius: 20px;
          font-size: 12px;
          padding: 3px 8px;
        "
      >
        <b-link
          :to=" path: '/dashboard/freelancer/editprofile' "
          style="background-color: #595cf5; color: white"
          >Edit profile</b-link
        >
      </button>
    </div>
    <div style="background-color: #3b4664; height: 65%">
      <ul class="ul-dash">
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/' "
          >
            <b-icon icon="pentagon" class="mr-4"></b-icon>Home</b-link
          >
        </li>
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/projects' "
            ><b-icon icon="files" class="mr-4"></b-icon>Projects</b-link
          >
        </li>
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/invitations' "
          >
            <b-icon icon="person-plus" class="mr-4"></b-icon
            >Invitations</b-link
          >
        </li>
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/transactions' "
            ><b-icon icon="cash" class="mr-4"></b-icon>Transactions</b-link
          >
        </li>
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/referrals' "
            ><b-icon icon="people-fill" class="mr-4"></b-icon>Referrals</b-link
          >
        </li>
        <li>
          <b-link
            exact-active-class="active"
            :to=" path: '/dashboard/freelancer/resume' "
          >
            <b-icon icon="file-earmark-text" class="mr-4"></b-icon
            >Resume</b-link
          >
        </li>
      </ul>

      <div class="contact">
        <span style="color: gray"> Having troubles? </span>
        <p>Contact us</p>
      </div>
    </div>
  </div>
</b-collapse>

<div class="content">
  <b-navbar fixed="top" class="navbar">
  
    <b-navbar-brand href="#"
      ><img
        src="../../../public/images/fevicon.png"
        :class="visible ? null : 'collapsed'"
        :aria-expanded="visible ? 'true' : 'false'"
        aria-controls="collapse-4"
        @click="visible = !visible"
        style="
          width: 30px;
          margin-right: 10px;
          box-shadow: 1px 1px 3px #eaeaea;
          border-radius: 50%;
        "
      />KickStartup
    </b-navbar-brand>
    <b-collapse id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->

      <b-navbar-nav class="ml-auto">
        <!-- <b-nav-item href="#" class="nav-item"
          ><span>Top 1%</span></b-nav-item
        > -->

        <b-nav-item-dropdown right>
          <!-- Using 'button-content' slot -->
          <template #button-content>
            <img
              src="../../../public/images/profile.png"
              style="width: 25px"
            />
          </template>
          <b-dropdown-item to="/dashboard/freelancer/editprofile"
            >Profile</b-dropdown-item
          >
          <b-dropdown-item @click="logout">Sign Out</b-dropdown-item>
        </b-nav-item-dropdown>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>
  <div class="maincontent">
    <router-view></router-view>
  </div>
</div>

CSS -

    .sidebar 
  position: fixed;
  background-color: #181e36;
  border-radius: 0px;
  top: 0px;
  left: 0px;
  height: 100vh;
  width: 270px;
  z-index: 1000;
  overflow-x: hidden;
  overflow-y: auto;
  padding-left: 0;
  padding-right: 0;


.nav-item span 
  background-color: orange;
  padding: 5px 10px;
  border-radius: 10px;
  color: white;
  margin-right: 10px;


.navbar 
  margin-left: 0px;
  background-color: white !important;
  box-shadow: 0px 2px 5px 2px #efefef, 0px 2px 5px 2px #efefef;


.collapse.show ~ .content 
  margin-left: 280px;


.collapse.show ~ .content .navbar 
  margin-left: 272px;


.content 
  margin-top: 0px;
  margin-right: 5px;
  border-radius: 15px;


.maincontent 
  margin-top: 60px;

.active 
  color: white !important;


.ul-dash li a 
  color: #aeb1ba;


.ul-dash 
  position: relative;
  padding-top: 20px;
  margin: -10px;
  color: lightgray;

.ul-dash li 
  text-align: start;
  display: block;
  list-style: none;
  font-size: 17px;
  margin-bottom: 20px;


a:hover 
  text-decoration: none;


.ul-dash .active::after 
  content: "• "; /*don't miss the space*/
  color: #595cf5;
  font-size: 20px;
  margin-left: 45px;


.contact 
  margin: 20px;
  margin-top: 90px;
  text-align: start;
  background-color: #181e36;
  border-radius: 15px;
  color: white;
  font-size: 13px;
  padding-top: 10px;


.contact p 
  margin: 0px 15px;
  padding: 5px 0 10px 0;

.contact span 
  margin-left: 15px;

js-

  props: ,
  created() 
    HTTP.get("/freelancerdash/myprofile", 
      withCredentials: true,
    )
      .then((res) => 
        console.log(res);
        this.name = res.data.name;
      )
      .catch((errors) => 
        console.log("error hai");
      );
  ,
  data() 
    return 
      visible: true,
      name: "",
    ;
  ,
  methods: 
    ...mapActions(["logout"]),
  ,

【问题讨论】:

【参考方案1】:

&lt;b-collapse&gt;v-model 绑定是对v-model 指令的误解。 v-model 指令用于绑定表单元素,而不是显示/隐藏您需要使用 v-ifv-show、here you can learn the difference 指令的元素。可以看here文档深入理解v-model指令。

出于您的目的,请允许使用 bootsrap-vue 文档,而不是使用 &lt;b-collapse&gt; 组件,您可以尝试使用 &lt;b-sidebar&gt; 组件

<template>
  <div>
    <b-button v-b-toggle.sidebar-right>Toggle Sidebar</b-button>
    <b-sidebar id="sidebar-right" title="Sidebar" right shadow>
      <div class="px-3 py-2">
        <p>
          Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis
          in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
        </p>
        <b-img src="https://picsum.photos/500/500/?image=54" fluid thumbnail></b-img>
      </div>
    </b-sidebar>
  </div>
</template>

我认为这会如你所愿。

其他解决方案:

有 vuetify,它是一个材料 Vue UI 库,而确切地说 here 有一个组件可以按照您的要求工作。

【讨论】:

我已经给出了我关注的帖子的链接并且它正在工作,但是当点击汉堡时导航栏和侧边栏不同步,侧边栏应该通过滑动效果关闭 是的,等一下,我也在帖子中编辑它 好的,我不确定这是否能解决您的问题,但我看到您使用的是绑定&lt;b-collpase&gt; 的 v-model,这是一个错误,因为 v-model 是用于绑定输入或选择。我正在编辑我的答案来帮助你 @Dario 你对v-model 的说法不正确,可以将逻辑实现到自定义组件中,BootstrapVue 已经做到了。所以在&lt;b-collapse&gt; 上使用v-model 非常好。 docs 上甚至还有一个关于它的部分【参考方案2】:

/components/Menu/Burger.vue

<template>
<div id="burger" :class=" 'active' : isBurgerActive " @click.prevent="toggle">
    <slot>
        <div class="container-fluid">
            <div class="row">
                <button type="button" class="burger-button" title="Menu">
                    <span class="hidden">Toggle menu</span>
                    <span class="burger-bar burger-bar--1"></span>
                    <span class="burger-bar burger-bar--2"></span>
                    <span class="burger-bar burger-bar--3"></span>
                </button>
            </div>
        </div>
    </slot>
</div>
</template>
<script>
import  store, mutations  from '@/store';
export default 
    computed: 
        isBurgerActive() 
            return store.isNavOpen;
        
    ,
    methods: 
        toggle() 
            mutations.toggleNav();
        
    
;
</script>
<style>
.hidden 
    visibility: hidden;

button 
    cursor: pointer;

/* remove blue outline */
button:focus 
    outline: 0;

.burger-button 
    position: relative;
    height: 45px;
    width: 45px;
    display: block;
    z-index: 999;
    border: 2px solid #F56905;
    border-radius: 10px;
    background-color: transparent;
    pointer-events: all;
    transition: transform 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);

.burger-bar 
    background-color: #8cae42;
    position: absolute;
    top: 50%;
    right: 6px;
    left: 6px;
    height: 3px;
    width: auto;
    margin-top: -1px;
    transition: transform 0.6s cubic-bezier(0.165, 0.84, 0.44, 1),
    opacity 0.3s cubic-bezier(0.165, 0.84, 0.44, 1),
    background-color 0.6s cubic-bezier(0.165, 0.84, 0.44, 1);

.burger-bar--1 
    -webkit-transform: translateY(-6px);
    transform: translateY(-6px);

.burger-bar--2 
    transform-origin: 100% 50%;
    /*transform: scaleX(0.8);*/

.burger-button:hover .burger-bar--2 
    transform: scaleX(1);

.no-touchevents .burger-bar--2:hover 
    transform: scaleX(1);

.burger-bar--3 
    transform: translateY(6px);

#burger.active .burger-button 
    transform: rotate(-180deg);

#burger.active .burger-bar 
    background-color: #fff;

#burger.active .burger-bar--1 
    transform: rotate(45deg);

#burger.active .burger-bar--2 
    opacity: 0;

#burger.active .burger-bar--3 
    transform: rotate(-45deg);

.menutext
    margin-bottom: 0;
    line-height: 1;
    vertical-align: center;
    font-weight: bold;
    padding-left: 5%;
    padding-top: 5%;
    font-size: .9em;
    margin-top: auto;
    margin-bottom: auto;

</style>

/components/Menu/Sidebar.vue

<template>
<div class="sidebar">
    <div class="sidebar-backdrop" @click="closeSidebarPanel" v-if="isPanelOpen"></div>
    <transition name="slide">
        <div v-if="isPanelOpen"
             class="sidebar-panel">
            <slot></slot>
        </div>
    </transition>
</div>
</template>
<script>
import  store, mutations  from '@/store';

export default 
    methods: 
        closeSidebarPanel: mutations.toggleNav
    ,
    computed: 
        isPanelOpen() 
            return store.isNavOpen
        
    ,
    watch: 
        '$route' () 
            if(store.isNavOpen)
                this.closeSidebarPanel();
            
        
    

</script>
<style>
.slide-enter-active,
.slide-leave-active

    transition: transform 0.2s ease;


.slide-enter,
.slide-leave-to 
    transform: translateX(-100%);
    transition: all 150ms ease-in 0s


.sidebar-backdrop 
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    cursor: pointer;
    z-index: 1000;


.sidebar-panel 
    overflow-y: auto;
    position: fixed;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 999;
    padding: 3rem 20px 2rem 20px;
    width: 300px;
    z-index: 1001;

</style>

/components/Navigation.vue

<template>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-4 col-md-12">
            <nav class="main-nav">
                <Burger></Burger>
                <Sidebar>
                    <ul class="sidebar-panel-nav">
                        <li>
                            <router-link to="/">Home</router-link>
                        </li>
                        <li>
                            <router-link to="/about">About</router-link>
                        </li>
                        <li>
                            <router-link to="/contact">Contact Us</router-link>
                        </li>
                    </ul>
                </Sidebar>
            </nav>
        </div>
            <div class="col-lg-4 col-md-12">
              <h1>Hardik Rawat</h1>
            </div>
    </div>
</div>
</template>

<script>
import Burger from "@/components/Menu/Burger.vue";
import Sidebar from "@/components/Menu/Sidebar.vue";
export default 
    name: "Navigation",
    components: 
        Burger,
        Sidebar
    ,
    methods: 
        navHome() 
            this.$router.push( name: 'Home' );
        ,
    

</script>

<style>
.main-nav 
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0.8rem;

ul.sidebar-panel-nav 
    list-style-type: none;

ul.sidebar-panel-nav > li > a 
    color: #fff;
    text-decoration: none;
    font-size: 1.5rem;
    display: block;
    padding-bottom: 0.5em;

</style>

/App.vue

<template>
  <div id="app">
    <Navigation></Navigation>
    <main>
      <router-view></router-view>
    </main>
  </div>
</template>
<script>
import Navigation from "@/components/Navigation";
import  store, mutations  from '@/store'
components: 
  Navigation
  ,
methods: 
  closeSidebarPanel: mutations.toggleNav,
,
computed: 
  isPanelOpen() 
    return store.isNavOpen
  

</script>

/store.js

import Vue from 'vue';

export const store = Vue.observable(
  isNavOpen: false,
);

export const mutations = 
  setIsNavOpen(yesno) 
    store.isNavOpen = yesno;
  ,
toggleNav() 
    store.isNavOpen = !store.isNavOpen;
  ,
;

【讨论】:

【参考方案3】:

我建议你使用 Vuetify 并使用 Toolbar 组件。然后,您可以使用显示帮助器使其具有响应性。

标签内,我们可以使用如下代码:

这将在屏幕中等或更大时显示

            <div class="d-none d-md-block">
                <v-btn outlined @click="$router.push('/login')">
                    <span>
                        Login
                    </span>
                </v-btn>
            </div>

这将在屏幕小于中等时显示

            <div class="d-md-none">
                <v-btn icon @click.stop="drawer = !drawer">
                    <v-app-bar-nav-icon></v-app-bar-nav-icon>
                </v-btn>
            </div>

这是导航抽屉的代码

   <v-navigation-drawer
      v-model="drawer"
      absolute
      temporary
      right
    >
      <v-list-item>
        <v-list-item-content>
          <v-list-item-title>MENU</v-list-item-title>
        </v-list-item-content>
      </v-list-item>

      <v-divider></v-divider>

      <v-list dense>
        <v-list-item
          v-for="item in items"
          :key="item.title"
          :href="item.title"
          link
        >
          <v-list-item-icon>
            <v-icon> item.icon </v-icon>
          </v-list-item-icon>

          <v-list-item-content>
            <v-list-item-title> item.title </v-list-item-title>
          </v-list-item-content>
        </v-list-item>
      </v-list>
    </v-navigation-drawer>

其中 item.title 和 item.icon 由脚本中的 data() 函数返回

链接:

Vuetify Toolbar Component

Vuetify Display Helpers

【讨论】:

以上是关于vuejs 中带有侧边栏的响应式导航栏( bootstrap 或 bootstrap-vue )的主要内容,如果未能解决你的问题,请参考以下文章

VueJS + Vuex + Vuetify 导航抽屉

带有 jQ​​uery 和 Bootstrap 3 的可折叠响应式侧边栏菜单

Vue 收到了一个组件,它是一个响应式对象

在css中设置主要内容相对于响应侧边栏宽度的边距[重复]

vue自适应导航栏

在 React 中动态更新导航栏和侧边栏的最佳方法是啥?