当前位置:首页 > 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 内置的 <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)

使用 @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>

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

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

vue实现左侧滑出效果

<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实现框架效果的方法 使用Vue实现框架效果可以通过多种方式完成,包括使用Vue Router、动态组件或第三方UI库。以下是几种常见的方法: 使用Vue Router实现布局框架 Vue Ro…

vue实现tag效果

vue实现tag效果

实现基本标签结构 使用 Vue 的模板语法创建标签容器和单个标签元素。通过 v-for 动态渲染标签列表,结合 v-bind:key 确保渲染性能。 <template> <…

vue实现滑动效果

vue实现滑动效果

Vue 实现滑动效果的方法 在 Vue 中实现滑动效果可以通过多种方式,包括使用 CSS 过渡、第三方动画库或直接操作 DOM。以下是几种常见的方法: 使用 CSS 过渡和 Vue 的 <tr…

vue实现tab效果

vue实现tab效果

Vue 实现 Tab 效果的方法 使用动态组件和 v-if 通过 v-if 或 v-show 控制不同标签内容的显示与隐藏,结合点击事件切换当前激活的标签页。 <template>…

vue实现日历效果

vue实现日历效果

Vue实现日历效果的方法 使用第三方库 Vue生态中有多个成熟的日历组件库,如v-calendar或vue2-calendar,可以快速实现日历功能。安装后直接引入组件即可使用。 npm insta…

vue实现菜单效果

vue实现菜单效果

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