当前位置:首页 > VUE

vue实现声光弹窗

2026-01-08 15:27:29VUE

Vue 实现声光弹窗的方法

使用 Vue 自定义组件

创建一个独立的弹窗组件,包含声光效果。通过 v-ifv-show 控制弹窗显示,利用 CSS 动画实现闪光效果,通过 Audio API 播放声音。

<template>
  <div class="alert-modal" v-if="show">
    <div class="alert-content">
      <h3>{{ title }}</h3>
      <p>{{ message }}</p>
    </div>
  </div>
</template>

<script>
export default {
  props: ['show', 'title', 'message'],
  watch: {
    show(newVal) {
      if (newVal) {
        this.playSound();
        this.flashEffect();
      }
    }
  },
  methods: {
    playSound() {
      const audio = new Audio('notification.mp3');
      audio.play();
    },
    flashEffect() {
      const modal = document.querySelector('.alert-modal');
      modal.classList.add('flash');
      setTimeout(() => modal.classList.remove('flash'), 500);
    }
  }
};
</script>

<style>
.alert-modal {
  position: fixed;
  top: 20%;
  left: 50%;
  transform: translateX(-50%);
  padding: 20px;
  background: white;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
  z-index: 1000;
}

.flash {
  animation: flash 0.5s;
}

@keyframes flash {
  0% { box-shadow: 0 0 5px 5px rgba(255, 255, 0, 0.5); }
  50% { box-shadow: 0 0 20px 10px rgba(255, 255, 0, 0.8); }
  100% { box-shadow: 0 0 5px 5px rgba(255, 255, 0, 0.5); }
}
</style>

使用第三方库

对于更复杂的效果,可以考虑使用专门的动画库如 GSAPAnimate.css 来实现更专业的声光效果。

安装 Animate.css

npm install animate.css

在 Vue 组件中使用:

<template>
  <div class="animated flash" v-if="show">
    <!-- 弹窗内容 -->
  </div>
</template>

<script>
import 'animate.css';
export default {
  // 组件逻辑
}
</script>

浏览器通知 API

结合浏览器通知 API 实现系统级的声光提示:

methods: {
  showNotification() {
    if (!("Notification" in window)) {
      alert("浏览器不支持通知");
      return;
    }

    if (Notification.permission === "granted") {
      this.createNotification();
    } else if (Notification.permission !== "denied") {
      Notification.requestPermission().then(permission => {
        if (permission === "granted") {
          this.createNotification();
        }
      });
    }
  },

  createNotification() {
    const notification = new Notification("新消息", {
      body: "您有一条新消息",
      icon: "/notification-icon.png"
    });

    // 播放声音
    const audio = new Audio('/notification.mp3');
    audio.play();

    // 点击通知关闭
    notification.onclick = () => {
      window.focus();
      notification.close();
    };
  }
}

响应式设计考虑

确保弹窗在不同设备上都能正常显示:

@media (max-width: 768px) {
  .alert-modal {
    width: 90%;
    top: 10%;
  }
}

无障碍访问

为弹窗添加适当的 ARIA 属性,确保屏幕阅读器用户可以感知:

<div class="alert-modal" v-if="show" role="alertdialog" aria-labelledby="alert-title" aria-describedby="alert-message">
  <h3 id="alert-title">{{ title }}</h3>
  <p id="alert-message">{{ message }}</p>
</div>

vue实现声光弹窗

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

相关文章

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm ins…

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addToC…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…