当前位置:首页 > VUE

vue实现侧边栏抽屉

2026-02-22 14:15:00VUE

vue实现侧边栏抽屉

使用 Vue 实现侧边栏抽屉

基础实现(基于 Vue 3 + Composition API)

<template>
  <div class="drawer-container">
    <button @click="toggleDrawer">Toggle Drawer</button>
    <div class="drawer" :class="{ 'drawer-open': isOpen }">
      <div class="drawer-content">
        <slot></slot>
      </div>
    </div>
    <div 
      class="overlay" 
      :class="{ 'overlay-visible': isOpen }"
      @click="toggleDrawer"
    ></div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const isOpen = ref(false);

const toggleDrawer = () => {
  isOpen.value = !isOpen.value;
};
</script>

<style scoped>
.drawer-container {
  position: relative;
}

.drawer {
  position: fixed;
  top: 0;
  left: -300px;
  width: 300px;
  height: 100vh;
  background: white;
  box-shadow: 2px 0 5px rgba(0,0,0,0.1);
  transition: left 0.3s ease;
  z-index: 1000;
}

.drawer-open {
  left: 0;
}

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 999;
}

.overlay-visible {
  opacity: 1;
  pointer-events: all;
}
</style>

使用 Vue Transition 添加动画效果

<template>
  <div>
    <button @click="isOpen = !isOpen">Toggle</button>

    <Transition name="slide">
      <div v-if="isOpen" class="sidebar">
        <!-- 侧边栏内容 -->
      </div>
    </Transition>
  </div>
</template>

<style>
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.3s ease;
}

.slide-enter-from,
.slide-leave-to {
  transform: translateX(-100%);
}
</style>

响应式侧边栏实现

<template>
  <div class="responsive-drawer">
    <div 
      class="drawer"
      :class="{
        'mobile-drawer': isMobile,
        'desktop-drawer': !isMobile,
        'open': isOpen
      }"
    >
      <!-- 内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isMobile: false,
      isOpen: false
    };
  },
  mounted() {
    this.checkScreenSize();
    window.addEventListener('resize', this.checkScreenSize);
  },
  methods: {
    checkScreenSize() {
      this.isMobile = window.innerWidth < 768;
      if (!this.isMobile) {
        this.isOpen = true;
      }
    }
  }
};
</script>

<style>
.mobile-drawer {
  position: fixed;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
}

.mobile-drawer.open {
  transform: translateX(0);
}

.desktop-drawer {
  position: relative;
  width: 250px;
}
</style>

使用 Vue Router 集成

// router.js
const routes = [
  {
    path: '/',
    component: () => import('@/layouts/MainLayout.vue'),
    children: [
      {
        path: '',
        component: () => import('@/views/Home.vue')
      }
    ]
  }
];
<!-- MainLayout.vue -->
<template>
  <div class="app-layout">
    <AppDrawer :is-open="drawerOpen" @close="drawerOpen = false"/>
    <div class="main-content">
      <router-view/>
    </div>
  </div>
</template>

可复用组件实现

// Drawer.vue
export default {
  props: {
    position: {
      type: String,
      default: 'left',
      validator: value => ['left', 'right', 'top', 'bottom'].includes(value)
    },
    width: {
      type: String,
      default: '300px'
    },
    closeOnClickOutside: {
      type: Boolean,
      default: true
    }
  },
  // 其余实现...
};
/* 根据位置调整样式 */
.drawer-left {
  top: 0;
  left: 0;
  height: 100vh;
  width: v-bind(width);
  transform: translateX(-100%);
}

.drawer-right {
  top: 0;
  right: 0;
  height: 100vh;
  width: v-bind(width);
  transform: translateX(100%);
}

这些实现方式涵盖了从基础到高级的各种侧边栏抽屉场景,可以根据具体需求选择合适的实现方案。

vue实现侧边栏抽屉

标签: 侧边抽屉
分享给朋友:

相关文章

vue实现侧边栏

vue实现侧边栏

Vue 侧边栏实现方法 基础结构搭建 使用Vue Router和动态组件可以实现一个灵活的侧边栏。以下是一个基础模板结构: <template> <div class="app…

vue实现侧边栏抽屉

vue实现侧边栏抽屉

使用Vue实现侧边栏抽屉 在Vue中实现侧边栏抽屉效果可以通过多种方式完成,以下是几种常见的方法: 基于Vue和CSS过渡动画 通过Vue的v-if或v-show控制侧边栏显示隐藏,结合CSS过渡动…

vue侧边栏的实现

vue侧边栏的实现

Vue 侧边栏实现方法 基础布局与样式 使用Vue结合CSS实现侧边栏的基础布局。通过v-show或v-if控制侧边栏的显示与隐藏,结合CSS过渡效果实现平滑动画。 <template>…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 在 Vue 中实现抽屉效果可以通过多种方式完成,以下是几种常见的方法: 使用第三方库(如 Element UI) Element UI 提供了现成的抽屉组件 el-drawer…

vue实现侧边导航

vue实现侧边导航

Vue 侧边导航实现方法 使用 Vue 实现侧边导航可以通过多种方式完成,以下提供几种常见方案: 基于 Element UI 的侧边导航 Element UI 提供了现成的导航组件,适合快速开发:…

vue实现侧边菜单

vue实现侧边菜单

Vue 侧边菜单实现方法 使用 Element UI 的 el-menu 组件 安装 Element UI: npm install element-ui 在 main.js 中引入: impor…