如何更改jquerymobile中默认的加载ajax加载程序gif

how to change the default load ajax loader gif in jquery mobile

本文关键字:加载 ajax 程序 gif 默认 何更改 jquerymobile      更新时间:2023-09-26

我已经看过jquery mobile的文档,但不知道如何在我的移动网站上集成它。文件在

http://jquerymobile.com/demos/1.2.0-pre/docs/pages/loader.html

事实上,gif图片在2.x安卓设备上没有动画,所以我正在考虑制作一个纯文本的预加载小部件。

我试着通过像这样的css来更改它

.ui-icon-loading {
            background: url(themes/images/custom-ajax-loader.gif);
        }

但是新的imag没有正确缩放,旧的背景仍然可见。

我完全不懂javascript。有人能帮我吗?

您提到的是jQM 1.2 Alpha文档,所以我的答案基于这个jQM版本。

下面可以找到两个选项来更改默认加载程序映像。

解决方案1

如jQM 1.2 Alpha文档中所述:

当jQuery Mobile启动时,它会在文档对象上触发一个mobileinit事件。要覆盖默认设置,请绑定到mobileinit。因为mobileinit事件是立即触发的,所以在加载jQueryMobile之前,您需要绑定事件处理程序。按以下顺序链接到您的JavaScript文件:

<script src="jquery.js"></script>
<script src="custom-scripting.js"></script>
<script src="jquery-mobile.js"></script>

要全局配置加载对话框,可以在mobileinit事件期间在其原型上定义以下设置:

$( 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 = "";
});

下面您可以找到一个工作示例,在该示例中,您可以在html原型选项中完全自定义加载程序。

示例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Page Title</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-alpha.1/jquery.mobile-1.2.0-alpha.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script>
            $(document).bind( 'mobileinit', function(){
              $.mobile.loader.prototype.options.text = "loading";
              $.mobile.loader.prototype.options.textVisible = true;
              $.mobile.loader.prototype.options.theme = "a";
              $.mobile.loader.prototype.options.textonly = false;
              $.mobile.loader.prototype.options.html = "<span class='ui-bar ui-overlay-c ui-corner-all'><img src='../images/my-custom-image.png' /><h2>loading</h2></span>";
            });
        </script>
        <script src="http://code.jquery.com/mobile/1.2.0-alpha.1/jquery.mobile-1.2.0-alpha.1.min.js"></script>
        <script>
            $(document).on("click", ".show-page-loading-msg", function() {          
                $.mobile.loading('show');
            });
        </script>
    </head> 
    <body>
        <!-- /page -->
        <div data-role="page" class="page-map" style="width:100%; height:100%;">
            <!-- /header -->
            <div data-role="header" data-theme="b">
                <h1>Test</h1>
            </div>
            <!-- /content -->
            <div data-role="content" class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                <button class="show-page-loading-msg">Click</button>
            </div>
        </div>
    </body>
</html>

解决方案2

覆盖用于描述页面加载程序的默认CSS样式。

已编辑

对于jQM 1.1.1版本,有以下可配置选项:

  • loadingMessage字符串,默认值:"loading"
    设置加载页面时显示的文本。如果设置为false,则根本不会显示该消息
  • loadingMessageTextVisible布尔值,默认值:false
    显示加载消息时,文本是否应可见。对于加载错误,文本始终可见
  • loadingMessageTheme字符串,默认值:"a"
    当文本可见时,加载消息框使用的主题

代码示例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Page Title</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script>
            $(document).bind( 'mobileinit', function(){
               $.mobile.loadingMessageTheme = 'e';
               $.mobile.loadingMessageTextVisible = true;
                $.mobile.loadingMessage = "test";
            });
        </script>
        <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
        <script>
            $(document).on("click", ".show-page-loading-msg", function() {          
                $.mobile.showPageLoadingMsg();
            });
        </script>
    </head> 
    <body>
        <!-- /page -->
        <div data-role="page" class="page-map" style="width:100%; height:100%;">
            <!-- /header -->
            <div data-role="header" data-theme="b">
                <h1>Test</h1>
            </div>
            <!-- /content -->
            <div data-role="content" class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                <button class="show-page-loading-msg">Click</button>
            </div>
        </div>
    </body>
</html>

此外,您可以尝试覆盖用于设置jQM加载程序样式的默认CSS。更具体地说,您可以修改或覆盖加载屏幕部分中的样式和加载图标中用于描述页面加载程序的样式。

您可以在这里找到jQM1.1.1中使用的CSS。

/* loading icon */
.ui-icon-loading {
    background: url(images/ajax-loader.gif);
    background-size: 46px 46px;
}
/* loading screen */
.ui-loading .ui-loader { display: block; }
.ui-loader { display: none; z-index: 9999999; position: fixed; top: 50%; left: 50%; border:0; }
.ui-loader-default { background: none; opacity: .18; width: 46px; height: 46px; margin-left: -23px; margin-top: -23px; }
.ui-loader-verbose { width: 200px; opacity: .88; box-shadow: 0 1px 1px -1px #fff; height: auto; margin-left: -110px; margin-top: -43px; padding: 10px; }
.ui-loader-default h1 { font-size: 0; width: 0; height: 0; overflow: hidden; }
.ui-loader-verbose h1 { font-size: 16px; margin: 0; text-align: center; }
.ui-loader .ui-icon { background-color: #000; display: block; margin: 0; width: 44px; height: 44px; padding: 1px; -webkit-border-radius: 36px; -moz-border-radius: 36px; border-radius: 36px; }
.ui-loader-verbose .ui-icon { margin: 0 auto 10px; opacity: .75; }
.ui-loader-textonly { padding: 15px; margin-left: -115px; }
.ui-loader-textonly .ui-icon { display: none; }
.ui-loader-fakefix { position: absolute; }