雅虎、網易ajax標籤導航效果實現

來源:互聯網
上載者:User
ajax|導航|雅虎

經過多次改進,可以在普通網站上直接使用了,現在發布來源程式下載(編碼為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.lorlo.com/tab.html

源碼下載:點擊這裡下載源檔案

JS檔案分析:

代碼拷貝框
function getObject(objectId) { if(document.getElementById && document.getElementById(objectId)) { // W3C DOM return document.getElementById(objectId); } else if (document.all && document.all(objectId)) { // MSIE 4 DOM return document.all(objectId); } else if (document.layers && document.layers[objectId]) { // NN 4 DOM.. note: this won't find nested layers return document.layers[objectId]; } else { return false; }} var responsecont;var xmlHttp;var xH=new Object();var rqtp=new Object();var dateStr,dStr;var requestType;var etype=0;var newsstring;var ajccache=new Object();var url;var MouseDelayTime=200;//滑鼠感應延遲var waitInterval;var Browser = new Object();Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');Browser.isIE = window.ActiveXObject ? true : false;Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);function SetCookie(name,value,expires){//寫cookievar exp=new Date();exp.setTime(exp.getTime()+expires*60000);document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();//+";domain=arkoo.com;path=/";} function readcookie(name){//讀取cookievar oRegex=new RegExp(name+'=([^;]+)','i');var oMatch=oRegex.exec(document.cookie);if(oMatch&&oMatch.length>1)return unescape(oMatch[1]);else return '';}function CreateXMLHttpRequest(){ // Initialize Mozilla XMLHttpRequest object if (window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } // Initialize for IE/Windows ActiveX version else if (window.ActiveXObject) { try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){newsstring = "<div class='loading'>Loading rquest content fail, Please try it again latter...</div>";} } }}function getnews(tagid,x){url = "tab/"+tagid+'_'+x+'.html';var url2=url;requestType = tagid;if(ajccache[url]==null){var loadstatustext="<div class='loading'><img src='http://www.webjx.com/htmldata/2007-01-19/tab/images/loading.gif' /> Loading request content, please wait...</div>";rqtp[tagid]=tagid;CreateXMLHttpRequest(); var hdr=tagid; xH[tagid]=xmlHttp; if(etype==1)getObject(requestType+'_cnt').innerHTML = loadstatustext; xH[tagid].onreadystatechange = function(){ // only if xmlHttp shows "complete" if (xH[tagid].readyState == 4){ // only http 200 to process if (window.location.href.indexOf("http")==-1 || xH[tagid].status == 200){ newsstring = xH[tagid].responseText; dateStr=xH[tagid].getResponseHeader("Date"); dStr = new Date(dateStr);SetCookie('dStr',dStr,10000000); //inject centent to tab-pane shownews(rqtp[tagid],newsstring); ajccache[url2]=newsstring; } }} xH[tagid].open("GET", url, true); xH[tagid].setRequestHeader("If-Modified-Since","0"); xH[tagid].send(null);}else{shownews(requestType,ajccache[url]);}}function shownews(requestType,newsstring){//<![CDATA[ responsecont = getObject(requestType+'_cnt'); responsecont.innerHTML = newsstring; getObject("pdate").innerHTML=dStr;if(getObject("alta")){ata(getObject("alta"));hideText();}//]]>}function TabNews(tagid,x){ var lim=getObject(tagid+"_mn").getElementsByTagName('li'); var mnt=getObject(tagid+"_mn").getElementsByTagName('li').length; if(!mnt)mnt=4; for(var i=0;i<mnt;i++){ if(i == (x-1)){ if(i==0){ lim[i].className="tabactive1"; } else{ lim[i].className="tabactive2"; lim[i-1].getElementsByTagName('a')[0].style.background="url()"; } lim[i].getElementsByTagName('a')[0].style.background="url(tab/images/tab_bgs.gif) right -60px no-repeat"; try{getnews(tagid,x);}catch(e){alert(e);} } else{ lim[i].className=""; lim[i].getElementsByTagName('a')[0].style.background="url(tab/images/tab_bgs.gif) right -88px no-repeat"; } }}var idn,tmpa,tmpat;var aw="";var rT;function aet(tagid,etp){var lis=getObject(tagid+"_mn").getElementsByTagName('li');var wts=tagid+"_cnt";var wnf=tagid+"info";if(readcookie(tagid+"_cntd")=="none"){ hiddenList(getObject(wts)); maxh[wts]=readcookie(tagid+"_cnth"); getObject("pdate").innerText=readcookie("dStr"); }else{ if(parseInt(readcookie(wnf))>1) TabNews(tagid,readcookie(wnf));//返回上次訪問 else TabNews(tagid,1); if(etp==1)aw=tagid+readcookie(wnf);} for(var iy=0;iy<lis.length;iy++){ with(lis[iy]){ if(etp==0){ lis[iy].onmouseover=function(){ rT=attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1); if(getObject(rT+'_cnt').style.display != "none"&&className!="") return; idn=parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length)); clearTimeout(waitInterval); 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); } lis[iy].onmouseout=function(){if(getObject(rT+'_cnt').style.display != "none"&&className!="") return;clearTimeout(waitInterval);} } if(etp==1){ lis[iy].onmouseover=function(){getElementsByTagName('a')[0].blur();} lis[iy].onclick=function(){ idn=parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length)); rT=attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1); if(getObject(rT+'_cnt').style.display != "none"&&(idn==1&&(this.className=="tabactive1")||aw==attributes.getNamedItem('id').value)) return true; if(getObject(rT+'_cnt').style.display == "none"){ displayList(rT+'_cnt');SetCookie(rT+'_cntd','ture',10000000); } etype=1; TabNews(rT,idn); SetCookie(rT+'info',idn,10000000); aw=attributes.getNamedItem('id').value; getElementsByTagName('a')[0].blur(); return false; }}} }} function ata(objid){var as=objid.getElementsByTagName('a');for(var i=0;i<as.length;i++){as[i].target="_self";}}function GetUrlParam(paramName){ //擷取URL參數的函數 var oRegex = new RegExp('[\?&]' + paramName + '=([^&]+)', 'i'); var oMatch = oRegex.exec(window.location.search); if(oMatch && oMatch.length > 1) return oMatch[1]; else return '';}function hideText(){if(document.getElementsByTagName("arkoo:title").length==0){var upu=GetUrlParam("username");var upa=GetUrlParam("articleid");getObject("closeit").innerHTML='<a href="http://cn.arkoo.com/diaryfiles/showpage.aspx?username='+upu+'&articleid='+upa+'" target="_blank">查看原文</a> <a href="tab.html" target="_self">×</a>';}}var maxh=new Object();//顯示內容框function displayList(oid){ var h = 0; var objid=getObject(oid); if(isNaN(parseInt(maxh[oid]))) var max_h = 210; // 容器的最大高度 else var max_h = maxh[oid]; var anim = function(){ h += 50; // 每次遞增50像素 if(h >= max_h){ objid.style.height = max_h + "px";; getObject('tab'+objid.attributes.getNamedItem('id').value).style.background="url(tab/images/tab_bgs.gif) 5px -113px no-repeat"; // 讓圖片標籤改變背景 if(tt){window.clearInterval(tt);} } else{ objid.style.display="block"; objid.style.height = h + "px"; } } var tt = window.setInterval(anim,2); }// 隱藏列表框function hiddenList(objid){ var h = objid.offsetHeight; var anim = function(){ h -= 50; // 每次遞減50像素 if(h <= 5){ objid.style.display="none"; getObject('tab'+objid.attributes.getNamedItem('id').value).style.background="url(tab/images/tab_bgs.gif) -14px -114px no-repeat"; if(tt){window.clearInterval(tt);} } else{ objid.style.height = h + "px"; } } var tt = window.setInterval(anim,2); }function showClassList(oid){var objid=getObject(oid); if(objid.style.display == "none"){ if(objid.getElementsByTagName("img").length==0)TabNews(oid.substring(0,oid.indexOf("_")),1); displayList(oid); // 顯示內容框 SetCookie(oid+'d',"ture",10000000); } else{ if(isNaN(parseInt(maxh[oid]))){ if(Browser.isFirefox){ maxh[oid]=objid.offsetHeight; } else{ maxh[oid]=objid.offsetHeight; // 內容容器的初始高度 } } hiddenList(objid); // 隱藏內容框 SetCookie(oid+'d',"none",10000000);SetCookie(oid+'h',maxh[oid],10000000); }}
[Ctrl+A 全部選擇 然後拷貝]



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.