简体   繁体   English

如何在iframe加载网站时显示加载gif?

[英]How to show loading gif while iframe is loading up a website?

I have inc/content.php file which contains iframe with src= to my domain. 我有inc / content.php文件,其中包含iframe和src =到我的域名。

On my index.php page I have button <button id="nextButton" onClick="viewNext();return false;">New</button> which when clicked calls following JavaScript function: 在我的index.php页面上,我有按钮<button id="nextButton" onClick="viewNext();return false;">New</button> ,点击后调用以下JavaScript函数:

<script type="text/javascript">
function viewNext()
{
  $('#content').load('inc/content.php');
}
</script>

Function loads iframe from inc/content.php file into index.php page <div id="content"></div> 函数将inc/content.php文件中的iframe加载到index.php页面<div id="content"></div>

How can I show loading.gif image while inc/content.php file gets loaded into index file? inc/content.php文件加载到索引文件中时,如何显示loading.gif图像?

You can display the loading image before loading the page, and hide it when done (use the second parameter for adding a callback which is called when the request has completed): 您可以在加载页面之前显示加载图像,并在完成后隐藏它(使用第二个参数添加在请求完成时调用的回调):

$("#loading").show();
$("#content").load("inc/content.php", function () {
    $("#loading").hide();
});

Obviously, you need an element in your document with the ID loading , like: 显然,您需要文档中的元素loading ID,例如:

<img id="loading" src="loading.gif" alt="Loading">
<script type="text/javascript">
function viewNext()
{
  show_loading()
  $('#content').load('inc/content.php' , hide_loading );
}

function show_loading(){ $('.loading').show() }
function hide_loading(){ $('.loading').hide() }
</script>

While "loading" is the classname of the div that contains loading.gif. 而“loading”是包含loading.gif的div的类名。


For the loading.gif image, you should put it into a div which can "float" at the center of your page like this : 对于loading.gif图像,你应该将它放入一个可以“漂浮”在页面中心的div,如下所示:

HTMLCODE: HTMLCODE:

<div style="position:absolute; top:100px; left:50%;" class="loading">
     <img src="/img/loading.gif" />
</div>

You can change the appearing position of the loading image by changing the inline style "top:... ; left:....;". 您可以通过更改内嵌样式“top:...; left:....;”来更改加载图像的显示位置。 If you want to posiion the loading bases on screen, instead of the page's position, then replace position:absolute; 如果你想在屏幕上设置加载基础,而不是页面的位置,那么替换position:absolute; by: position:fixed; 通过: position:fixed; (although this won't work with IE) (虽然这不适用于IE)

If you want it to look nice, here is solution i use at job which puts a modal in the screen and a loading gif in the middle. 如果你希望它看起来不错,这里是我在工作中使用的解决方案,它在屏幕中放置一个模态,在中间放置一个加载的gif。

<script type="text/javascript">
function viewNext()
{  
  $('#loading').attr( 'src', 'some_path/loading.gif' );
  $('#container').block( { message: $( '#loading' ) } );
  $('#content').load('inc/content.php' , function(){ $('#container').unblock(); } );
}
</script>

it uses jquery.blockUI.js which allows me to block certain container, may be a div or the whole screen. 它使用jquery.blockUI.js,它允许我阻止某些容器,可能是div或整个屏幕。

The html you need is this 你需要的HTML是这个

<img id="loading" style="display: none;" src="" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM