天天看点

jquery mobile在页面加载时添加加载中效果

<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>验证加载顺序</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script>
        setTimeout('showLoader()', 100);//这里要延迟一下,直接调用无法显示加载器
        //显示加载器.for jQuery Mobile 1.2.0    
        function showLoader() {
            $.mobile.loading('show', {
                text: '正在登陆...', //加载器中显示的文字  
                textVisible: true, //是否显示文字  
                theme: 'a',        //加载器主题样式a-e  
                textonly: false,   //是否只显示文字  
                html: ""           //要显示的html内容,如图片等  
            });
        }
        //隐藏加载器.for jQuery Mobile 1.2.0  
        function hideLoader() {
            $.mobile.loading('hide');
        }
        window.onload = function () {            
            hideLoader();
            //setTimeout('hideLoader()', 5000);//延迟5秒,模拟图片和多媒体加载耗时
        }
        $(document).ready(function () {           
            //setTimeout('hideLoader()', 5000);//延迟5秒,模拟页面请求数据耗时,ajax异步请求等放在这里
        })
    </script>
</head>
<body >
    <form id="form1" runat="server">            
        <img src="http://images.aviary.com/imagesv5/feather_default.jpg" />
        <img src="http://car0.autoimg.cn/car/upload/2015/1/8/v_20150108092921264345010.jpg" />
    </form>
</body>
</html>      

1)9行的代码要稍作延迟执行,否则有可能上面引用的js文件还没有加载完,这时候调用showLoader方法,是无法正确执行,就不能显示加载器

可以放在document.ready或者window.onload中,具体看页面的执行情况需要。

3)如果网速足够快,两个图片瞬间加载完成,有可能看不到明显的加载器显示和关闭的过程。