基于Vue2+Element UI或Vue3+Element plus对 el-notification 增加倒计时进度条特效,鼠标移入暂停计时,移出继续计时

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。基于Vue2+Element UI或Vue3+Element plus对 el-notification 增加倒计时进度条特效,鼠标移入暂停计时,移出继续计时,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1、基于Vue2+Element UI的项目

<template>
  <div>
    <el-button @click="showTip">do it</el-button>
  </div>
</template>

<script>
export default {
  data: () => ({

  }),
  created() {
    console.log('created =>', 'SUCCESS')

    // 在 created 生命周期,调用 method 的 yyyyyy 方法
    // this.yyyyyy();
  },
  methods: {
    /**
     * 通知消息
     */
    showTip() {
      const h = this.$createElement;
      const contents = []
      const msg = h('p', {}, '任务启动成功!')
      const task = h('p', {}, '任务ID:' + 1234)
      const job = h('p', {}, 'JobID:' + 661234)
      const progress = h(
        'div',
        {
          class: {
            'el-progress-plus': true
          },
          style: {
              'width': '230px',
              'height': '6px',
              'background-color': '#5e7ce0',
              'margin-top': '6px',
              'border-radius': '6px',
          },
          percentage: 100
        },
        ''
      )
      const br = h('p', { style: 'width: auto; height: 10px' }, '')
      contents.push(msg)
      contents.push(task)
      contents.push(job)
      contents.push(progress)
      contents.push(br)

      let classID = 1
      let className = 'el-notification-plus' + classID
      // 判断是否已存在该通知元素,以及最多限制生成10000个通知
      while(document.getElementsByClassName(className)[0]) {
        if (classID > 10000) {
          // 无法生成元素
          console.log('无法生成元素')
          break
        } else {
          // 继续累加
          classID++
          className = 'el-notification-plus' + classID
        }
      }

      // 实例化通知
      const notifyInstance = this.$notify({
        title: '一键测试',
        type: 'success',
        customClass: className,
        message: h('div', {}, contents),
        duration: 0
      })

      // 启动倒计时
      let timer = this.countDown(className, notifyInstance)

      // 获取 Notification 的DOM元素
      const ElNotificationPlus = document.getElementsByClassName(className)[0]
      // console.log('ElNotificationPlus =>', ElNotificationPlus)

      // 为 Notification 元素 定义鼠标进入方法,暂停倒计时
      ElNotificationPlus.onmouseenter = () => {
        console.log('onmouseover~', className)
        clearInterval(timer)
      }

      // 为 Notification 元素 定义鼠标移出方法,继续倒计时
      ElNotificationPlus.onmouseleave = () => {
        console.log('onmouseout~', className)
        clearInterval(timer)
        timer = this.countDown(className, notifyInstance)
      }
    },

    /**
     * 倒计时
     */
    countDown(className, notifyInstance) {
      const timer = setInterval(() => {
        try {
          if (notifyInstance) {
            const ElNotificationPlus = document.getElementsByClassName(className)[0]
            // console.log('ElNotificationPlus =>', ElNotificationPlus)
            const ElProgressPlus = ElNotificationPlus.getElementsByClassName('el-progress-plus')[0]
            // console.log('ElProgressPlus =>', ElProgressPlus)

            let percentage = ElProgressPlus.getAttribute('percentage')
            if (percentage >= 0) {
              percentage = percentage - 0.5
              ElProgressPlus.setAttribute('percentage', percentage)
              ElProgressPlus.style.width = (230 * (percentage / 100)) + 'px'
            } else {
              // 清除定时器
              clearInterval(timer)

              // 手动关闭通知
              setTimeout(() => {
                notifyInstance.close()
              }, 50);
            }
          } else {
            // 清除定时器
            clearInterval(timer)
          }
        } catch (error) {
          // 清除定时器
          clearInterval(timer)
        }
      }, 50)

      return timer
    },
  }
}
</script>

<style>
  .el-notification-plus .el-progress .el-progress__text{
    display: none;
  }
</style>

2、基于Vue3+Element plus的项目

<template>
  <div>
    <el-button @click="showTip">do it</el-button>
  </div>
</template>

<script>
import { h, onMounted, getCurrentInstance } from 'vue'

