如何改进我的 Jquery 移动应用程序的页面转换? [英] How can I improve the page transitions for my Jquery mobile app?

查看:17
本文介绍了如何改进我的 Jquery 移动应用程序的页面转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单个 HTML 页面中创建了一个小型的 Jquery Mobile 应用程序.我面临的问题是移动设备中页面转换的性能非常糟糕.在我滑动到下一页后,我最终在页面更改之前等待了 3-4 秒.有什么想法可以改进吗?

代码如下:

<头><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>多页模板</title><link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css"/><link rel="stylesheet" href="http://jquerymobile.com/branches/tables/css/themes/default/jquery.mobile.css"><link rel="stylesheet" href="http://jquerymobile.com/branches/tables/docs/_assets/css/jqm-docs.css"><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script><身体><form id="test" method="post"><!-- 页面开始:#p01 --><div data-role="page" id="p01" data-theme="b" data-prefetch><div data-role="header" data-theme="a"><h1>第 01 页 of 05</h1></div><!--/header --><div data-role="内容" ><h3>请提供有关孩子的这些详细信息</h3><br/><字段集><label>孩子的全名:</label><字段集><input id = "p01_childFirstName_text" type="text" placeholder="名字..."><input id = "p01_childMidName_text" type="text" placeholder="中间名..."><input id = "p01_childLastName_text" type="text" placeholder="Last Name..."></fieldset></fieldset><br/><fieldset data-role="controlgroup" data-type="horizo​​ntal"><legend>孩子的性别:</legend><input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderMale_radio" value="choice-1" checked="checked"/><label for="p01_childGenderMale_radio">女性</label><input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderFemale_radio" value="choice-2"/><label for="p01_childGenderFemale_radio">男性</label><input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderOther_radio" value="choice-3"/><label for="p01_childGenderOther_radio">其他</label></fieldset><br/><字段集><label for = "p01_childEthnGroup_text">孩子的族群或种族:</label><input id = "p01_childEthnGroup_text" type="text" placeholder="Ethnic Group..."></fieldset><br/><字段集><label for="p01_childBirthDate_text">孩子的出生日期:</label><input id = "p01_childBirthDate_text" type="text" placeholder="DD/MM/YYYY"></fieldset><br/><字段集><label for="p01_childBirthDate_text">孩子的在校成绩:</label><input id = "p01_childBirthDate_text" type="text" placeholder="DD/MM/YYYY"></fieldset><br/><字段集><label for="flip-1">孩子在上学吗?</label><select name="flip-1" id="flip-1" data-role="slider" data-theme="a"><option value="off">是</option><option value="on">否</option></选择></fieldset></div><!--/content --><div data-role="footer" data-theme="a"><p class = "footer_text"><small><em>请将页面向两侧滑动以转到下一页,或返回到上一页.</em></small></p></div><!--/footer --></div><!--/第一页--><!-- 页面开始:#p02 --><div data-role="page" id="p02" data-theme="b" data-prefetch><div data-role="header" data-theme="a"><h1>第 02 页 of 05</h1></div><!--/header --><div data-role="content" data-theme="b"><h3>请提供有关孩子父母的这些详细信息</h3><p><small><em>即使父母现在没有工作,也请输入这些详细信息.具体 - 例如:汽车修理工、高中老师、家庭主妇、临时工、车床操作员、军士等.</em></small></p><br/><字段集><label for="p02_fatherWorkType_text">父亲的工作类型:</label><input id = "p02_fatherWorkType_text" type="text" placeholder="工作类型..."></fieldset><br/><字段集><label for="p02_motherWorkType_text">母亲的工作类型:</label><input id = "p02_motherWorkType_text" type="text" placeholder="工作类型..."></fieldset><br/><字段集><label>您的全名:</label><字段集><input id = "p02_userFirstName_text" type="text" placeholder="名字..."><input id = "p02_userMidName_text" type="text" placeholder="中间名..."><input id = "p02_userLastName_text" type="text" placeholder="Last Name..."></fieldset></fieldset><br/><fieldset data-role="controlgroup" data-type="horizo​​ntal"><legend>你的性别:</legend><input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderMale_radio" value="choice-1" checked="checked"/><label for="p02_userGenderMale_radio">女性</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderFemale_radio" value="choice-2"/><label for="p02_userGenderFemale_radio">男性</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderOther_radio" value="choice-3"/><label for="p02_userGenderOther_radio">其他</label></fieldset><br/><fieldset data-role="控制组"><legend>你与孩子的关系:</legend><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationBio_radio" value="choice-1" checked="checked"/><label for="p02_userRelationBio_radio">生物父母</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationAdopt_radio" value="choice-2"/><label for="p02_userRelationAdopt_radio">养父母</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationStep_radio" value="choice-3"/><label for="p02_userRelationStep_radio">父级</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationGrand_radio" value="choice-3"/><label for="p02_userRelationGrand_radio">祖父</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationFoster_radio" value="choice-3"/><label for="p02_userRelationFoster_radio">寄养父母</label><input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationOther_radio" value="choice-3"/><label for="p02_userRelationOther_radio">其他(请指定)</label><input class="p02_input_hidden_​​text" id = "p02_userRelationOther_text" type="text" placeholder="指定关系..."></fieldset></div><!--/content --><div data-role="footer" data-theme="a"><p class = "footer_text"><small><em>请将页面向两侧滑动以转到下一页,或返回到上一页.</em></small></p></div><!--/footer --></div><!--/第二页--><!-- 页面开始:#p05 --><div data-role="page" id="p05" data-theme="b" data-prefetch><div data-role="header" data-theme="a"><h1>最后一页</h1></div><!--/header --><div data-role="content" data-theme="b"><h2>恭喜!您已填写儿童行为清单.</h2><p><small><em>请点击下面的提交"按钮将清单发送给我们.</em></small></p><br/><br/><br/><button type="submit" data-theme="a" class="ui-btn-hidden" data-disabled="false">提交</button></div><!--/content --></div><!--/第五页--></表单><风格>.footer_text {文字对齐:居中;}</风格><脚本>$( 文档 ).on( 'mobileinit', function(){$.mobile.loader.prototype.options.text = "加载中...";$.mobile.loader.prototype.options.textVisible = false;$.mobile.loader.prototype.options.theme = "a";$.mobile.loader.prototype.options.html = "";});$(document).on ('pageshow', function (e, data) {//将所有以前访问过的页面保留在 DOM 中.稍后当应用程序变大时删除此选项.$.mobile.page.prototype.options.domCache = true;var activePage = $.mobile.activePage.attr("id");var previousPage = (parseInt(activePage.slice(1,3))-1).toString();var nextPage = (parseInt(activePage.slice(1,3))+1).toString();window.myActivePage = activePage;window.myPreviousPage = "#p0" + previousPage;window.myNextPage = "#p0" + nextPage;});//使用滑动的页面导航$(document).on('swipeleft',function(event, ui){$.mobile.changePage(window.myNextPage, { transition: "slide"});});$(document).on('swiperight',function(event, ui){$.mobile.changePage(window.myPreviousPage, { transition: "slide", reverse:true});});//显示和隐藏字段的代码.意大利面代码.在处理实际应用程序时重构它.$(document).ready(function() {$('[id=p02_userRelationOther_text]').hide()$('[type=radio]').click(function(eventName) {如果(this.id == 'p02_userRelationOther_radio'){$('[id=p02_userRelationOther_text]').show('慢')}别的 {$('[id=p02_userRelationOther_text]').hide('慢')}});$('[id=p03_noSports_check]').click(function(eventName) {if ($('[id=p03_noSports_check]').is(":checked")) {$('[class=p03_hidden_​​container]').hide('慢')}别的 {$('[class=p03_hidden_​​container]').show('慢')}});$('[id=p04_noHobby_check]').click(function(eventName) {if ($('[id=p04_noHobby_check]').is(":checked")) {$('[class=p04_hidden_​​container]').hide('慢')}别的 {$('[class=p04_hidden_​​container]').show('慢')}});});

