Jump to content

AJAX loading bar


c_pattle

Recommended Posts

This requires jQuery.

 

The CSS class to make.

.hidden { display: none !important; }

 

The HTML code to embed.

<div class="hidden" id="busy_container">
    <img alt="Busy" src="/images/working.gif" id="busy_img">
</div>

 

The JavaScript to make the screen busy.

/**
* Make the screen look busy.
*
* @param flag bool True makes page look busy, false makes page look ready.
* @param target string Node ID of element to mask out
* @param target string Node ID of element to center image over
*/
function busy( flag, target, center ) {
    var top, left, width, height, position, busy_container, busy_img;
    // get the busy_container and busy_img
    busy_container = $('#busy_container');
    busy_img = $('#busy_img');
    if( flag === true ) {
        // Turn target id into jquery object
        target = $('#' + target);
        // Turn center id into jquery object
        center = $('#' + center);
        // Need the target's position
        position = target.position();
        // top and left of target will go here
        top = position.top + 1;
        left = position.left + 1;
        // target will be this high and wide
        width = target.innerWidth();
        height = target.innerHeight();
        //
        busy_container.css( 'position', 'absolute' );
        busy_container.css( 'background-color', '#eaeaea' );
        busy_container.css( 'opacity', '0.5' );
        busy_container.css( 'top', top + 'px' );
        busy_container.css( 'left', left + 'px' );
        busy_container.css( 'width', width + 'px' );
        busy_container.css( 'height', height + 'px' );
        //
        busy_img.css( 'position', 'absolute' );
        busy_img.css( 'top', Math.floor((center.innerHeight() - busy_img.height()) / 2) + 'px' );
        busy_img.css( 'left', Math.floor((center.innerWidth() - busy_img.width()) / 2) + 'px' );
        //
        busy_container.removeClass( 'hidden' );
    } else if( flag === false ) {
        busy_container.addClass( 'hidden' );
    }
};

 

Let's assume you have a form with id frm_add_user.  In the onsubmit() handler you might add this code:

busy( true, 'frm_add_user', 'frm_add_user' );

When the XHR request finishes you will call this:

busy( false );

 

Let's assume you have a form with id frm_login inside a larger container div with id container.  When the form is submitted you want to make the entire container busy but center the busy image over the form.  You will call:

busy( true, 'container', 'frm_login' );

When the XHR request finishes you call the same thing:

busy( false );

 

I wrote this as a one-off for my current project.  It may require tweaking to work in yours!

Link to comment
https://forums.phpfreaks.com/topic/217497-ajax-loading-bar/#findComment-1129589
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.