当前位置:首页 > VUE

vue实现声光弹窗

2026-02-11 05:29:26VUE

Vue 实现声光弹窗的方法

基础弹窗组件

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

<template>
  <transition name="fade">
    <div v-if="visible" class="alert-modal" :class="type">
      <div class="content">{{ message }}</div>
    </div>
  </transition>
</template>

<script>
export default {
  props: {
    visible: Boolean,
    message: String,
    type: { type: String, default: 'info' } // 支持 success/warning/error 等类型
  }
}
</script>

<style scoped>
.alert-modal {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0,0,0,0.2);
  animation: pulse 1.5s infinite;
}

.success { background-color: #4CAF50; color: white; }
.warning { background-color: #FFC107; color: black; }
.error { background-color: #F44336; color: white; }

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

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.7); }
  70% { box-shadow: 0 0 0 10px rgba(76, 175, 80, 0); }
  100% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0); }
}
</style>

添加声音效果

在组件挂载时播放提示音:

<script>
export default {
  // ...其他代码
  mounted() {
    const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3');
    audio.play().catch(e => console.log("Audio playback failed:", e));
  }
}
</script>

全局调用方式

创建插件式调用方法(在 main.js 中注册):

// alertPlugin.js
const AlertPlugin = {
  install(Vue) {
    const AlertConstructor = Vue.extend(AlertModal)
    const alertInstance = new AlertConstructor()
    document.body.appendChild(alertInstance.$mount().$el)

    Vue.prototype.$alert = (message, type = 'info', duration = 3000) => {
      alertInstance.message = message
      alertInstance.type = type
      alertInstance.visible = true

      setTimeout(() => {
        alertInstance.visible = false
      }, duration)
    }
  }
}

// main.js
import AlertPlugin from './alertPlugin'
Vue.use(AlertPlugin)

使用方法

在任意组件中触发:

this.$alert('操作成功!', 'success')
// 或
this.$alert('发生错误', 'error', 5000)

高级定制选项

  1. 添加更多动画效果:

    .alert-modal {
    transform: translateY(-20px);
    transition: transform 0.3s ease-out;
    }
    .alert-modal.show {
    transform: translateY(0);
    }
  2. 支持自定义音效:

    Vue.prototype.$alert = (message, options = {}) => {
    if (options.sound) {
     new Audio(options.sound).play()
    }
    // ...其他逻辑
    }
  3. 响应式位置调整:

    vue实现声光弹窗

    @media (max-width: 768px) {
    .alert-modal {
     width: 90%;
     left: 5%;
     right: auto;
    }
    }

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

相关文章

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…

vue滑块实现

vue滑块实现

Vue滑块实现方法 使用原生HTML5 range input Vue中可以绑定原生HTML5的range类型input元素实现基础滑块功能: <template> <div&…