jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
想要添加这个效果,先来弄明白页面的加载和事件执行顺序,看这个简单例子:
<htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>验证加载顺序</title> <scriptsrc="../Scripts/jquery-1.7.1.js"></script> <linkhref="../Scripts/Mobile/jquery.mobile-1.4.0.min.css"rel="stylesheet"/> <scriptsrc="../Scripts/Mobile/jquery.mobile-1.4.0.min.js"></script> <script> alert("DOM还没加载"); window.onload=function(){ alert('onload,图片加载完'); } $(document).ready(function(){ alert('ready,dom加载完'); }) </script> </head> <body> <formid="form1"runat="server"> <imgsrc="http://images.aviary.com/imagesv5/feather_default.jpg"/> <imgsrc="http://car0.autoimg.cn/car/upload/2015/1/8/v_20150108092921264345010.jpg"/> </form> </body> </html>
执行结果:9行>14行>11行,9行代码放置的上下位置不同,结果依然是一样的。弄明白上面的顺序之后,如果想让页面在加载之前显示jquerymobile的加载器,然后等页面数据请求执行完,图片等多媒体加载完之后,再关闭加载器的话,就可以按照以下思路来解决:
<htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>验证加载顺序</title> <scriptsrc="../Scripts/jquery-1.7.1.js"></script> <linkhref="../Scripts/Mobile/jquery.mobile-1.4.0.min.css"rel="stylesheet"/> <scriptsrc="../Scripts/Mobile/jquery.mobile-1.4.0.min.js"></script> <script> setTimeout('showLoader()',100);//这里要延迟一下,直接调用无法显示加载器 //显示加载器.forjQueryMobile1.2.0 functionshowLoader(){ $.mobile.loading('show',{ text:'正在登陆...',//加载器中显示的文字 textVisible:true,//是否显示文字 theme:'a',//加载器主题样式a-e textonly:false,//是否只显示文字 html:""//要显示的html内容,如图片等 }); } //隐藏加载器.forjQueryMobile1.2.0 functionhideLoader(){ $.mobile.loading('hide'); } window.onload=function(){ hideLoader(); //setTimeout('hideLoader()',5000);//延迟5秒,模拟图片和多媒体加载耗时 } $(document).ready(function(){ //setTimeout('hideLoader()',5000);//延迟5秒,模拟页面请求数据耗时,ajax异步请求等放在这里 }) </script> </head> <body> <formid="form1"runat="server"> <imgsrc="http://images.aviary.com/imagesv5/feather_default.jpg"/> <imgsrc="http://car0.autoimg.cn/car/upload/2015/1/8/v_20150108092921264345010.jpg"/> </form> </body> </html>
说明:
1)9行的代码要稍作延迟执行,否则有可能上面引用的js文件还没有加载完,这时候调用showLoader方法,是无法正确执行,就不能显示加载器
2)关闭加载器可以放在document.ready或者window.onload中,具体看页面的执行情况需要。
3)如果网速足够快,两个图片瞬间加载完成,有可能看不到明显的加载器显示和关闭的过程。
以上所述是小编给大家介绍的jQuerymobile在页面加载时添加加载中效果document.ready和window.onload执行顺序比较,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!