export default {
  setup() {
    onMounted(() => {
      console.log('onMounted =>', 'SUCCESS')

      // const { proxy  } = getCurrentInstance()

      // 在 onMounted 生命周期,调用 method 的 xxxxxx 方法
      // proxy.xxxxxx()
    })
  },
  data: () => ({

  }),
  created() {
    console.log('created =>', 'SUCCESS')

    // 在 created 生命周期,调用 method 的 yyyyyy 方法
    // this.yyyyyy();
  },
  methods: {
    /**
     * 通知消息
     */
    showTip() {
      const contents = []
      const msg = h('p', {}, '任务启动成功!')
      const task = h('p', {}, '任务ID:' + 1234)
      const job = h('p', {}, 'JobID:' + 661234)
      const progress = h(
        'div',
        {
          class: {
            'el-progress-plus': true
          },
          style: {
              'width': '230px',
              'height': '6px',
              'background-color': '#5e7ce0',
              'margin-top': '6px',
              'border-radius': '6px',
          },
          percentage: 100
        },
        ''
      )
      const br = h('p', { style: 'width: auto; height: 10px' }, '')
      contents.push(msg)
      contents.push(task)
      contents.push(job)
      contents.push(progress)
      contents.push(br)

      let classID = 1
      let className = 'el-notification-plus' + classID
      // 判断是否已存在该通知元素,以及最多限制生成100个通知
      while(document.getElementsByClassName(className)[0]) {
        if (classID > 100) {
          // 无法生成元素
          console.log('无法生成元素')
          break
        } else {
          // 继续累加
          classID++
          className = 'el-notification-plus' + classID
        }
      }

      // 实例化通知
      const notifyInstance = this.$notify({
        title: '一键测试',
        type: 'success',
        customClass: className,
        message: h('div', {}, contents),
        duration: 0
      })

      // 启动倒计时
      let timer = this.countDown(className, notifyInstance)

      // 获取 Notification 的DOM元素
      const ElNotificationPlus = document.getElementsByClassName(className)[0]
      // console.log('ElNotificationPlus =>', ElNotificationPlus)

      // 为 Notification 元素 定义鼠标进入方法,暂停倒计时
      ElNotificationPlus.onmouseenter = () => {
        console.log('onmouseover~', className)
        clearInterval(timer)
      }

      // 为 Notification 元素 定义鼠标移出方法,继续倒计时
      ElNotificationPlus.onmouseleave = () => {
        console.log('onmouseout~', className)
        clearInterval(timer)
        timer = this.countDown(className, notifyInstance)
      }
    },

    /**
     * 倒计时
     */
    countDown(className, notifyInstance) {
      const timer = setInterval(() => {
        try {
          if (notifyInstance) {
            const ElNotificationPlus = document.getElementsByClassName(className)[0]
            // console.log('ElNotificationPlus =>', ElNotificationPlus)
            const ElProgressPlus = ElNotificationPlus.getElementsByClassName('el-progress-plus')[0]
            // console.log('ElProgressPlus =>', ElProgressPlus)

            let percentage = ElProgressPlus.getAttribute('percentage')
            if (percentage >= 0) {
              percentage = percentage - 0.5
              ElProgressPlus.setAttribute('percentage', percentage)
              ElProgressPlus.style.width = (230 * (percentage / 100)) + 'px'
            } else {
              // 清除定时器
              clearInterval(timer)

              // 手动关闭通知
              setTimeout(() => {
                notifyInstance.close()
              }, 50);
            }
          } else {
            // 清除定时器
            clearInterval(timer)
          }
        } catch (error) {
          // 清除定时器
          clearInterval(timer)
        }
      }, 50)

      return timer
    },
  }
}
</script>

<style>
  .el-notification-plus .el-progress .el-progress__text{
    display: none;
  }
</style>

3、以上效果都一样的,如下图所示:

基于Vue2+Element UI或Vue3+Element plus对 el-notification 增加倒计时进度条特效,鼠标移入暂停计时,移出继续计时​​​​​​​

 

4、辅助参考资料

JS DOM获取元素属性+操作方法
Vue2的h函数(createElement)与Vue3中的h函数用法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151130.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!