解决方案

有几种方法:

  • 如果您正在使用 !包含多个页面的 html 文件,将它们包装成单个 div:

并设置这个css:

 body {边距:0;}#容器 {位置:绝对;宽度:100%;高度:100%;}

js 代码:

 $(document).one("mobileinit", function () {$.mobile.pageContainer = $('#container');$.mobile.defaultPageTransition = '幻灯片';});

可以在此处找到有关此方法的更多信息:http://outof.me/fixing-flickers-jumps-of-jquery-mobile-transitions-in-phonegap-apps/

  • 其他常见的解决方案是设置这个 css:.ui页面{-webkit-backface-visibility:隐藏;}

该解决方案的问题在于它破坏了表单上的选择列表.

  • 关闭它们:

    $(document).bind("mobileinit", function(){$.mobile.defaultDialogTransition = "none";$.mobile.defaultPageTransition = "无";});

  • 在 jquery 移动应用上使用 fastclick 来加速点击事件,从而加速页面转换.单击事件最多可以将 300 毫秒添加到页面转换中.这个插件会做更多的事情,但在你的情况下就足够了.

链接:https://github.com/drowne/jquery.mobile.fastclick

  • 如果您不想要额外的插件,您仍然可以通过从页面更改按钮中删除 href 然后执行以下操作来实现更快的页面转换:

    <a class="ui-btn-left" data-icon="arrow-l" href="#" data-theme="a" id="back-btn">Back</a>$('#back-btn').bind('touchstart', function(e) {$.mobile.changePage("#pageID");});

