【问题标题】:jQuery mobile show loader for custom loading process?用于自定义加载过程的 jQuery 移动显示加载器?
【发布时间】:2013-05-02 10:58:55
【问题描述】:

我正在构建一个包含丰富图像和交互元素的 Web 应用程序。由于这些原因,我只想在加载所有图像后显示页面:

$(document).on('pageinit', function(){
    $('.ui-content').hide();
    var imgs = $(".ui-content img").not(function() { return this.complete; });
    var count = imgs.length;
    if (count) {
        imgs.load(function() {
            count--;
            if (!count) {
                $(".ui-content").show()
            }
        });
    } else {
        $(".ui-content").show();
    }
});

我需要 a) 完全移除加载器并用我自己的替换它,或者 b) 让加载器保持运行直到上述功能完成。

如何移除加载程序或保持它直到不需要?

【问题讨论】:

  • 通常你可以打电话给$.mobile.loading('hide');$.mobile.loading('show');
  • 你试过 $.mobile.loading('hide'); ?
  • @m90 试过了,不行!

标签: jquery css jquery-mobile setinterval jquery-mobile-loader


【解决方案1】:

jQuery Mobile 自定义加载器

解决方案

工作 jsFiddle:http://jsfiddle.net/Gajotres/vdgB5/

Mobileinit 事件必须在 jQuery Mobile 初始化之前和 jQuery 之后初始化。还必须对 css 进行一些额外的更改才能使其正常工作。

首先,我们需要覆盖默认的ui-loader-default 类,因为它的不透明度很低,而且最终的微调器很难看到。随心所欲地更改不透明度值。

.ui-loader-default {
    opacity: 1 !important;      
}

这是我们的微调器。

.custom-spinner {
    width: 37px !important;
    height: 37px !important;
    background-image:url('http://pictures.reuters.com/ClientFiles/RTR/Images/ajax-loader.gif');
    display: block;
}

这是一个工作示例:

代码示例

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <style>

            .ui-loader-default {
                opacity: 1 !important;      
            }

            .custom-spinner {
                width: 37px !important;
                height: 37px !important;
                background-image:url('http://pictures.reuters.com/ClientFiles/RTR/Images/ajax-loader.gif');
                opacity: 1 !important;
                display: block;
            }
        </style>
        <script type="text/javascript" src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>
        <script>
            $( document ).bind( 'mobileinit', function(){
                $.mobile.loader.prototype.options.text = "loading";
                $.mobile.loader.prototype.options.textVisible = false;
                $.mobile.loader.prototype.options.theme = "a";
                $.mobile.loader.prototype.options.html = "<i class='custom-spinner'></i>";
            }); 
        </script>       
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
        <script>
            $(document).on('pageshow', '#index', function(){        
                $.mobile.loading( 'show');          
            }); 
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">

            </div>

            <div data-theme="a" data-role="footer" data-position="fixed">

            </div>
        </div> 
    </body>
</html>   

jQuery mobile ajax loader 的程序化执行

一些浏览器,包括像 Chrome 这样的 webkit 浏览器有 jQuery 移动 ajax 加载器的程序执行。它们可以使用 serinterval 手动执行,如下所示:

$(document).on('pagebeforecreate', '#index', function(){     
    var interval = setInterval(function(){
        $.mobile.loading('show');
        clearInterval(interval);
    },1);    
});

$(document).on('pageshow', '#index', function(){  
    var interval = setInterval(function(){
        $.mobile.loading('hide');
        clearInterval(interval);
    },1);      
});

【讨论】:

  • 使用此代码我可以点击后页,是否有任何选项不允许在加载时点击页面。
【解决方案2】:

不是 100% 确定您要做什么。在文档中,您可以使用 loader 做一些事情:

http://api.jquerymobile.com/page-loading

正如其他人所说,您可以通过手动调用来使加载器出现/消失。

$.mobile.loading("show");

$.mobile.loading("hide");

【讨论】:

    【解决方案3】:

    我在引导时使用的一种技术是将第一个 data-role="page" div 作为加载屏幕。引导后,我使用 changePage 以编程方式转换到主页:

    $.mobile.changePage( "#home" , { reverse: false, changeHash: false } );
    

    这是一个小提琴。
    http://jsfiddle.net/puleos/Esf7H/

    【讨论】:

      猜你喜欢
      • 2014-03-02
      • 1970-01-01
      • 2012-11-08
      • 2012-10-30
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多