这个概念很简单。设置一个包含所有页面内容的包装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内,该怎么办? –