当前位置:首页 > VUE

vue实现声光弹窗

2026-01-15 02:28:48VUE

Vue 实现声光弹窗的方法

基础弹窗组件

创建基础弹窗组件 AlertModal.vue,包含动画和样式:

<template>
  <transition name="fade">
    <div v-if="show" class="alert-modal" :class="{'flash': isFlashing}">
      <div class="modal-content">
        <slot></slot>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  props: {
    show: Boolean,
    flash: Boolean
  },
  computed: {
    isFlashing() {
      return this.flash
    }
  }
}
</script>

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

.alert-modal {
  position: fixed;
  top: 20%;
  left: 50%;
  transform: translateX(-50%);
  padding: 20px;
  background: #ff4757;
  color: white;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(255, 71, 87, 0.7);
  z-index: 1000;
}

.flash {
  animation: flash 0.5s infinite alternate;
}

@keyframes flash {
  from { box-shadow: 0 0 10px rgba(255, 71, 87, 0.7); }
  to { box-shadow: 0 0 30px rgba(255, 71, 87, 1); }
}
</style>

声音控制逻辑

在调用弹窗时加入声音效果:

vue实现声光弹窗

// 在工具文件中定义声音播放方法
export function playAlertSound() {
  const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3')
  audio.volume = 0.3
  audio.play().catch(e => console.log('Audio play failed:', e))
}

组合使用

在父组件中使用弹窗和声音:

<template>
  <button @click="triggerAlert">触发警报</button>
  <AlertModal :show="showAlert" :flash="true">
    警告!发现异常情况!
  </AlertModal>
</template>

<script>
import AlertModal from './AlertModal.vue'
import { playAlertSound } from './alertUtils'

export default {
  components: { AlertModal },
  data() {
    return {
      showAlert: false
    }
  },
  methods: {
    triggerAlert() {
      this.showAlert = true
      playAlertSound()
      setTimeout(() => {
        this.showAlert = false
      }, 3000)
    }
  }
}
</script>

增强效果选项

  1. 震动反馈(需要用户授权):

    vue实现声光弹窗

    function triggerVibration() {
    if ('vibrate' in navigator) {
     navigator.vibrate([200, 100, 200])
    }
    }
  2. 自定义音效:

    
    const soundMap = {
    warning: 'warning-sound.mp3',
    success: 'success-sound.mp3'
    }

export function playCustomSound(type) { const audio = new Audio(soundMap[type]) audio.play() }


#### 无障碍支持
为弹窗添加ARIA属性:
```vue
<div class="alert-modal" role="alert" aria-live="assertive">
  <!-- 内容 -->
</div>

标签: 声光vue
分享给朋友:

相关文章

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scre…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…