当前位置:首页 > 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实现定位效果

使用Geolocation API实现定位 在Vue中可以通过浏览器内置的Geolocation API获取用户位置信息。需要在组件的mounted钩子中调用相关方法: mounted() {…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue实现动画效果

vue实现动画效果

Vue 实现动画效果的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及 CSS 动画。以下是几种常见的方法: 使用 Vue 内置的过渡系统 Vue 的 <tra…

vue 实现滑动效果

vue 实现滑动效果

Vue 实现滑动效果的方法 使用 CSS Transition 通过 Vue 的 v-if 或 v-show 结合 CSS Transition 实现滑动效果。定义一个 CSS 类,利用 transf…

vue实现taggle效果

vue实现taggle效果

Vue实现Toggle效果 在Vue中实现Toggle效果可以通过多种方式完成,以下是几种常见的方法: 方法1:使用v-model和v-show/v-if 通过v-model绑定一个布尔值,结合v…