Tar
Tar

Reputation: 9015

Show spinner ($.mobile.showPageLoadingMsg()) on just one element?

Is there a simple way to show the spinner ($.mobile.showPageLoadingMsg()) on just one element/region ?

I'm loading this element's content via AJAX, so until it's finished I have to block it and show this spinner.

Upvotes: 0

Views: 1352

Answers (2)

Jasper
Jasper

Reputation: 76003

You can set the CSS position of the loading spinner to appear over a certain region. Here is a code example that both shows and hides the loading spinner over an element:

//this is an IIFE, it creates an enclosure around the code that separates it from global code
(function ($) {

    //a flag so we know what state the loading spinner is in
    var isShowing = false;

    //this example is binding to all link elements
    $('a').on('click', function () {

        //check if the loading spinner is already showing
        if (isShowing === false) {

            //the loader is not showing, so create an overlay in the container element
            var $con = $('#container').append('<div class="container-overlay"></div>');

            //now position the loader over the container
            $('.ui-loader').css({
                position : 'absolute',
                top      : ($con.offset().top + ($con.height() / 2)),
                left     : ($con.offset().left + ($con.width() / 2))
            });

            //fade-in the overlay and show the loading spinner
            $con.children('.container-overlay').fadeIn(500);
            $.mobile.showPageLoadingMsg();

            //set the flag so next time around we hide the spinner
            isShowing = true;
        } else {

            //fade-out the overlay
            $('#container').children('.container-overlay').fadeOut(500, function () {

                //remove the overlay from the DOM
                $(this).remove();

                //hide the loader
                $.mobile.hidePageLoadingMsg();

                //reset the CSS of the loader element
                $el.css({
                    position : 'fixed',
                    top      : '50%',
                    left     : '50%'
                });
            });

            //set the flag so next time around we show the loading spinner
            isShowing = false;
        }
    });​
})(jQuery);

Here is a demo: http://jsfiddle.net/CkUZf/

For the demo (link) above, I used this CSS for the overlay element:

#container .container-overlay {
    display    : none;
    height     : 100%;
    background : #000;
    background : rgba(0, 0, 0, 0.75);
}​

It would also be possible to append the loader element to whatever container you want to "load" but $.mobile.showPageLoadingMsg() automatically resets the loader element so you'd have to disable that code in the jQuery Mobile include (which is why I went this the lighter CSS version above).

Update

This is probably more like what you were thinking:

$.fn.customMobileSpinner = function (options) {

    var defaults = {
            url             : null,
            fadeDuration    : 500,
            bgColor         : 'rgba(0, 0, 0, 0.75)',
            bgColorFallback : '#000'
        };

    //merge the defaults and options objects
    options = $.extend({}, defaults, options);

    //make sure the URL is specified
    if (options.url !== null) {

        //only work with the first element passed-in
        var $element = this.eq(0);
        $element.append(
            $('<div class="container-overlay" />').css({
                display    : 'none',
                height     : '100%',
                width      : '100%',
                background : options.bgColorFallback,
                background : options.bgColor
            })
        ).children('.container-overlay').fadeIn(options.fadeDuration);

        //update loader CSS
        $('.ui-loader').css({
            position : 'absolute',
            top      : ($element.offset().top + ($element.height() / 2)),
            left     : ($element.offset().left + ($element.width() / 2))
        });

        //show spinner
        $.mobile.showPageLoadingMsg();

        //create AJAX call
        $.ajax({
            url : options.url,
            success : function (response) {
                $element.fadeOut(options.fadeDuration, function () {
                    $element.html(response).fadeIn(options.fadeDuration);
                    $.mobile.hidePageLoadingMsg();

                    //reset loader CSS
                    $(".ui-loader").css({
                        position : 'fixed',
                        top      : '50%',
                        left     : '50%'
                    });
                });
            }
        });
    }
};

Then you just call this method on a jQuery object:

$('#some-container').customMobileSpinner({
    url : 'some-url'
});

Upvotes: 1

davids
davids

Reputation: 6371

I'm afraid you can't. Take a look at the docs, you can just customize the message or hide the spinner, but not making it fit to a element. If you just want to show a spinner on some element loading, use the beforeSend and complete options of the ajax method to hide/show it

Upvotes: 0

Related Questions