源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

js实现网页标题栏闪烁提示效果实例分析

  • 时间:2021-08-25 00:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:js实现网页标题栏闪烁提示效果实例分析
本文实例讲述了js实现网页标题栏闪烁提示效果的方法。分享给大家供大家参考。具体分析如下: 网页标题栏闪烁效果我们在一些聊天工具会常看到,像现在流量的聊天室,下面我们就来给大家总结一款实现网页标题栏闪烁提示代码,感兴趣可参考一下。 公司的项目中用到了这个新消息提示的效果,主要用于提示用户有新消息。具体实现代码如下:
[u]复制代码[/u] 代码如下:
var newMessageRemind={ _step: 0, _title: document.title, _timer: null, //显示新消息提示 show:function(){ var temps = newMessageRemind._title.replace("【   】", "").replace("【新消息】", ""); newMessageRemind._timer = setTimeout(function() { newMessageRemind.show(); //这里写Cookie操作 newMessageRemind._step++; if (newMessageRemind._step == 3) { newMessageRemind._step = 1 }; if (newMessageRemind._step == 1) { document.title = "【   】" + temps }; if (newMessageRemind._step == 2) { document.title = "【新消息】" + temps }; }, 800); return [newMessageRemind._timer, newMessageRemind._title]; }, //取消新消息提示 clear: function(){ clearTimeout(newMessageRemind._timer ); document.title = newMessageRemind._title; //这里写Cookie操作 } };
调用显示新消息提示:newMessageRemind.show(); 调用取消新消息提示:newMessageRemind.clear(); 看了上面代码自己再进行优化一下,不管怎样,自己能吸收学习到就好了。:)我主要是觉得他代码里面 newMessageRemind 这字段用得太多了,看起来密密麻麻的,多不舒服啊,想着换一种小清新的方式展现出来,于是乎就有了下面的代码:
[u]复制代码[/u] 代码如下:
var newMessageRemind = function () {     var i = 0,         title = document.title,         loop;       return {         show: function () {             loop = setInterval(function () {                 i++;                 if ( i == 1 ) document.title = '【新消息】' + title;                 if ( i == 2 ) document.title = '【   】' + title;                 if ( i == 3 ) i = 0;             }, 800);         },         stop: function () {             clearInterval(loop);             document.title = title;         }     }; } ();
是不是清新了很多呢?^_^
[u]复制代码[/u] 代码如下:
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>放假啦!!!</title> </head> <body> <button id="test">stop</button> <script type="text/javascript"> var newMessageRemind = function () {  var i = 0,   title = document.title,   loop;  return {   show: function () {    loop = setInterval(function () {     i++;     if ( i == 1 ) document.title = '【新消息】' + title;     if ( i == 2 ) document.title = '【   】' + title;     if ( i == 3 ) i = 0;    }, 800);   },   stop: function () {    clearInterval(loop);    document.title = title;   }  }; } (); newMessageRemind.show(); document.getElementById('test').onclick = function () {  newMessageRemind.stop(); }; </script> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部