DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
AJAX 標簽導航效果(仿網易首頁)實現
編輯:AJAX詳解     

標簽式導航能讓多個內容面板在同一區域切換,是門戶網站的常見交互。本例在原有效果基礎上做了改進,重點在於 JS 部分的緩存與事件綁定優化,並支持鼠標滑過或點擊兩種觸發方式。

兼容多瀏覽器的對象獲取

function getObject(objectId) {
    if (document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId); // W3C DOM
    } else if (document.all && document.all(objectId)) {
        return document.all(objectId); // MSIE 4 DOM
    } else if (document.layers && document.layers[objectId]) {
        return document.layers[objectId]; // NN 4 DOM
    } else {
        return false;
    }
}

var responsecont;
var XMLHttp;
var requestType;
var newsstring;
var ajccache = new Object(); // 緩存已訪問的數據頁面
var url;
var MouseDelayTime = 150; // 鼠標感應延遲
var waitInterval;

創建請求對象

function CreateXMLHttpRequest(){
    if (window.XMLHttpRequest){ // Mozilla
        xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE/Windows
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
        } catch (e){
            try{
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                newsstring = "<div class='loading'>Loading request content fail, Please try it again latter...</div>";
            }
        }
    }
}

請求並緩存內容

function getnews(tagid, x){
    url = "tab/"+tagid+'_'+x+'.Html';
    requestType = tagid;
    if(ajccache[url] == null){
        CreateXMLHttpRequest();
        XMLHttp.onreadystatechange = processRequestChange;
        XMLHttp.open("GET", url, true);
        XMLHttp.setRequestHeader("If-Modified-Since","0");
        XMLHttp.send(null);
    } else {
        shownews(requestType, ajccache[url]);
    }
}

function processRequestChange(){
    if (XMLHttp.readyState == 4){
        if (window.location.href.indexOf("http")==-1 || XMLHttp.status == 200){
            newsstring = XMLHttp.responseText;
            shownews(requestType, newsstring);
            ajccache[url] = newsstring; // 把已訪問的數據緩存下來
        }
    }
}

function shownews(requestType, newsstring){
    responsecont = getObject(requestType+'_cnt');
    responsecont.innerHTML = newsstring;
}

這裡的關鍵優化是引入了 ajccache 對象:一旦某個標簽的內容被請求過,就把它緩存起來,下次切換回來直接讀緩存,避免重複請求服務器,顯著提升切換速度。

標簽切換邏輯

function TabNews(tagid, x){
    for (var i=1; i<=7; i+=2) {
        if (i == x) {
            getObject(tagid+i).className="tabactive"+i;
            if(i!=1){
                getObject(tagid+(i-1)).style.display="none";
                if(i!=7){ getObject(tagid+(i+1)).style.display="none"; }
            }
            if(i==1){ getObject(tagid+"2").style.display="none"; }
            try{ getnews(tagid, i); } catch(e){ alert(e); }
        } else {
            getObject(tagid+i).className="";
            if(i!=7){ getObject(tagid+(i+1)).style.display="block"; }
        }
    }
}

為列表項綁定事件

function aet(objid, tagid, etp){ // 為 LI 加上事件
    // objid:容器ID  tagid:組別  etp:觸發類型 0-onmouseover 1-onclick
    var iif = 0;
    var lis = objid.getElementsByTagName('li');
    requestType = tagid;
    url = "tab/"+requestType+"_1.Html";
    ajccache[url] = getObject(requestType+'_cnt').innerHTML;
    for(var iy=0; iy<lis.length; iy++){
        if(iif==0){
            iif = 1;
            with(lis[iy]){
                if(etp==0){
                    lis[iy].onmouseover = function(){
                        if(this.status=="active") return;
                        requestType = attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1);
                        idn = parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length));
                        clearTimeout(waitInterval);
                        waitInterval = window.setTimeout("TabNews(requestType,idn);", MouseDelayTime);
                    }
                    lis[iy].onmouseout = function(){ if(this.status=="active") return; clearTimeout(waitInterval); }
                }
                if(etp==1){
                    lis[iy].onmouseover = function(){}
                    lis[iy].onclick = function(){
                        if(this.status=="active") return;
                        requestType = attributes.getNamedItem('id').value.substring(0, attributes.getNamedItem('id').value.length-1);
                        idn = parseInt(attributes.getNamedItem('id').value.substring(attributes.getNamedItem('id').value.length-1, attributes.getNamedItem('id').value.length));
                        var loadstatustext = "<div class='loading'><img src='https://www.pushad.com/Info/images/loading.gif' /> Loading request content, please wait...</div>";
                        getObject(requestType+'_cnt').innerHTML = loadstatustext;
                        TabNews(requestType, idn);
                    }
                }
            }
        } else { iif=0; }
    }
}

aet 函數為每個 <li> 動態綁定事件:滑過模式(etp=0)用 setTimeout 加了 150 毫秒的延遲,避免鼠標快速劃過時頻繁觸發;點擊模式(etp=1)則直接切換並顯示 Loading 提示。通過容器 ID、組別和觸發類型三個參數,同一套邏輯即可複用到多組標簽導航上。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved