当前位置:首页 > 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 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

前端多线程实现vue

前端多线程实现vue

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

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…