当前位置:首页 > 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 支持情况。

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

相关文章

js实现点击效果

js实现点击效果

实现点击效果的JavaScript方法 使用addEventListener绑定点击事件 通过document.getElementById或document.querySelector获取DOM元素…

vue实现框选效果

vue实现框选效果

实现框选效果的基本思路 在Vue中实现框选效果,通常需要监听鼠标事件,记录起始和结束位置,动态绘制一个矩形框,并根据框选范围判断哪些元素被选中。以下是具体实现方法: 监听鼠标事件 在Vue组件的模板…

vue实现multiselect效果

vue实现multiselect效果

Vue 实现多选(Multiselect)效果 使用第三方库 vue-multiselect 安装 vue-multiselect 库: npm install vue-multiselec…

vue实现导航栏效果

vue实现导航栏效果

Vue 实现导航栏效果的方法 基础导航栏结构 使用 Vue 的模板语法构建导航栏的基本 HTML 结构,通常包含 <nav> 和 <ul> 元素。通过 v-for 动态渲染导航…

vue实现定时效果

vue实现定时效果

使用 setInterval 实现定时器 在 Vue 中可以通过 setInterval 实现定时效果,适用于周期性任务。在组件的 mounted 钩子中启动定时器,并在 beforeDestroy…

vue实现标签多选效果

vue实现标签多选效果

实现标签多选效果的方法 在Vue中实现标签多选效果可以通过多种方式完成,以下是几种常见的实现方法: 使用v-model和数组绑定 通过v-model绑定一个数组,当选中标签时,将标签的值添加到数组…