网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
内容
    想要添加这个效果,先来弄明白页面的加载和事件执行顺序,看这个简单例子:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head >
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>验证加载顺序</title>
    <script src="../Scripts/jquery-1.7.1.js"></script>
    <link href="../Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
    <script src="../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="/uploads/202504/02/feather_default5939.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="../Scripts/jquery-1.7.1.js"></script>
    <link href="../Scripts/Mobile/jquery.mobile-1.4.0.min.css" rel="stylesheet" />
    <script src="../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="/uploads/202504/02/feather_default5939.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执行顺序比较 ,希望对大家有所帮助
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/21 18:12:36