如果您知道用户不会滚动,那么 touchstart(或 touchend)事件会很好用.这实际上是点击事件在移动设备上需要很长时间才能解决的原因,设备正在等待查看用户是滚动还是点击.所以touchstart不应该像普通的点击/点击事件那样有延迟.

我希望其中的一些解决方案对您有所帮助.请考虑到,这些都不是万无一失的解决方案,它们也有自己的缺点.

I have created a tiny Jquery Mobile app in a single HTML page. The problem I am facing is that the performance of page transitions in Mobile devices is horrendous. After I swipe to goto next page, I end up waiting for 3-4 seconds befoe the page changes. Any ideas how I can improve it?

Here's the code:

<!DOCTYPE html> 
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Multi-page template</title> 

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="http://jquerymobile.com/branches/tables/css/themes/default/jquery.mobile.css">  
    <link rel="stylesheet" href="http://jquerymobile.com/branches/tables/docs/_assets/css/jqm-docs.css">
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>

</head> 


<body> 
    <form id="test" method="post">
        <!-- Start of page: #p01 -->
        <div data-role="page" id="p01" data-theme="b" data-prefetch>

            <div data-role="header" data-theme="a">
                <h1>Page 01 of 05</h1>
            </div><!-- /header -->

            <div data-role="content" >
                <h3>Please provide these details about the child</h3>
                <br/>
                <fieldset>
                    <label>Child's Full Name:</label>
                    <fieldset>
                        <input id = "p01_childFirstName_text" type="text" placeholder="First Name...">
                        <input id = "p01_childMidName_text" type="text" placeholder="Middle Name...">
                        <input id = "p01_childLastName_text" type="text" placeholder="Last Name...">
                    </fieldset>
                </fieldset>    
                <br/>
                <fieldset data-role="controlgroup" data-type="horizontal">
                    <legend>Child's Gender:</legend>
                        <input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderMale_radio" value="choice-1" checked="checked" />
                        <label for="p01_childGenderMale_radio">Female</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderFemale_radio" value="choice-2"  />
                        <label for="p01_childGenderFemale_radio">Male</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p01_childGenderOther_radio" value="choice-3"  />
                        <label for="p01_childGenderOther_radio">Other</label>
                </fieldset>
                <br/>
                <fieldset>
                    <label for = "p01_childEthnGroup_text">Child's Ethnic Group or Race:</label>
                    <input id = "p01_childEthnGroup_text" type="text" placeholder="Ethnic Group...">
                </fieldset>
                <br/>
                <fieldset>
                    <label for="p01_childBirthDate_text">Child's Birth Date:</label>
                    <input id = "p01_childBirthDate_text" type="text" placeholder="DD/MM/YYYY">
                </fieldset>  
                <br/>
                <fieldset>
                    <label for="p01_childBirthDate_text">Child's Grade in School:</label>
                    <input id = "p01_childBirthDate_text" type="text" placeholder="DD/MM/YYYY">
                </fieldset>  
                <br/>
                <fieldset>
                    <label for="flip-1">Is the Child attending a School?</label>
                    <select name="flip-1" id="flip-1" data-role="slider" data-theme="a">
                        <option value="off">Yes</option>
                        <option value="on">No</option>
                    </select>   
                </fieldset>  

            </div><!-- /content -->

            <div data-role="footer" data-theme="a">
                    <p class = "footer_text"><small><em>Please swipe the page to the sides to go to the next page, or to return to the previous one.</em></small></p>
            </div><!-- /footer -->

        </div><!-- /page one -->

        <!-- Start of page: #p02 -->
        <div data-role="page" id="p02" data-theme="b" data-prefetch>

            <div data-role="header" data-theme="a">
                <h1>Page 02 of 05</h1>
            </div><!-- /header -->

            <div data-role="content" data-theme="b">    
                <h3>Please provide these details about the child's parent</h3>
                <p><small><em>Please enter these details even if the parents are not working now. 
                              Be specific - for example: auto mechanic, high school teacher, home maker, 
                              day labourer, lathe operator, army sergeant etc.
                </em></small></p>
                <br/>
                <fieldset>
                    <label for="p02_fatherWorkType_text">Father's Type of Work:</label>
                    <input id = "p02_fatherWorkType_text" type="text" placeholder="Type of Work...">
                </fieldset>  
                <br/>
                <fieldset>
                    <label for="p02_motherWorkType_text">Mother's Type of Work:</label>
                    <input id = "p02_motherWorkType_text" type="text" placeholder="Type of Work...">
                </fieldset>  
                <br/>
                <fieldset>
                    <label>Your Full Name:</label>
                    <fieldset>
                        <input id = "p02_userFirstName_text" type="text" placeholder="First Name...">
                        <input id = "p02_userMidName_text" type="text" placeholder="Middle Name...">
                        <input id = "p02_userLastName_text" type="text" placeholder="Last Name...">
                    </fieldset>
                </fieldset>    
                <br/>
                 <fieldset data-role="controlgroup" data-type="horizontal">
                    <legend>Your Gender:</legend>
                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderMale_radio" value="choice-1" checked="checked" />
                        <label for="p02_userGenderMale_radio">Female</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderFemale_radio" value="choice-2"  />
                        <label for="p02_userGenderFemale_radio">Male</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userGenderOther_radio" value="choice-3"  />
                        <label for="p02_userGenderOther_radio">Other</label>
                </fieldset>
                <br/>
                 <fieldset data-role="controlgroup">
                    <legend>Your Relation with the Child:</legend>
                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationBio_radio" value="choice-1" checked="checked" />
                        <label for="p02_userRelationBio_radio">Biological Parent</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationAdopt_radio" value="choice-2"  />
                        <label for="p02_userRelationAdopt_radio">Adoptive Parent</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationStep_radio" value="choice-3"  />
                        <label for="p02_userRelationStep_radio">Step Parent</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationGrand_radio" value="choice-3"  />
                        <label for="p02_userRelationGrand_radio">Grand Parent</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationFoster_radio" value="choice-3"  />
                        <label for="p02_userRelationFoster_radio">Foster Parent</label>

                        <input data-theme="a" type="radio" name="radio-choice" id="p02_userRelationOther_radio" value="choice-3"  />
                        <label for="p02_userRelationOther_radio">Others (Please Specify)</label>
                        <input class= "p02_input_hidden_text" id = "p02_userRelationOther_text" type="text" placeholder="Specify Relation...">

                </fieldset>

            </div><!-- /content -->

            <div data-role="footer" data-theme="a">
                    <p class = "footer_text"><small><em>Please swipe the page to the sides to go to the next page, or to return to the previous one.</em></small></p>
            </div><!-- /footer -->

        </div><!-- /page two -->



         <!-- Start of page: #p05 -->
        <div data-role="page" id="p05" data-theme="b" data-prefetch>

            <div data-role="header" data-theme="a">
                <h1>Last Page</h1>
            </div><!-- /header -->

            <div data-role="content" data-theme="b">    
                <h2>Congratulations! You have filled out the Child Behaviour Checklist.</h2>
                <p><small><em>Please click on the "SUBMIT" button below to send us the checklist.</em></small></p>
                <br/>
                <br/>
                <br/>
                <button type="submit" data-theme="a" class="ui-btn-hidden" data-disabled="false">Submit</button>

            </div><!-- /content -->


        </div><!-- /page five -->

    </form>
    <style>
        .footer_text {
            text-align:center;
        }
    </style>

    <script>
    $( document ).on( 'mobileinit', function(){
        $.mobile.loader.prototype.options.text = "loading...";
        $.mobile.loader.prototype.options.textVisible = false;
        $.mobile.loader.prototype.options.theme = "a";
        $.mobile.loader.prototype.options.html = "";
    });
    $(document).on ('pageshow', function (e, data) {         
        // keep all previously-visited pages in the DOM. Remove this option later when application becomes large.
        $.mobile.page.prototype.options.domCache = true;

        var activePage = $.mobile.activePage.attr("id");
        var previousPage = (parseInt(activePage.slice(1,3))-1).toString();
        var nextPage = (parseInt(activePage.slice(1,3))+1).toString();

        window.myActivePage = activePage;
        window.myPreviousPage = "#p0" + previousPage;
        window.myNextPage = "#p0" + nextPage;

    });
    // page navigation on using swipes
    $(document).on('swipeleft',function(event, ui){
            $.mobile.changePage(window.myNextPage, { transition: "slide"});
        });
    $(document).on('swiperight',function(event, ui){
            $.mobile.changePage(window.myPreviousPage, { transition: "slide", reverse:true});
        });

    // Code to show and hide fields. Spaghetti code. Refactor this when working on actual app.
    $(document).ready(function() {
        $('[id=p02_userRelationOther_text]').hide()
        $('[type=radio]').click(function(eventName) {
            if (this.id == 'p02_userRelationOther_radio') {
                $('[id=p02_userRelationOther_text]').show('slow')
            }
            else {
                $('[id=p02_userRelationOther_text]').hide('slow')
            }
        });

        $('[id=p03_noSports_check]').click(function(eventName) {
            if ($('[id=p03_noSports_check]').is(":checked")) {
                $('[class=p03_hidden_container]').hide('slow')
            }
            else {
            $('[class=p03_hidden_container]').show('slow')
            }
        });

        $('[id=p04_noHobby_check]').click(function(eventName) {
            if ($('[id=p04_noHobby_check]').is(":checked")) {
                $('[class=p04_hidden_container]').hide('slow')
            }
            else {
            $('[class=p04_hidden_container]').show('slow')
            }
        });
    });
    </script>
