导航首页 » 技术教程 » jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较 125 2024-02-24   

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证加载顺序</title>
<script src="http://www.gimoo.net/t/Scripts/jquery-1.7.1.js"></script>
<link href="http://www.gimoo.net/t/Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
<script src="http://www.gimoo.net/t/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 >
<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>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证加载顺序</title>
<script src="http://www.gimoo.net/t/Scripts/jquery-1.7.1.js"></script>
<link href="http://www.gimoo.net/t/Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
<script src="http://www.gimoo.net/t/Scripts/Mobile/jquery.mobile-1.4.0.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方法,是无法正确执行,就不能显示加载器

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

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

以上所述是小编给大家介绍的jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对绿夏网网站的支持!



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103