Vue2全局提示(Message)

简介: 这篇文章介绍了如何在Vue 3框架中创建一个全局提示组件(Message),支持自定义提示的持续时间和位置,并且可以展示多种类型(info, success, error, warn)的消息提示。

可自定义设置以下属性:

  • 全局提示自动关闭的延时时长(duration)单位ms,默认为3000ms
  • 消息提示距离顶部的位置(top)单位像素px,默认为30px

调用一次只展示一个提示,若调用多次则依次展示多个提示

效果如下图:

info()调用:

success()调用:

error()调用:

warn()调用:

①创建全局提示组件Message:

<template>
  <div class="m-message-wrap" :style="`top: ${top}px;`">
    <div class="m-message" v-for="(content, index) in messageContent" :key="index">
      <transition name="slide-fade" :key="index">
        <div class="m-message-content" @mouseenter="onEnter(index)" @mouseleave="onLeave(index)" v-if="showMessage[index]">
          <svg class="svg" v-if="mode==='info'" :style="{fill: colorStyle[mode] }" viewBox="64 64 896 896" data-icon="info-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm32 664c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V456c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272zm-32-344a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"></path></svg>
          <svg class="svg" v-if="mode==='success'" :style="{fill: colorStyle[mode] }" viewBox="64 64 896 896" data-icon="check-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm193.5 301.7l-210.6 292a31.8 31.8 0 0 1-51.7 0L318.5 484.9c-3.8-5.3 0-12.7 6.5-12.7h46.9c10.2 0 19.9 4.9 25.9 13.3l71.2 98.8 157.2-218c6-8.3 15.6-13.3 25.9-13.3H699c6.5 0 10.3 7.4 6.5 12.7z"></path></svg>
          <svg class="svg" v-if="mode==='error'" :style="{fill: colorStyle[mode] }" viewBox="64 64 896 896" data-icon="close-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm165.4 618.2l-66-.3L512 563.4l-99.3 118.4-66.1.3c-4.4 0-8-3.5-8-8 0-1.9.7-3.7 1.9-5.2l130.1-155L340.5 359a8.32 8.32 0 0 1-1.9-5.2c0-4.4 3.6-8 8-8l66.1.3L512 464.6l99.3-118.4 66-.3c4.4 0 8 3.5 8 8 0 1.9-.7 3.7-1.9 5.2L553.5 514l130 155c1.2 1.5 1.9 3.3 1.9 5.2 0 4.4-3.6 8-8 8z"></path></svg>
          <svg class="svg" v-if="mode==='warn'" :style="{fill: colorStyle[mode] }" viewBox="64 64 896 896" data-icon="exclamation-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm-32 232c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V296zm32 440a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"></path></svg>
          <p class="content">{
  
  { content }}</p>
        </div>
      </transition>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Message',
  props: {
    duration: { // 自动关闭的延时,单位ms
      type: Number,
      default: 3000
    },
    top: { // 消息距离顶部的位置,单位px
      type: Number,
      default: 30
    }
  },
  data () {
    return {
      colorStyle: { // 颜色主题对象
        info: '#1890FF',
        success: '#52c41a',
        error: '#f5222d',
        warn: '#faad14'
      },
      mode: 'info',
      resetTimer: null,
      showMessage: [],
      hideTimers: [],
      messageContent: []
    }
  },
  computed: {
    clear () { // 所有提示是否已经全部变为false
      return this.showMessage.every(item => !item)
    }
  },
  watch: {
    clear (to, from) { // 所有提示都消失后重置
      if (!from && to) {
        this.resetTimer = setTimeout(() => {
          this.messageContent.splice(0)
          this.showMessage.splice(0)
        }, 500)
      }
    }
  },
  methods: {
    onEnter (index) {
      clearTimeout(this.hideTimers[index])
    },
    onLeave (index) {
      this.onHideMessage(index)
    },
    show (content) {
      clearTimeout(this.resetTimer)
      this.messageContent.push(content)
      const index = this.messageContent.length - 1
      console.log('index:', index)
      this.$nextTick(() => { // 待异步更新队列之后显示提示框,否则过渡效果会异常
        this.$set(this.showMessage, index, true)
        this.onHideMessage(index)
      })
    },
    info (content) {
      this.mode = 'info'
      this.show(content)
    },
    success (content) {
      this.mode = 'success'
      this.show(content)
    },
    error (content) {
      this.mode = 'error'
      this.show(content)
    },
    warn (content) {
      this.mode = 'warn'
      this.show(content)
    },
    onHideMessage (index) {
      this.hideTimers[index] = setTimeout(() => {
        this.$set(this.showMessage, index, false)
      }, this.duration)
    }
  }
}
</script>
<style lang="less" scoped>
// 渐变过渡效果
.fade-enter-active, .fade-leave-active {
  transition: opacity .3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
// 滑动渐变过渡效果
.slide-fade-enter-active {
  transition: all .5s ease;
}
.slide-fade-leave-active {
  transition: all .5s ease;
}
.slide-fade-enter, .slide-fade-leave-to {
  transform: translateY(-16px);
  -ms-transform: translateY(-16px); /* IE 9 */
  -webkit-transform: translateY(-16px); /* Safari and Chrome */
  opacity: 0;
}
.m-message-wrap {
  position: fixed;
  z-index: 999; // 突出显示该层级
  width: 100vw;
  left: 0;
  right: 0;
  pointer-events: none; // 保证整个message区域不遮挡背后元素响应鼠标事件
  .m-message {
    text-align: center;
    .m-message-content {
      margin: 8px 0;
      display: inline-block;
      padding: 10px 16px;
      background: #FFF;
      border-radius: 4px;
      box-shadow: 0 4px 12px rgba(0,0,0,.15);
      pointer-events: auto; // 保证内容区域部分可以正常响应鼠标事件
      .svg {
        width: 16px;
        height: 16px;
        margin-right: 8px;
        position: relative;
        top: 2px;
      }
      .content {
        display: inline-block;
        font-size: 14px;
        color: rgba(0,0,0,.65);
        line-height: 20px;
      }
    }
  }
}
</style>

②在要使用的页面引入:

<div class="m-tag yellow" @click="onShowMessage('This is a normal message')">哥斯拉</div>
<Message ref="message" :duration="3000" :top="30" />
import Message from './Message'
components: {
    Message
},
onShowMessage (content) {
      this.$refs.message.info(content) // info调用
      // this.$refs.message.success(content) // success调用
      // this.$refs.message.error(content) // error调用
      // this.$refs.message.warn(content) // warning调用
}
.m-tag {
  margin-top: 120px;
  display: inline-block;
  padding: 6px 12px;
  border-radius: 15px;
  height: 20px;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  cursor: pointer;
}
.yellow {
  color: #FF9000;
  background: #FFF9F0;
}
相关文章
|
缓存
若依 this.$router.push 同地址不同参,页面不刷新问题
若依 this.$router.push 同地址不同参,页面不刷新问题
1496 0
vue2项目使用?.语法报错如何解决?(@babel/plugin-proposal-optional-chaining)
vue2项目使用?.语法报错如何解决?(@babel/plugin-proposal-optional-chaining)
857 0
|
前端开发 API
解决el-table中横向滚动条无法显示的问题
解决el-table中横向滚动条无法显示的问题
3123 0
Vue2信息提示(Modal)
这是一个基于 Vue3 的信息提示模态框(Modal)组件,提供了丰富的自定义属性,包括标题、内容、宽度、按钮文本等。它支持两种模式:确认提示框(confirm)和信息提示框(info),并有六种不同的展示效果。模态框可以水平垂直居中或固定高度水平居中显示,支持加载中状态。该组件模仿了 ant-design-vue 的样式,适用于各种场景下的信息提示。
283 1
Vue2信息提示(Modal)
|
12月前
|
存储 供应链 前端开发
2024你不得不知道的免费API
本文首发于微信公众号“前端徐徐”,介绍了 2024 年各领域的免费 API 列表,涵盖游戏、语言、音乐、安全、科学、体育、Web 应用、产品、健康、政府和地理、食品、开源项目、电影和漫画等多个类别。每个 API 均提供官网链接、简介及示例代码,帮助开发者轻松集成第三方服务,增强应用程序的功能和用户体验。
937 4
2024你不得不知道的免费API
|
JavaScript
Vue2通知提醒框(Notification)
这篇文章介绍了如何在Vue 3框架中创建一个通知提醒框(Notification)组件,支持自定义延时关闭、弹出位置和五种不同样式的消息提示。
481 1
Vue2通知提醒框(Notification)
|
前端开发 JavaScript
使用Vue+xlsx+xlsx-style实现导出自定义样式的Excel文件
本文介绍了在Vue项目中使用`xlsx`和`xlsx-style`(或`xlsx-style-vite`)库实现导出具有自定义样式的Excel文件的方法,并提供了详细的示例代码和操作效果截图。
2106 2
使用Vue+xlsx+xlsx-style实现导出自定义样式的Excel文件
Vue3通知提醒(Notification)
这是一个基于 Vue2 的通知提醒框组件,支持高度自定义设置,包括消息标题、自动关闭延时、弹出位置等。提供了五种样式:默认、信息、成功、警告和错误,并可通过不同方法调用以实现相应样式。组件还支持多种位置设置,如顶部左侧、顶部右侧、底部左侧和底部右侧,并允许调整与屏幕边缘的距离。
654 3
Vue3通知提醒(Notification)
|
移动开发 JavaScript HTML5
el-input限制输入整数等分析
本文介绍了在Vue中限制el-input只能输入整数的几种方式,包括设置type为number,使用inputmode属性,自定义指令,计算属性,使用onafterpaste和onkeyup事件以及使用el-input-number的precision属性。每种方式都有其优缺点,可以根据实际需求选择合适的方式。比较建议用自定义指令的方式来实现。
2239 0
el-input限制输入整数等分析
|
前端开发
css动画(仿微信聊天页面)
css动画(仿微信聊天页面)