最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display a loader when the page loads - Stack Overflow

programmeradmin0浏览0评论

How do I display a loader when the page loads and hide it when it is loaded?

$(document).ready(function() {
    $('.windowLoader').show().fadeOut(2000);
});

Displays the loader long after the page start loading and sometime the 2000 ms duration of the fadeOut event pleted before the page has loaded.

Is there anyway to executed the display of the loader as soon as the DOM is ready and remain it visible until the page is loaded (not the images) and then hide the loader?

How do I display a loader when the page loads and hide it when it is loaded?

$(document).ready(function() {
    $('.windowLoader').show().fadeOut(2000);
});

Displays the loader long after the page start loading and sometime the 2000 ms duration of the fadeOut event pleted before the page has loaded.

Is there anyway to executed the display of the loader as soon as the DOM is ready and remain it visible until the page is loaded (not the images) and then hide the loader?

Share Improve this question asked Aug 22, 2013 at 11:53 Tapas BoseTapas Bose 29.9k78 gold badges220 silver badges338 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Why not put the loader directly in the document and then on ready remove it using jQuery? E.G.

<div id="loading"></div>

$(document).ready(function() {
    $("#loading").fadeOut(function() {
        $(this).remove(); // Optional if it's going to only be used once.
    });
});

Else, if you're doing other things within your $(document).ready() then .fadeIn() (/show/create) your loading bar at the top of the method, do your extensive code, and then at the bottom call the .fadeOut()

As kindly suggested if you're worried about people without JavaScript viewing the loading bar then also add the following:

<noscript>
    <style> #loading { display:none; } </style>
</noscript>

it should help customize it according to your code

  $(document).ready(function () {
            // calculate height
            var screen_ht = jQuery(window).height();
            var preloader_ht = 5;
            var padding = (screen_ht / 5) - preloader_ht;
            jQuery("#preloader").css("padding-top", padding + "px");


            // loading animation using script 

            function anim() {
                jQuery("#preloader_image").animate({ left: '1px' }, 2000,
                function () {
                    jQuery("#preloader_image"), animate({ left: '1px' }, 2000);
                }
                );
            }
            //anim();
        });
    function hide_preloader() {
    // To apply Fade Out Effect to the Preloader 
    jQuery("#preloader").fadeOut(1000);
    }
    </script>
<style>
    #preloader {background: #1c1c1c;position:fixed;left:0px; top:0px; width:100%; height:100%; text-align:center;color:#fff;z-index: 100000000;}
    #preloader div {width:228px;height:240px;margin:auto;padding:10px 0;text-align:center;overflow:hidden;}
    #preloader_image {width:228px;height:240px;position: relative;left:0px;top:-10px;}
</style>
</head>

<body id="home" onload="hide_preloader()">
    <div id="preloader">
        <div>
            <div id="preloader_image">
                <img src="loading.gif" style="position: absolute;bottom: 0;left: 35%;">
            </div>
        </div>
    </div>
    </body>
发布评论

评论列表(0)

  1. 暂无评论