2014-06-24 155 views
0
$(function(){ 
    $("a[rel='tab']").click(function(e){ 

    //get the link location that was clicked 
    pageurl = $(this).attr('href'); 
    $("#Content").fadeOut(800); 

    setTimeout(function(){ 
     $.ajax({url:pageurl+'?rel=tab',success: function(data){ 

     $("#Content").fadeIn(900); 
     $('#Content').html(data); 

     }}); 
    }, 900); 
    //to get the ajax content and display in div with id 'content' 

    //to change the browser URL to 'pageurl' 
    if(pageurl!=window.location){ 
     window.history.pushState({path:pageurl},'',pageurl); 
    } 

    return false; 
    }); 

}); 

我想通過ajax更改內容與更改網址,,我使用上面的代碼,但它是使網站非常緩慢。點擊網站中的任何頁面時,如何快速找到它?更改網址並顯示ajax內容

謝謝

+0

平均用戶不喜歡等待超過3秒或更少的內容,除非預計長時間操作。只有在用戶點擊後900毫秒才能打電話,這樣您可以淡出網頁是其中的三分之一。考慮縮短超時和淡出/插入。除此之外的任何其他延遲都是服務器根據內容大小需要回復給您和/或渲染時間的時間。 – Nope

+0

也許加載()而不是ajax – turson

+0

@turson:'load()'是一個簡寫'ajax'。一樣。 – Nope

回答

0

fadeIn()/fadeOut()/setTimeout()延遲正在放慢速度。減少或刪除它們。

$(function(){ 
    $("a[rel='tab']").click(function(e){ 

    //get the link location that was clicked 
    pageurl = $(this).attr('href'); 
    $("#Content").fadeOut(50); 

    $.ajax({url:pageurl+'?rel=tab',success: function(data){ 

     $('#Content').html(data); 
     $("#Content").fadeIn(25); 

    }}); 

    //to get the ajax content and display in div with id 'content' 

    //to change the browser URL to 'pageurl' 
    if(pageurl!=window.location){ 
     window.history.pushState({path:pageurl},'',pageurl); 
    } 

    return false; 
    }); 

}); 
+0

謝謝,我會嘗試 – user3771471