vue页面title标题滚动以及浏览器通知消息提示

今天开发遇到一个新问题,需求是要求当服务器有新数据推送过来的时候页面有提示,分别是title标题滚动提示和弹窗提示。下面我就来说明一下这两种方式都是如何实现的。

1.首先是页面title标题滚动,首先定义好title要显示的内容,demo中我写了一条死数据

document.title='你有一条新的消息'

定义text=document.title

data() {
    return {
      text: null,
    }
  },

在methods里面添加newtext方法,通过字符串的substring方法截取title

newtext() {
      document.title = this.text.substring(1, this.text.length) + this.text.substring(0, 1)
      this.text = document.title.substring(0, this.text.length)
      this.time()
    },

再添加定时器调用newtext方法即可实现title轮播。

2.现成的UI库中大多数的消息提示都是在本页面提示,并不能够实现桌面级消息提示,但是在需求中需要能够有一个跨页面进行的一个消息提示,那该如何去做呢?

主要使用的是浏览器的Notification方法

created(){
      if (window.Notification) {
      // 浏览器通知--window.Notification
      if (Notification.permission == "granted") {
        console.log("允许通知")
      }else if( Notification.permission != "denied"){
        console.log("需要通知权限")
        Notification.requestPermission((permission)=> {});
      }
    } else {
      console.error('浏览器不支持Notification');
    }
  },

在created里面对浏览器做一个Notification方法校验和权限授权,如果浏览器支持Notification方法的话会弹出权限框,点击允许给Notification进行授权

 在methods中定义消息的弹出类型和内容

 methods: {
      popNotice(user, content) {
      let that = this;
      if (Notification.permission == "granted") {
        let notification = new Notification(user, {
          body: content,
        });

        notification.onclick = function(e) {
          that.$nextTick(() => {
            setTimeout(()=>{
              //具体操作
            },500);
          });
          //可直接打开通知notification相关联的tab窗
          window.focus();
          notification.close();
        };
      }
    },
    handle(){
      this.popNotice('您有一条新的消息')
    }
  }

 接着,消息就会以浏览器通知的形式弹出

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值