dsgft1486 2013-01-29 15:53
浏览 107
已采纳

页面加载GIF - 有人可以解释下面的答案吗?

I found what I was looking for on this link How to show ajax loading gif animation while the page is loading? but I dont know how to implement this on my website.

I want a loading GIF to appear while the page loads and disappears by itself once the page is completely loaded with PHP results.

On the website I use one page template (index.php) and I have all data loaded in that page dynamically according to the user's query.

Thank you in advance.

  • 写回答

3条回答 默认 最新

  • doumi1099 2013-01-30 17:51
    关注

    Well, this is my solution.

    1) CSS part

    #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 part

    Place div tag within body tag anywhere.

    <!-- With or without text -->
    <div id="loadgif">Loading...</div>
    

    3) Javascript part

    I made two functions, one to show and another to hide the 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) Showing the div. Call function showloadgif() using onlick="" event anywhere.

    For example

    <img src="abc/def.jpg" onlick="showloadgif()">
    

    Once the image is clicked, the div will appear and at the same time, hideloadgif() will trigger and hides the div within 3 seconds.

    function WindowLoad(click) {
        setInterval("hideloadgif()",3000)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP