本文实例讲述了jQuery实现新消息闪烁标题提示的方法。分享给大家供大家参考。具体如下:
该代码可实现在标题栏部位闪烁地显示提示信息。
1. jQuery插件风格代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ;( function ($) { $.extend({ /** * 调用方法: var timerArr = $.blinkTitle.show(); * $.blinkTitle.clear(timerArr); */ blinkTitle : { show : function () { //有新消息时在title处闪烁提示 var step=0, _title = document.title; var timer = setInterval( function () { step++; if (step==3) {step=1}; if (step==1) {document.title= '【 】' +_title}; if (step==2) {document.title= '【新消息】' +_title}; }, 500); return [timer, _title]; }, /** * @param timerArr[0], timer标记 * @param timerArr[1], 初始的title文本内容 */ clear : function (timerArr) { //去除闪烁提示,恢复初始title文本 if (timerArr) { clearInterval(timerArr[0]); document.title = timerArr[1]; }; } } }); })(jQuery); |
2. 调用方法如下:
1 2 3 4 5 6 7 | jQuery( function ($) { var timerArr = $.blinkTitle.show(); setTimeout( function () { //此处是过一定时间后自动消失 $.blinkTitle.clear(timerArr); }, 10000); //若人为操作消失,只需如此调用:$.blinkTitle.clear(timerArr); }); |