我在这个链接How to show ajax loading gif animation while the page is loading?找到了我想要的内容,但我不知道如何在我的网站上实现这一点。
我希望在页面加载时显示加载GIF ,并在页面完全加载PHP结果后自动消失。
在网站上我使用一个页面模板(index.php),我根据用户的查询动态地加载了该页面中的所有数据。
提前谢谢。
答案 0 :(得分:2)
你会在这里找到你需要的一切
http://mycodeprograms.blogspot.ca/2012/10/how-to-add-loader-while-page-load.html
答案 1 :(得分:2)
当页面完全加载时,首先发生的是<body>
的{{1}}事件触发。这是你让图像消失的地方。
例如,让onload
标记看起来像这样:
<body>
页面正文中的某处为您的GIF提供ID:
<body onload="makeLoadingGifDisappear()">
然后在页面的JavaScript中,让<img src="loadinggif.gif" id="myLoadingGif">
函数隐藏GIF:
makeLoadingGifDisappear
答案 2 :(得分:2)
嗯,这是我的解决方案。
1)CSS部分
#loadgif {
padding-top:2px;
font: 10px #000;
text-align:center;
vertical-align:text-top;
height: 80px;
width: 130px;
/* Centering the div to fit any screen resolution */
top: 50%;
left: 50%;
margin-top: -41px; /* Div height divided by 2 including top padding */
margin-left: -65px; /* Div width divided by 2 */
position: absolute;
display: none; /* JS will change it to block display */
position: fixed;
z-index: 1000;
/* Loading GIF set as background of the div */
background: #FFF url('../img/loader.gif') 50% 75% no-repeat;
/* Misc decoration */
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
border:#7580a8 solid 1px;
}
2)HTML部分
将div标签放在body标签内的任何位置。
<!-- With or without text -->
<div id="loadgif">Loading...</div>
3)Javascript部分
我制作了两个函数,一个用于显示,另一个用于隐藏div。
<script type="text/javascript">
// Hide function by changing <div> style display attribute back to none
function hideloadgif() {
document.getElementById('loadgif').style.display = 'none';
}
// Show function by changing <div> style display attribute from none to block.
function showloadgif() {
document.getElementById('loadgif').style.display = 'block';
}
// Making sure that any other event running in the background isn't affected
if (window.addEventListener) { // Mozilla, Netscape, Firefox
window.addEventListener('load', WindowLoad, false);
} else if (window.attachEvent) { // IE
window.attachEvent('onload', WindowLoad);
}
// Call the hideloadgif() function on click event,
// with interval time set to 3 seconds to hide the <div>
function WindowLoad(click) {
setInterval("hideloadgif()",3000)
}
</script>
4)显示div。 在任何地方使用onlick =“”事件调用函数showloadgif()。
例如
<img src="abc/def.jpg" onlick="showloadgif()">
单击图像后,将显示div,同时hideloadgif()将在3秒内触发并隐藏div。
function WindowLoad(click) {
setInterval("hideloadgif()",3000)
}