当前位置:首页 > VUE

用vue实现弹窗抖动

2026-01-20 01:42:17VUE

使用Vue实现弹窗抖动效果

弹窗抖动是一种常见的交互效果,可以通过CSS动画或JavaScript动态样式实现。以下是两种在Vue中实现弹窗抖动的方案:

用vue实现弹窗抖动

方案一:CSS动画实现

创建一个抖动的CSS动画关键帧,通过Vue动态绑定类名触发动画。

用vue实现弹窗抖动

<template>
  <div class="modal" :class="{ 'shake-animation': isShaking }" @click="stopShaking">
    点击停止抖动
  </div>
  <button @click="startShaking">开始抖动</button>
</template>

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

<style>
.modal {
  width: 200px;
  height: 100px;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px auto;
}

.shake-animation {
  animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) infinite;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}
</style>

方案二:JavaScript动态样式

通过ref获取DOM元素,使用setInterval动态修改样式实现抖动。

<template>
  <div ref="modal" class="modal" @click="stopShaking">
    点击停止抖动
  </div>
  <button @click="startShaking">开始抖动</button>
</template>

<script>
export default {
  methods: {
    startShaking() {
      this.shakeInterval = setInterval(() => {
        const randomX = (Math.random() * 10) - 5
        this.$refs.modal.style.transform = `translateX(${randomX}px)`
      }, 50)
    },
    stopShaking() {
      clearInterval(this.shakeInterval)
      this.$refs.modal.style.transform = 'translateX(0)'
    }
  },
  beforeUnmount() {
    clearInterval(this.shakeInterval)
  }
}
</script>

<style>
.modal {
  width: 200px;
  height: 100px;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px auto;
  transition: transform 0.1s;
}
</style>

方案三:使用第三方库

如果需要更复杂的动画效果,可以考虑使用动画库如Animate.css或GSAP。

<template>
  <div class="modal animate__animated" :class="{ 'animate__headShake': isShaking }">
    抖动弹窗内容
  </div>
  <button @click="toggleShaking">切换抖动</button>
</template>

<script>
import 'animate.css'
export default {
  data() {
    return {
      isShaking: false
    }
  },
  methods: {
    toggleShaking() {
      this.isShaking = !this.isShaking
    }
  }
}
</script>

以上三种方案均可实现弹窗抖动效果,CSS动画方案性能最佳,JavaScript方案控制更灵活,第三方库方案实现最简单。根据项目需求选择合适的实现方式。

标签: vue
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

vue.js实现轮播的方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入Swiper: <template>…

vue实现tab标签

vue实现tab标签

Vue 实现 Tab 标签的方法 使用动态组件和 v-for 渲染标签 通过 v-for 循环生成标签页标题,结合 v-bind:class 动态切换激活状态。使用 v-show 或动态组件 <…

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供的…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

前端多线程实现vue

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…