</body>

</html>

解决方案

There are few ways:

  • In case you are using ! html file with multiple pages, wrap them into single div:

    <div id="container"/>
    

and set this css:

    body {
        margin: 0;
    }

    #container {
        position: absolute;
        width: 100%;
        height: 100%;
    }

js code:

    $(document).one("mobileinit", function () {
        $.mobile.pageContainer = $('#container');
        $.mobile.defaultPageTransition = 'slide';
    });

More about this aproach can be found here: http://outof.me/fixing-flickers-jumps-of-jquery-mobile-transitions-in-phonegap-apps/

  • Other common solution is to set this css: .ui-page { -webkit-backface-visibility: hidden; }

The problem with that solution is that it breaks Select list on your forms.

  • Turn them off:

    $(document).bind("mobileinit", function(){
        $.mobile.defaultDialogTransition = "none";
        $.mobile.defaultPageTransition = "none";
    });
    

  • Use fastclick on jquery mobile apps to speed click events thus speeding page transitions. Click events can add up to 300ms into page transition. This plugin will do much more then this but in your case it will be enough.

Link: https://github.com/drowne/jquery.mobile.fastclick

  • In case you don't want additional plugins you can still achieve faster page transition by removing href from page changing buttons and then doing this:

    <a class="ui-btn-left" data-icon="arrow-l" href="#" data-theme="a" id="back-btn">Back</a>
    
    $('#back-btn').bind('touchstart', function(e) {
        $.mobile.changePage("#pageID");
    });
    

The touchstart (or touchend) event works great if you know the user won't be scrolling. That's actually the reason click events take so long to resolve on mobile devices, the device is waiting to see if the user is scrolling or clicking. So touchstart should not have a delay like common click/tap event.

I hope some of this solutions will help you. Take into consideration, these are not bulletproof solution and they have downsides of they own.

这篇关于如何改进我的 Jquery 移动应用程序的页面转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