仅在一个元素上显示微调器($.mobile.showPageLoadingMsg())

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

本文关键字:showPageLoadingMsg mobile 显示 元素 一个      更新时间:2023-09-26

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

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

您可以将加载微调器的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-include中禁用该代码(这就是为什么我使用了上面较轻的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'
});

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

相关文章:
  • 没有找到相关文章