0

有没有一种简单的方法可以$.mobile.showPageLoadingMsg()在一个元素/区域上显示微调器()?

我正在通过 加载这个元素的内容AJAX,所以在它完成之前我必须阻止它并显示这个微调器。

4

2 回答 2

1

您可以将加载微调器的 CSS 位置设置为显示在某个区域上。这是一个代码示例,它显示和隐藏元素上的加载微调器:

//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);

这是一个演示:http: //jsfiddle.net/CkUZf/

对于上面的演示(链接),我将这个 CSS 用于覆盖元素:

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

也可以将 loader 元素附加到您要“加载”的任何容器,但$.mobile.showPageLoadingMsg()会自动重置 loader 元素,因此您必须在 jQuery Mobile 包含中禁用该代码(这就是为什么我选择了更轻的 CSS 版本多于)。

更新

这可能更像你的想法:

$.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%'
                    });
                });
            }
        });
    }
};

然后你只需在一个 jQuery 对象上调用这个方法:

$('#some-container').customMobileSpinner({
    url : 'some-url'
});
于 2012-08-22T16:18:33.033 回答
0

恐怕你不能。查看文档,您可以自定义消息或隐藏微调器,但不能使其适合元素。如果您只想在某些元素加载时显示微调器,请使用该方法的beforeSendandcomplete选项ajax来隐藏/显示它

于 2012-08-21T11:50:48.327 回答