嗯,这是我的解决方案。
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)
}