【问题标题】:changePage "jumps" back to old pagechangePage “跳”回旧页面
【发布时间】:2012-12-22 21:33:35
【问题描述】:

我有一个 jQuery Mobile 应用程序的大问题: 我正在使用自定义函数(它们由 onClick 触发)用 currentPage 切换页面。

这种情况只会发生在 Android 设备上的网站上(由于 ajax 请求)已使用集成浏览器进行更改。 iOS 和 Chrome 运行良好。

点击一个元素后,动画开始,但就在它结束之前,它切换回旧页面。半秒后,它切换回新的。

我在这里制作了这个错误的电影:http://www.youtube.com/watch?v=sXxvVUxniNg

非常感谢

代码(CoffeeScript):

class Guide

    @categoriesLoaded = false

    @loadSearch: ->

        $.mobile.changePage $("#guide"),
            transition: 'slide'
            changeHash: false

        if !@categoriesLoaded

            @categoriesLoaded = true

            GuideApi.getCategories (data) ->
                output = Mustache.render $("#tmpl-guide-categories-select").html(), 
                    categories: data

                $("#guide-search-category").append output

                $("#guide-search-category").val($("#guide-search-category option:first").val());

window.WgSwitchGuide = ->
        Guide.loadSearch

【问题讨论】:

  • jQuery Mobile 页面转换在桌面浏览器中相当不错,但在设备浏览器中却是灾难性的。我更喜欢$.mobile.defaultPageTransition = 'none'; :-(
  • @Mithun: 这解决了我的问题

标签: jquery-mobile webkit css-transitions android-browser


【解决方案1】:

我遇到了同样的问题。我尝试了一切,最终以解决方案结束。我发现错误主要在浏览器中。所以我将pushStateEnabled的配置设置为false。我通过执行以下操作来添加此脚本。

<script type="text/javascript">
$(document).bind("mobileinit", function(){
$.mobile.pushStateEnabled = false;
});
</script>

它应该在 jquery-mobile 脚本被调用之前添加,更多信息你可以在JQuery description看到它

它解决了问题,不再跳回。

【讨论】:

  • 我真的希望我有时间了解为什么这样做,但它对我有用。
【解决方案2】:

我在 android 和 ios 上都遇到了完全相同的问题。对我来说,它发生在 heavy 页面上,即具有复杂元素的页面等。看起来你正在使用“幻灯片”过渡,这也是我正在使用的。取出这些页面的页面转换(即 $.mobile.changePage("page.html", { transition: "none" }))为我解决了这个问题。希望这会有所帮助。

如果要保留过渡,可以尝试在显示上一页时先使用 $.mobile.loadPage 预加载页面,然后再显示过渡。我自己也在探索这条路线,但可能值得一试。

编辑:好的 - 我探索了最后一个建议,但这似乎不起作用。会坚持第一个选项。

【讨论】:

  • 嗨萨米克!不幸的是,这对我来说不是一个真正的解决方案,因为我的客户想要过渡:-/但谢谢你的回答。
  • 用另一个选项编辑了答案。谢谢。
【解决方案3】:

你会尝试在第一页的点击事件中添加事件 stopPropagation 和 preventDefault 方法吗?这样就不会触发点击事件的默认动作。此外,stopPropagation 可以防止事件在 DOM 树中冒泡,从而防止任何父处理程序收到事件通知。

  • event.stopPropagation();
  • event.preventDefault();

例子:

$("p").click(function(event){
    event.stopPropagation();
    event.preventDefault();
    // change page
});

【讨论】:

  • 对 href 的默认操作是 javascript:; - 无论如何 - 没有解决问题。但我们正在解决它 - 这很可能是一个 CSS 问题......
【解决方案4】:

在尝试了数周来解决此问题后,我最终修改了 JQM 库,以便一个接一个地禁用页面转换。这不是一个好的解决方案,但这是我唯一可以开始工作的事情。

我让页面在 $.mobile.changePage 和锚链接上跳回。我使用了幻灯片过渡,但删除它并不能解决问题。将 pushStateEnabled 设置为 false 也不起作用。所有设备和浏览器(无论如何我都测试过)都发生了跳跃。

这就是我对 JQM 库 (v1.3.2) 所做的。

在定义$.mobile.changePage函数之前,我添加了:

var justChangedPage = false;

然后在函数内有一行代码:

if ( pbcEvent.isDefaultPrevented()) {
    return;
}

我改成了:

if ( pbcEvent.isDefaultPrevented() || justChangedPage) {
    return;
}

然后在 $.mobile.changePage 函数的这部分之后:

if ( toPage[ 0 ] === $.mobile.firstPage[ 0 ] && !settings.dataUrl ) {
    settings.dataUrl = documentUrl.hrefNoHash;
}

我补充说:

justChangedPage = true;
setTimeout(function() {
    justChangedPage = false;
}, 500);

(把它放在函数的前面是行不通的——所有这些东西在一个页面转换中执行了不止一次。半秒似乎是阻止页面跳转的最小超时。)

我希望这对某人有帮助,即使它是一个黑客......

【讨论】:

    【解决方案5】:

    您的 JQM 和 Android 版本是多少?

    我不确定我是否理解正确。我认为过渡闪烁可能来自以下假设。

    1. 重页 DOM 转换。
    2. 在 css 文件中的某处使用“translate3d”。
    3. 未使用“硬件加速”功能。通过将此行添加到 &lt;application&gt; 中的 AndroidManifest.xml 来启用

    android:hardwareAccelerated="true"

    【讨论】:

    • jQM 1.2,用 1.3 试过(那里有类似的错误)。 Android 4.x 默认浏览器和 PhoneGap - 两者都有。它可能是由转换之前的 DOM 操作引起的。 PhoneGap 端口中启用了硬件加速,禁用了 translate3d。感谢您的帮助!
    【解决方案6】:

    我遇到了完全相同的行为,似乎很少有人遇到同样的问题。起初我以为是 jQuery 移动库引起的。后来,我设法找到问题出在哪里,这是我自己的代码中的一个错误。 我做了一个演示来解释这个问题。

    http://jsfiddle.net/pengyanb/6zvpgd4p/10/

    希望这可以为遇到相同问题的人提供提示。

    $(document).on('pagebeforeshow', '#page2', function(){
        console.log('Page2 before show');
        var htmlGeneratedOnTheFly = '<ul data-role="listview" data-inset="true">';
        for(var i=0; i<4; i++)
        {
            htmlGeneratedOnTheFly += '<li><a>Random html element</a></li><li data-role="list-divider"></li>';
        }
        htmlGeneratedOnTheFly += '</div>';
        $('#page2UiContent').empty();
        $('#page2UiContent').append(htmlGeneratedOnTheFly);
        $('#page2UiContent').trigger('create');
        
       
        //////////////////////////////////////////////////
        //The following section is where the bug is generated. 
        //Each on "page2 before show event" will add a OK Button click handler. 
        //The handlers never get cleared.
        //More and more handler is added to the Page2 OK button as pages going back and forth.
        //Open the browser's console window to see multiple "Page 2 OK Button clicked!!!" lines on one button click. 
        //To fix the bug, move the following section out of the $(document).on('pagebeforeshow', '#page2', function(){});
        //////////////////////////////////////////////////
        $('#page2OkButton').click(function(){
            console.log("Page 2 OK Button clicked!!!");
            $.mobile.changePage('#page1', {transition:"flip"});
        });
        //////////////////////////////////////////////
        //////////////////////////////////////////////
    });
    <link href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" rel="stylesheet"/>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div data-role="page" id="page1" data-theme="a">
        <div data-role="header" data-position="fixed">
            <h5>Demo Page 1</h5>
        </div>
        <div data-role="main" class="ui-content">
            <h2>jQuery mobile changepage jumps back to old page demo</h2>
            <p>Click "Go To Page 2" button to go to page2</p>
            <p>On Page2 click Ok Button to come back to page1</p>
            <p>Keeping going back forth between two pages for few times.</p>
            <p>Eventually, you will find that clicked on "Go To Page2" button to flip to Page2 but it soon jumps back to page1 automatically. </p>
            <h2>Please read the comments in the javascript for explaination</h2>
            <a href="#page2" data-transition="flip" class="ui-btn ui-icon-carat-r ui-btn-icon-right">Go To Page 2</a>
        </div>
    </div>
    
    <div data-role="page" id="page2" data-theme="a">
        <div data-role="header" data-position="fixed">
            <h5>Demo Page 2</h5>
        </div>
        <div id="page2UiContent" data-role="main" class="ui-content">
        </div>
        <div data-role="footer" data-position="fixed" style="text-align:center;">
            <div data-role="navbar">
                <ul>
                    <li><a id="page2OkButton" class="ui-btn ui-icon-check ui-btn-icon-left">OK</a></li>
                </ul>
            </div>
        </div>
    </div>

    【讨论】:

    • 这是一个 jQuery Mobile 错误,在我的应用程序中,我已经在 pagecreate 事件上设置了所有更改页面事件,每个页面仅触发一次,并且我仍然有相同的页面行为返回到旧页面。
    猜你喜欢
    • 2015-03-04
    • 2012-05-22
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多