js和jquery 实现网站来消息网站标题闪动提示
js版
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <meta charset="UTF-8"> <title>无标题页</title> <script> //页面激活判断 var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null; var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange'); var onVisibilityChange = function(){ if (!document[hiddenProperty]) { console.log('页面非激活'); }else{ alert('页面激活') console.log('页面激活'); } } document.addEventListener(visibilityChangeEvent, onVisibilityChange); //网站标题闪动提示 var g_blinkid = 0; var g_blinkswitch = 0; var g_blinktitle = document.title; var g_onlineuser = ""; var g_sysmsg_sound = null; var g_newmsg_sound = null; var g_app_num = 0; var g_appnum = 0; var g_bappmore = false; var g_inputtime = 0; function blinkNewMsg() { document.title = g_blinkswitch % 2==0 ? "【 】 - " + g_blinktitle : "【新消息】 - " + g_blinktitle; g_blinkswitch++; } g_blinkid = setInterval(blinkNewMsg, 1000); function stopBlinkNewMsg() { if (g_blinkid) { clearInterval(g_blinkid); g_blinkid = 0; document.title = g_blinktitle; } } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="button" onclick="stopBlinkNewMsg();" /> </div> </form> </body> </html>
jquery版,记得要引入jquery.js
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <base href="<%=basePath%>"> <title>网页标题</title> </head> <body> <p style="text-align: center;"> 请看网页标题处效果! <br /> 隔10秒后提示消失 </p> <script type="text/javascript" src="jquery-3.1.1.min.js"> </script> <script type="text/javascript"> (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); jQuery(function($) { var timerArr = $.blinkTitle.show(); setTimeout(function() { //此处是过一定时间后自动消失 $.blinkTitle.clear(timerArr); }, 10000); //若认为操作消失,只需如此调用: $.blinkTitle.clear(timerArr); }); </script> <br /> <center> 如不能显示效果,请按Ctrl+F5刷新本页 </center> </body> </html>
看完打开支付宝扫一扫领个红包吧!