如何在jquery mobile中显示非ajax导航中的加载程序?

时间:2012-05-21 07:34:46

标签: jquery jquery-mobile

我在jquery mobile中使用非基于ajax的导航,因为我想在每个页面上加载并运行特定的脚本。但问题是,当我在jquery mobile中使用非基于ajax的导航时。装载机没有出现。我使用

禁用了ajax
data-ajax="false"

每个锚链接。

有没有快速的方法来显示加载器的每个页面转换而无需编写自定义函数?

2 个答案:

答案 0 :(得分:2)

我认为没有禁用ajax的微调器是可能的。我的意思是你可以在页面加载之前或之后将它闪烁一秒钟,但这有点挫败了目的。但我知道在特定页面上加载和运行特定脚本是可能的。所以也许你的问题应该是如何让特定的脚本在特定的JQM页面中运行?

绑定到pageinit将帮助您为特定页面执行自己的javascript。以下内容仅在加载了id为page2的JQM页面时执行。只需将其放在外部脚本中,然后在页面的头部链接即可。

$(document).on('pageinit','#page2',function(){
    $('#appendMe').append('This text was appended');    
});

如果要加载外部脚本/库,请使用$ .getScript();方法。在我的示例中,当加载id为page3的JQM页面时,我将加载并执行spin.js库。 Spin.js只是在页面中放置一个微调器。

​​$(document).on('pageinit','#page3',function(){
    $.getScript('http://fgnass.github.com/spin.js/dist/spin.min.js', function(){
        //the following code gets executed after spin.js is loaded
        var opts = {
            lines: 13, // The number of lines to draw
            length: 7, // The length of each line
            width: 4, // The line thickness
            radius: 10, // The radius of the inner circle
            rotate: 0, // The rotation offset
            color: '#000', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: false, // Whether to use hardware acceleration
            className: 'spinner', // The CSS class to assign to the spinner
            zIndex: 2e9, // The z-index (defaults to 2000000000)
            top: 'auto', // Top position relative to parent in px
            left: 'auto' // Left position relative to parent in px
        };
        var target = document.getElementById('spin');
        var spinner = new Spinner(opts).spin(target);
    });
});​​​​​​​​​​​​​​​​

这是一个jsfiddle来说服你我不只是把这一切都搞定。和合

答案 1 :(得分:1)

有一个客户端方法来显示/隐藏加载器:

$.mobile.showPageLoadingMsg();

$.mobile.hidePageLoadingMsg();