查看: 6918|回复: 0
打印 上一主题 下一主题

雅虎、网易ajax标签导航效果实现

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-19 22:55:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
经过多次改进,可以在普通网站上直接使用了,现在发布源程序下载(编码为utf-8,改为gb3212的方法是:用记事本打开,另存时选择相应的编码),希望对大家有用(虽然不一定实用)。
因为只是标签导航,所有内容还会在频道页中体现,所在全部用AJAX读取。

功能的实现思路:

雅虎中国

单击触发
<A>块锁定:a{display:block;height:16px;}/*将A锁定为块级,再定义高度和宽度*/
再次单击弹出链接:在<A>标记的onclick事件中需要执行读取时,return false;否则return true;
AJAX读取数据
数据暂存
窗口平滑收缩
<!--由于我看不懂雅虎的代码,所以使用了一些基本的代码去实现这些功能,可能效率不高,不过效果是出来了.-->

网易

鼠标滑过延时触发: onmouseover时var waitInterval=window.setTimeout("func();",300);onmouseout时clearTimeout(waitInterval);
<A>块锁定、AJAX读取数据、数据暂存、窗口平滑收缩
其它功能:

cookie记忆
最后读取数据的服务器时间
滑动门技术
在线效果:
http://www.jzxue.com/Files/UpLoadfiles/2007/2/tabdomo/tab.html

源码下载:http://www.jzxue.com/Files/UpLoadfiles/2007/2/tabdomo.rar

JS文件分析:

代码拷贝框
  1. function getObject(objectId) {
  2.      if(document.getElementById && document.getElementById(objectId)) {
  3.     // W3C DOM
  4.        return document.getElementById(objectId);
  5.      }
  6.      else if (document.all && document.all(objectId)) {
  7.     // MSIE 4 DOM
  8.        return document.all(objectId);
  9.      }
  10.      else if (document.layers && document.layers[objectId]) {
  11.     // NN 4 DOM.. note: this won't find nested layers
  12.        return document.layers[objectId];
  13.      }
  14.      else {
  15.        return false;
  16.     }
  17. }

  18. var responsecont;
  19. var xmlHttp;
  20. var xH=new Object();
  21. var rqtp=new Object();
  22. var dateStr,dStr;
  23. var requestType;
  24. var etype=0;
  25. var newsstring;
  26. var ajccache=new Object();
  27. var url;
  28. var MouseDelayTime=200;//鼠标感应延迟
  29. var waitInterval;
  30. var Browser = new Object();
  31. Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
  32. Browser.isIE = window.ActiveXObject ? true : false;
  33. Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
  34. Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

  35. function SetCookie(name,value,expires){//写cookie
  36. var exp=new Date();
  37. exp.setTime(exp.getTime()+expires*60000);
  38. document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();//+";domain=arkoo.com;path=/";
  39. }
  40. function readcookie(name){//读取cookie
  41. var oRegex=new RegExp(name+'=([^;]+)','i');
  42. var oMatch=oRegex.exec(document.cookie);
  43. if(oMatch&&oMatch.length>1)return unescape(oMatch[1]);
  44. else return '';
  45. }

  46. function CreateXMLHttpRequest(){
  47.    // Initialize Mozilla XMLHttpRequest object
  48.    if (window.XMLHttpRequest){
  49.        xmlHttp = new XMLHttpRequest();
  50.    }
  51.    // Initialize for IE/Windows ActiveX version
  52.    else if (window.ActiveXObject) {
  53.        try{
  54.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
  55.        }
  56.        catch (e){
  57.             try{
  58.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  59.             }
  60.             catch (e){newsstring = "Loading rquest content fail, Please try it again latter...";}
  61.        }
  62.    }
  63. }

  64. function getnews(tagid,x){
  65. url = "tab/"+tagid+'_'+x+'.html';
  66. var url2=url;
  67. requestType = tagid;
  68. if(ajccache[url]==null){
  69. var loadstatustext=" Loading request content, please wait...";
  70. rqtp[tagid]=tagid;
  71. CreateXMLHttpRequest();   
  72. var hdr=tagid;
  73.    xH[tagid]=xmlHttp;
  74.    if(etype==1)getObject(requestType+'_cnt').innerHTML = loadstatustext;
  75.    xH[tagid].onreadystatechange = function(){
  76.    // only if xmlHttp shows "complete"
  77.    if (xH[tagid].readyState == 4){
  78.       // only http 200 to process
  79.       if (window.location.href.indexOf("http")==-1 || xH[tagid].status == 200){
  80.          newsstring = xH[tagid].responseText;
  81.     dateStr=xH[tagid].getResponseHeader("Date");
  82.     dStr = new Date(dateStr);SetCookie('dStr',dStr,10000000);
  83.          //inject centent to tab-pane
  84.             shownews(rqtp[tagid],newsstring);
  85.             ajccache[url2]=newsstring;
  86.       }
  87.    }
  88. }
  89.    xH[tagid].open("GET", url, true);
  90.    xH[tagid].setRequestHeader("If-Modified-Since","0");
  91.    xH[tagid].send(null);
  92. }
  93. else
  94. {shownews(requestType,ajccache[url]);}
  95. }

  96. function shownews(requestType,newsstring){
  97. //<![CDATA[
  98.     responsecont = getObject(requestType+'_cnt');
  99.     responsecont.innerHTML = newsstring;
  100.     getObject("pdate").innerHTML=dStr;
  101. if(getObject("alta")){ata(getObject("alta"));hideText();}
  102. //]]>
  103. }


  104. function TabNews(tagid,x){
  105.     var lim=getObject(tagid+"_mn").getElementsByTagName('li');
  106.     var mnt=getObject(tagid+"_mn").getElementsByTagName('li').length;
  107.     if(!mnt)mnt=4;
  108.     for(var i=0;i<mnt;i++){
  109.         if(i == (x-1)){
  110.             if(i==0){
  111.                 lim[i].className="tabactive1";
  112.             }
  113.             else{
  114.                 lim[i].className="tabactive2";
  115.                 lim[i-1].getElementsByTagName('a')[0].style.background="url()";
  116.             }
  117.             lim[i].getElementsByTagName('a')[0].style.background="url(tab/images/tab_bgs.gif) right -60px no-repeat";
  118.             try{getnews(tagid,x);}catch(e){alert(e);}
  119.         }   
  120.         else{
  121.             lim[i].className="";
  122.             lim[i].getElementsByTagName('a')[0].style.background="url(tab/images/tab_bgs.gif) right -88px no-repeat";        
  123.         }
  124.     }
  125. }

  126. var idn,tmpa,tmpat;
  127. var aw="";
  128. var rT;
  129. function aet(tagid,etp){
  130. var lis=getObject(tagid+"_mn").getElementsByTagName('li');
  131. var wts=tagid+"_cnt";
  132. var wnf=tagid+"info";
  133. if(readcookie(tagid+"_cntd")=="none"){
  134.     hiddenList(getObject(wts));
  135.     maxh[wts]=readcookie(tagid+"_cnth");
  136.     getObject("pdate").innerText=readcookie("dStr");
  137.     }
  138. else{
  139.     if(parseInt(readcookie(wnf))>1)
  140.     TabNews(tagid,readcookie(wnf));//返回上次访问
  141.     else
  142.     TabNews(tagid,1);
  143.     if(etp==1)aw=tagid+readcookie(wnf);
  144. }
  145.     for(var iy=0;iy<lis.length;iy++){
  146.         with(lis[iy]){
  147.         if(etp==0){
  148.         lis[iy].onmouseover=function(){
  149.             rT=attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1);
  150.             if(getObject(rT+'_cnt').style.display != "none"&&className!="") return;
  151.             idn=parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length));
  152.             clearTimeout(waitInterval);
  153.             waitInterval=window.setTimeout("etype=0;TabNews(rT,idn);if(getObject(rT+'_cnt').style.display == 'none')displayList(rT+'_cnt'); SetCookie(rT+'info',idn,10000000);SetCookie(rT+'_cntd','ture',10000000);",MouseDelayTime);
  154.             }
  155.         lis[iy].onmouseout=function(){if(getObject(rT+'_cnt').style.display != "none"&&className!="") return;clearTimeout(waitInterval);}
  156.         }
  157.         if(etp==1){
  158.         lis[iy].onmouseover=function(){getElementsByTagName('a')[0].blur();}
  159.         lis[iy].onclick=function(){
  160.             idn=parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length));
  161.             rT=attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1);
  162.             if(getObject(rT+'_cnt').style.display != "none"&&(idn==1&&(this.className=="tabactive1")||aw==attributes.getNamedItem('id').value)) return true;
  163.                 if(getObject(rT+'_cnt').style.display == "none"){
  164.                 displayList(rT+'_cnt');SetCookie(rT+'_cntd','ture',10000000);
  165.                 }
  166.             etype=1;
  167.             TabNews(rT,idn);
  168.             SetCookie(rT+'info',idn,10000000);
  169.             aw=attributes.getNamedItem('id').value;
  170.             getElementsByTagName('a')[0].blur();
  171.             return false;
  172.             }}}
  173.     }
  174. }

  175. function ata(objid){
  176. var as=objid.getElementsByTagName('a');
  177. for(var i=0;i<as.length;i++){
  178. as[i].target="_self";
  179. }
  180. }


  181. function GetUrlParam(paramName){    //获取URL参数的函数
  182.     var oRegex = new RegExp('[\?&]' + paramName + '=([^&]+)', 'i');
  183.     var oMatch = oRegex.exec(window.location.search);
  184.     if(oMatch && oMatch.length > 1)
  185.         return oMatch[1];
  186.     else
  187.         return '';
  188. }


  189. function hideText(){
  190. if(document.getElementsByTagName("arkoo:title").length==0){
  191. var upu=GetUrlParam("username");
  192. var upa=GetUrlParam("articleid");
  193. getObject("closeit").innerHTML='查看原文 ×';
  194. }
  195. }

  196. var maxh=new Object();
  197. //显示内容框
  198. function displayList(oid){   
  199.     var h = 0;  
  200.     var objid=getObject(oid);
  201.           if(isNaN(parseInt(maxh[oid]))) var max_h = 210; // 容器的最大高度
  202.         else
  203.             var max_h = maxh[oid];
  204.           var anim = function(){            
  205.                     h += 50; // 每次递增50像素
  206.                     if(h >= max_h){
  207.                     objid.style.height = max_h + "px";;
  208.                     getObject('tab'+objid.attributes.getNamedItem('id').value).style.background="url(tab/images/tab_bgs.gif) 5px -113px no-repeat"; // 让图片标签改变背景           
  209.                     if(tt){window.clearInterval(tt);}
  210.                     }
  211.                     else{
  212.                 objid.style.display="block";
  213.                 objid.style.height = h + "px";
  214.                     }
  215.             }
  216.                
  217.               var tt = window.setInterval(anim,2);      
  218. }

  219. // 隐藏列表框
  220. function hiddenList(objid){
  221.        var h = objid.offsetHeight;
  222.            var anim = function(){
  223.                  h -= 50; // 每次递减50像素
  224.                  if(h <= 5){
  225.                  objid.style.display="none";
  226.                    getObject('tab'+objid.attributes.getNamedItem('id').value).style.background="url(tab/images/tab_bgs.gif) -14px -114px no-repeat";
  227.                    if(tt){window.clearInterval(tt);}
  228.                }
  229.                else{
  230.                    objid.style.height = h + "px";
  231.                }
  232.            }
  233.             
  234.            var tt = window.setInterval(anim,2);
  235. }

  236. function showClassList(oid){
  237. var objid=getObject(oid);
  238.    if(objid.style.display == "none"){
  239.     if(objid.getElementsByTagName("img").length==0)TabNews(oid.substring(0,oid.indexOf("_")),1);
  240.        displayList(oid); // 显示内容框
  241.     SetCookie(oid+'d',"ture",10000000);
  242.    }
  243.    else{
  244.       if(isNaN(parseInt(maxh[oid]))){
  245.          if(Browser.isFirefox){
  246.             maxh[oid]=objid.offsetHeight;
  247.       }
  248.       else{
  249.          maxh[oid]=objid.offsetHeight; // 内容容器的初始高度
  250.           }
  251.       }
  252.       hiddenList(objid); // 隐藏内容框
  253.     SetCookie(oid+'d',"none",10000000);SetCookie(oid+'h',maxh[oid],10000000);
  254.    }
  255. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表