当前位置:首页 > VUE

vue实现删除特效

2026-02-20 06:29:45VUE

vue实现删除特效的方法

使用CSS过渡动画

通过Vue的<transition>组件结合CSS过渡效果实现删除动画。在元素被移除时触发动画效果。

<template>
  <div>
    <button @click="removeItem">删除</button>
    <transition name="fade">
      <div v-if="show" class="item">要删除的内容</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    removeItem() {
      this.show = false
    }
  }
}
</script>

<style>
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-leave-to {
  opacity: 0;
}
</style>

使用Animate.css库

利用第三方动画库Animate.css快速实现丰富的删除动画效果。

vue实现删除特效

<template>
  <div>
    <button @click="removeItem">删除</button>
    <transition
      enter-active-class="animate__animated animate__fadeIn"
      leave-active-class="animate__animated animate__fadeOut"
    >
      <div v-if="show" class="item">内容</div>
    </transition>
  </div>
</template>

<script>
import 'animate.css'
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    removeItem() {
      this.show = false
    }
  }
}
</script>

使用JavaScript钩子实现复杂动画

通过Vue过渡的JavaScript钩子实现更复杂的删除动画逻辑。

vue实现删除特效

<template>
  <div>
    <button @click="removeItem">删除</button>
    <transition
      @before-leave="beforeLeave"
      @leave="leave"
      @after-leave="afterLeave"
    >
      <div v-if="show" class="item" ref="item">内容</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    removeItem() {
      this.show = false
    },
    beforeLeave(el) {
      el.style.opacity = 1
    },
    leave(el, done) {
      let opacity = 1
      const timer = setInterval(() => {
        opacity -= 0.1
        el.style.opacity = opacity
        if (opacity <= 0) {
          clearInterval(timer)
          done()
        }
      }, 50)
    },
    afterLeave() {
      console.log('动画完成')
    }
  }
}
</script>

列表删除动画

使用<transition-group>为列表项添加删除动画效果。

<template>
  <div>
    <button @click="removeItem">删除最后一项</button>
    <transition-group name="list" tag="ul">
      <li v-for="item in items" :key="item.id" class="list-item">
        {{ item.text }}
      </li>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: '项目1' },
        { id: 2, text: '项目2' },
        { id: 3, text: '项目3' }
      ]
    }
  },
  methods: {
    removeItem() {
      this.items.pop()
    }
  }
}
</script>

<style>
.list-item {
  transition: all 0.5s;
}
.list-leave-active {
  position: absolute;
}
.list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

结合GSAP实现高级动画

使用GSAP动画库创建更流畅的删除动画效果。

<template>
  <div>
    <button @click="removeItem">删除</button>
    <transition
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-if="show" class="item" ref="item">内容</div>
    </transition>
  </div>
</template>

<script>
import gsap from 'gsap'
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    removeItem() {
      this.show = false
    },
    enter(el, done) {
      gsap.from(el, {
        opacity: 0,
        y: 50,
        duration: 0.5,
        onComplete: done
      })
    },
    leave(el, done) {
      gsap.to(el, {
        opacity: 0,
        x: 100,
        duration: 0.5,
        onComplete: done
      })
    }
  }
}
</script>

每种方法适用于不同场景,从简单的CSS过渡到复杂的JavaScript动画,可根据项目需求选择合适的实现方式。

标签: 特效vue
分享给朋友:

相关文章

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue实现拍照

vue实现拍照

Vue实现拍照功能 在Vue中实现拍照功能通常需要结合浏览器的MediaDevices API和Canvas元素。以下是具体实现方法: 安装依赖(可选) 如果需要更简单的封装,可以安装vue-web…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…