gpt4 book ai didi

javascript - 完全加载后使用 .load 查看 php 远程文件

转载 作者:行者123 更新时间:2023-12-03 05:55:44 27 4
gpt4 key购买 nike

我使用一个简单的代码来显示容器中的 php 文件,而无需使用 .load 加载页面,并具有显示和隐藏加载动画图像的功能

<style>
.loadingbg{width: 100%; height: 100%; position: fixed; top: 0; left: 0; background: #84ffbf;
display: none;
}
.loadingbg img{width: 60px; height: 60px; position: absolute; left: 48%; top: 48%;}
</style>

<script>
$(document).on('click','a',function (e) {
$(".loadingbg").css('display','block');
e.preventDefault();
var url = $(this).attr('href');
$('#container').load(url+ '#content',function () {
$(".loadingbg").css('display','none');
});
});
</script>

<div class="loadingbg"><img src="images/page-loader.gif"></div>
<a href="contact.php">contact</a>
<a href="about.php">about</a>

<div id="container">
<h1>index</h1>
</div>

因此,当我单击链接时,它会显示背景和小动画图像以加载其他页面,而无需更改网址,但它会快速获取文本内容,并且 loadbg 会消失,并开始在新网页中加载图像。我想要的不是隐藏loadingbg,直到远程php文件完全加载(包括图像)。

Demo

最佳答案

加载内容后,您必须确保所有图像均已加载。

  1. 在加载回调函数中,您可以使用 imagesLoaded library (或任何其他验证图像加载的库)。另外,在 anchor 上单击时,我会隐藏 #container ,当所有图像加载完毕后,再次显示它:
$(document).on('click','a',function (e) {
$(".loadingbg").css('display','block');
$("#container").hide();
e.preventDefault();
var url = $(this).attr('href');
$('#container').load(url+ '#content',function () {
$('#container').imagesLoaded( function() {
// images have loaded
$(".loadingbg").css('display','none');
$("#container").show();
});

});
});

关于javascript - 完全加载后使用 .load 查看 php 远程文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937435/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com