2014-06-24 150 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