這個概念很簡單。設置一個包含所有頁面內容的包裝div,並通過AJAX將數據加載到該容器中。在AJAX請求之前,顯示覆蓋圖,當數據更新時,隱藏覆蓋圖。
CSS:
#overlay {
position:absolute;
width:100%;
height:100%;
background-color:rgba(255,255,255,0.8);
text-align:center;
z-index:999;
display:none;
}
#overlay span {
margin:200px auto 0 auto;
}
HTML:
<body>
<div id="overlay">
<span>Please wait while your page is loaded...
</div>
<div id="content">
Page content. <a class="ajax-link" href="#!page2" rel="page2.php">Click for page 2</a>
</div>
</body>
JQuery的:
$('#ajax-link').live('click',function(event){
event.preventDefault();
var $this = $(this),
$overlay = $('#overlay');
$.ajax({
url: $this.attr('rel'),
beforeSend: function(){
$overlay.fadeIn();
window.location.hash = $this.attr('href').replace('#','');
},
success: function(data){
$('#content').fadeOut().html(data).fadeIn();
$overlay.fadeOut();
}
// I'd recommend adding some error handling here as well, possibly
// update the overlay text with the error response and gracefully
// fail-over to the previous content.
});
});
// Since we are setting a hashbang when we load new pages, let's also support someone
// landing on one of these AJAX-loaded pages
$(function(){
if(window.location.hash){
if(window.location.hash.indexOf('#!') === 0){
$.ajax({
url: window.location.hash.replace('#!','') + '.php',
success: function(data){
$('#content').fadeOut().html(data).fadeIn();
}
});
}
}
});
如果抓取網頁,你的意思是實際去到一個新的頁面,日ere無法加載該頁面,而前一頁仍然可見(任何事情都是可能的,但它會過於複雜)。如果你的意思是你的意思,你可以在新頁面上創建一個100%div的覆蓋文本,將文本全部打開,然後通過設置document.ready或window.onload將其刪除(或者使用jQuery hide,一樣)。 – adeneo
如果頁面位於iFrame內,該怎麼辦? –