本文实例讲述了JS实现消息来时让网页标题闪动效果的方法。分享给大家供大家参考,具体如下:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>
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>

参考开心网的head-26.js

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()
{
  var now  = new Date();
  var nowtime = now.getTime();
  if(nowtime - g_inputtime > 5000)
  {
    document.title = g_blinkswitch % 2 "【   】 - " + g_blinktitle : "【新消息】 - " + g_blinktitle;
  }
  g_blinkswitch++;
}
function blinkOnline()
{
  document.title = g_blinkswitch % 2 "○" + g_onlineuser + " 上线了 - " + g_blinktitle : "●" + g_onlineuser + " 上线了 - " + g_blinktitle;
  g_blinkswitch++;
  if (g_blinkswitch > 10)
  {
    stopBlinkNewMsg();
  }
}
function checkNewMsg()
{
  var url = "/home/newmsg.php";
  var pars = "";
  var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { checkNewMsgShow(req); } });
}
function stopBlinkNewMsg()
{
  if (g_blinkid)
  {
    clearInterval(g_blinkid);
    g_blinkid = 0;
    $("head_msgdiv").style.display = "none";
    document.title = g_blinktitle;
  }
}
var g_oldmsg = g_oldsysmsg = g_oldbbs = g_oldbbsreply = g_oldcomment = g_oldreply = 0;
function checkNewMsgShow(req)
{
  var r = req.responseText;
  stopBlinkNewMsg();
  setTimeout(checkNewMsg, 60000);
  eval ("r="+r);
  var a_msglist = new Array("msg", "sysmsg", "bbs", "bbsreply", "comment", "reply");
  if(r.notice == "1")
  {
    var forbidsound = parseInt(r.forbidsound);
    if (!forbidsound)
    {
      for (i=0; i<a_msglist.length; i++)
      {
        if ($("body_" + a_msglist[i] + "_num"))
        {
          var c = parseInt($("body_" + a_msglist[i] + "_num").innerHTML);
          eval("g_old" + a_msglist[i] + "=c;");
        }
      }
      var newmsg = parseInt(r.msg) + parseInt(r.bbs) + parseInt(r.bbsreply) + parseInt(r.comment) + parseInt(r.reply);
      var sysmsg = parseInt(r.sysmsg);
      var newchange = (parseInt(r.msg) - g_oldmsg)
        || (parseInt(r.bbs) - g_oldbbs)
        || (parseInt(r.bbsreply) - g_oldbbsreply)
        || (parseInt(r.comment) - g_oldcomment)
        || (parseInt(r.reply) - g_oldreply);
      var syschange = (parseInt(r.sysmsg) - g_oldsysmsg);
      for (i=0; i<a_msglist.length; i++)
      {
        eval("g_old" + a_msglist[i] + "=parseInt(r." + a_msglist[i] + ");");
      }
      if (newmsg && newchange)
      {
        if (g_newmsg_sound == null)
        {
          g_newmsg_sound = new SWFObject("http://img.kaixin001.com.cn/i2/newmsg_sound.1.0.swf", "newmsg_sound_swf", "1", "1", "8", "#ffffff", true);
          g_newmsg_sound.addParam("allowscriptaccess", "always");
          g_newmsg_sound.addParam("wmode", "opaque");
          g_newmsg_sound.addParam("menu", "false");
          g_newmsg_sound.addVariable("autoplay","0");
        }
        g_newmsg_sound.write("head_msgsound_div");
      }
      else if (sysmsg && syschange)
      {
        if (g_sysmsg_sound == null)
        {
          g_sysmsg_sound = new SWFObject("http://img.kaixin001.com.cn/i2/sysmsg_sound.1.0.swf", "sysmsg_sound_swf", "1", "1", "8", "#ffffff", true);
          g_sysmsg_sound.addParam("allowscriptaccess", "always");
          g_sysmsg_sound.addParam("wmode", "opaque");
          g_sysmsg_sound.addParam("menu", "false");
          g_sysmsg_sound.addVariable("autoplay","0");
        }
        g_sysmsg_sound.write("head_msgsound_div");
      }
    }
    $("head_msgdiv").style.display = "block";
    g_blinkid = setInterval(blinkNewMsg, 1000);
  }
  else if (0 && r.online.length)
  {
    g_blinkswitch = 0;
    g_onlineuser = r.online;
    g_blinkid = setInterval(blinkOnline, 500);
  }
  for (i=0; i<a_msglist.length; i++)
  {
    if (!parseInt(r[a_msglist[i]]))
    {
      $("head_" + a_msglist[i] + "_num").innerHTML = "";
      if ($("body_" + a_msglist[i] + "_num"))
      {
        $("body_" + a_msglist[i] + "_num").className = "ql2";
        $("body_" + a_msglist[i] + "_num").innerHTML = "0条新";
      }
    }
    else
    {
      $("head_" + a_msglist[i] + "_num").innerHTML = "(" + r[a_msglist[i]] + ")";
      if ($("body_" + a_msglist[i] + "_num"))
      {
        $("body_" + a_msglist[i] + "_num").className = "cr";
        $("body_" + a_msglist[i] + "_num").innerHTML = r[a_msglist[i]] + "条新";
      }
      if (a_msglist[i] == "msg")
      {
        if ('function' == typeof(msg_view_checkNewMsg))
        {
          msg_view_checkNewMsg();
        };
      }
    }
  }
}
function outputHead()
{
  var v_html = 
'<div id="head">'
+'  <div class="hd">'
+'    <div class="h1 wl1" style="margin-top:3px;">'
+'      <div style="padding-left:18px;"><a href="/" class="cf" title="开心网"><img src="/UploadFiles/2021-04-02/kaixinlogo.gif">

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中ajax操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

标签:
JS,消息来时,网页标题,闪动,JS标题闪动

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com