jquery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较...

想要添加这个效果,先来弄明白页面的加载和事件执行顺序,看这个简单例子:

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head >
 3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4     <title>验证加载顺序</title>
 5     <script src="../Scripts/jquery-1.7.1.js"></script>
 6     <link href="../Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
 7     <script src="../Scripts/Mobile/jquery.mobile-1.4.0.min.js"></script>
 8     <script>
 9         alert("DOM还没加载");        
10         window.onload = function () {           
11             alert('onload,图片加载完');          
12         }
13         $(document).ready(function () {
14             alert('ready,dom加载完');    
15         })      
16     </script>
17 </head>
18 <body >
19     <form id="form1" runat="server">            
20         <img src="http://images.aviary.com/imagesv5/feather_default.jpg" />
21         <img src="http://car0.autoimg.cn/car/upload/2015/1/8/v_20150108092921264345010.jpg" />
22     </form>
23 </body>
24 </html>

执行结果:9行>14行>11行,9行代码放置的上下位置不同,结果依然是一样的。弄明白上面的顺序之后,如果想让页面在加载之前显示jquery mobile的加载器,然后等页面数据请求执行完,图片等多媒体加载完之后,再关闭加载器的话,就可以按照以下思路来解决:

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head >
 3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4     <title>验证加载顺序</title>
 5     <script src="../Scripts/jquery-1.7.1.js"></script>
 6     <link href="../Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
 7     <script src="../Scripts/Mobile/jquery.mobile-1.4.0.min.js"></script>
 8     <script>
 9         setTimeout('showLoader()', 100);//这里要延迟一下,直接调用无法显示加载器
10         //显示加载器.for jQuery Mobile 1.2.0    
11         function showLoader() {
12             $.mobile.loading('show', {
13                 text: '正在登陆...', //加载器中显示的文字  
14                 textVisible: true, //是否显示文字  
15                 theme: 'a',        //加载器主题样式a-e  
16                 textonly: false,   //是否只显示文字  
17                 html: ""           //要显示的html内容,如图片等  
18             });
19         }
20         //隐藏加载器.for jQuery Mobile 1.2.0  
21         function hideLoader() {
22             $.mobile.loading('hide');
23         }
24         window.onload = function () {            
25             hideLoader();
26             //setTimeout('hideLoader()', 5000);//延迟5秒,模拟图片和多媒体加载耗时
27         }
28         $(document).ready(function () {           
29             //setTimeout('hideLoader()', 5000);//延迟5秒,模拟页面请求数据耗时,ajax异步请求等放在这里
30         })
31     </script>
32 </head>
33 <body >
34     <form id="form1" runat="server">            
35         <img src="http://images.aviary.com/imagesv5/feather_default.jpg" />
36         <img src="http://car0.autoimg.cn/car/upload/2015/1/8/v_20150108092921264345010.jpg" />
37     </form>
38 </body>
39 </html>

说明:

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

2)关闭加载器可以放在document.ready或者window.onload中,具体看页面的执行情况需要。

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

 

转载于:https://www.cnblogs.com/hiflora/p/4269408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值