当前位置:首页 > VUE

vue实现左侧滑出效果

2026-02-22 06:52:24VUE

使用 Vue 实现左侧滑出效果

方法一:使用 CSS Transition 和 Vue 数据绑定

通过 Vue 的 v-bind 绑定 classstyle,结合 CSS 的 transition 实现平滑滑动效果。

<template>
  <div>
    <button @click="toggleSidebar">Toggle Sidebar</button>
    <div class="sidebar" :class="{ 'active': isActive }">
      <!-- 侧边栏内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: false
    };
  },
  methods: {
    toggleSidebar() {
      this.isActive = !this.isActive;
    }
  }
};
</script>

<style>
.sidebar {
  position: fixed;
  left: -250px;
  width: 250px;
  height: 100vh;
  background: #333;
  transition: left 0.3s ease;
}
.sidebar.active {
  left: 0;
}
</style>

方法二:使用 Vue Transition 组件

vue实现左侧滑出效果

利用 Vue 内置的 <transition> 组件实现动画效果,支持更复杂的动画钩子。

<template>
  <div>
    <button @click="showSidebar = !showSidebar">Toggle Sidebar</button>
    <transition name="slide">
      <div class="sidebar" v-if="showSidebar">
        <!-- 侧边栏内容 -->
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showSidebar: false
    };
  }
};
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: transform 0.3s ease;
}
.slide-enter, .slide-leave-to {
  transform: translateX(-100%);
}
.sidebar {
  position: fixed;
  left: 0;
  width: 250px;
  height: 100vh;
  background: #333;
}
</style>

方法三:结合第三方库(如 VueUse)

vue实现左侧滑出效果

使用 @vueuse/coreuseTransition 实现更灵活的动画控制。

<template>
  <div>
    <button @click="toggleSidebar">Toggle Sidebar</button>
    <div class="sidebar" :style="{ left: `${left}px` }">
      <!-- 侧边栏内容 -->
    </div>
  </div>
</template>

<script>
import { useTransition } from '@vueuse/core';
export default {
  data() {
    return {
      isActive: false
    };
  },
  setup() {
    const left = useTransition(0, {
      duration: 300,
      transition: [0.25, 0.1, 0.25, 1] // 缓动函数
    });
    return { left };
  },
  methods: {
    toggleSidebar() {
      this.isActive = !this.isActive;
      this.left = this.isActive ? 0 : -250;
    }
  }
};
</script>

<style>
.sidebar {
  position: fixed;
  width: 250px;
  height: 100vh;
  background: #333;
}
</style>

方法四:响应式适配移动端

添加触摸事件支持,优化移动端体验。

<template>
  <div>
    <div 
      class="sidebar" 
      :class="{ 'active': isActive }"
      @touchstart="startDrag"
      @touchmove="onDrag"
      @touchend="endDrag"
    >
      <!-- 侧边栏内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isActive: false,
      startX: 0,
      currentX: 0
    };
  },
  methods: {
    startDrag(e) {
      this.startX = e.touches[0].clientX;
    },
    onDrag(e) {
      this.currentX = e.touches[0].clientX - this.startX;
      if (this.currentX > 0) {
        this.currentX = 0;
      }
    },
    endDrag() {
      if (this.currentX < -50) {
        this.isActive = false;
      } else {
        this.isActive = true;
      }
      this.currentX = 0;
    }
  }
};
</script>

<style>
.sidebar {
  position: fixed;
  left: -250px;
  width: 250px;
  height: 100vh;
  background: #333;
  transition: transform 0.3s ease;
}
.sidebar.active {
  transform: translateX(250px);
}
</style>

注意事项

  • 性能优化:避免在动画中使用 left 属性,优先使用 transform
  • 遮罩层:可添加半透明遮罩层增强用户体验。
  • 浏览器兼容性:测试不同浏览器的 transitiontransform 支持情况。

标签: 滑出效果
分享给朋友:

相关文章

vue实现玻璃效果

vue实现玻璃效果

实现玻璃效果的方法 在Vue中实现玻璃效果(毛玻璃或磨砂玻璃)可以通过CSS的backdrop-filter属性结合Vue的动态样式绑定来实现。以下是具体步骤: 使用CSS backdrop-fil…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…

vue实现翻牌效果

vue实现翻牌效果

实现翻牌效果的基本思路 翻牌效果通常指卡片正面和背面的翻转动画,可以通过CSS 3D变换和Vue的过渡系统实现。核心是利用transform-style: preserve-3d和rotateY旋转完…

vue实现菜单效果

vue实现菜单效果

Vue实现菜单效果的方法 基础菜单实现 使用Vue的v-for指令和v-show/v-if可以快速实现基础菜单。通过数据驱动的方式渲染菜单项,结合事件处理实现交互。 <template>…

vue实现钟表效果

vue实现钟表效果

Vue 实现钟表效果 创建基础结构 在 Vue 项目中创建一个组件,用于显示钟表。组件的模板部分包含钟表的外观,如时针、分针、秒针和钟面。 <template> <div cl…

vue实现点击效果

vue实现点击效果

实现点击效果的方法 在Vue中实现点击效果可以通过多种方式完成,以下是几种常见的实现方法: 使用v-on或@click绑定事件 通过v-on:click或简写@click绑定点击事件,触发方法或直…