屏幕转换:如何始终从左向右移动

时间:2012-04-30 11:09:03

标签: javascript jquery html jquery-mobile transition

我正在开发手机应用程序,屏幕顶部有两个按钮:“后退”和“下一步”。每当我点击下一个按钮时,屏幕从右向左移动,大部分时间我点击后退按钮,它从左向右移动;然而,经常点击“后退”从右到左过渡。

这是我用于后退按钮的代码:

html的

<a href="#" data-rel="back"  data-role="button" data-inline="true" data-transition="myTransition" data-direction="reverse">Back</a>

的.js

(function( $, undefined ) {

$.mobile.page.prototype.options.backBtnText  = "Back";
$.mobile.page.prototype.options.addBackBtn   = false;
$.mobile.page.prototype.options.backBtnTheme = null;
$.mobile.page.prototype.options.headerTheme  = "a";
$.mobile.page.prototype.options.footerTheme  = "a";
$.mobile.page.prototype.options.contentTheme = null;

$( document ).delegate( ":jqmData(role='page'), :jqmData(role='dialog')", "pagecreate", function( e ) {

var $page = $( this ),
    o = $page.data( "page" ).options,
    pageRole = $page.jqmData( "role" ),
    pageTheme = o.theme;

$( ":jqmData(role='header'), :jqmData(role='footer'), :jqmData(role='content')", this ).each(function() {
    var $this = $( this ),
        role = $this.jqmData( "role" ),
        theme = $this.jqmData( "theme" ),
        contentTheme = theme || o.contentTheme || ( pageRole === "dialog" && pageTheme ),
        $headeranchors,
        leftbtn,
        rightbtn,
        backBtn;

    $this.addClass( "ui-" + role ); 

    //apply theming and markup modifications to page,header,content,footer
    if ( role === "header" || role === "footer" ) {

        var thisTheme = theme || ( role === "header" ? o.headerTheme : o.footerTheme ) || pageTheme;

        $this
            //add theme class
            .addClass( "ui-bar-" + thisTheme )
            // Add ARIA role
            .attr( "role", role === "header" ? "banner" : "contentinfo" );

        // Right,left buttons
        $headeranchors  = $this.children( "a" );
        leftbtn = $headeranchors.hasClass( "ui-btn-left" );
        rightbtn = $headeranchors.hasClass( "ui-btn-right" );

        leftbtn = leftbtn || $headeranchors.eq( 0 ).not( ".ui-btn-right" ).addClass( "ui-btn-left" ).length;

        rightbtn = rightbtn || $headeranchors.eq( 1 ).addClass( "ui-btn-right" ).length;

        // Auto-add back btn on pages beyond first view
        if ( o.addBackBtn && 
            role === "header" &&
            $( ".ui-page" ).length > 1 &&
            $this.jqmData( "url" ) !== $.mobile.path.stripHash( location.hash ) &&
            !leftbtn ) {

            backBtn = $( "<a href='#' class='ui-btn-left' data-"+ $.mobile.ns +"rel='back' data-"+ $.mobile.ns +"icon='arrow-l'>"+ o.backBtnText +"</a>" )
                // If theme is provided, override default inheritance
                .attr( "data-"+ $.mobile.ns +"theme", o.backBtnTheme || thisTheme )
                .prependTo( $this );                
        }

        // Page title
        $this.children( "h1, h2, h3, h4, h5, h6" )
            .addClass( "ui-title" )
            // Regardless of h element number in src, it becomes h1 for the enhanced page
            .attr({
                "tabindex": "0",
                "role": "heading",
                "aria-level": "1"
            });

    } else if ( role === "content" ) {
        if ( contentTheme ) {
            $this.addClass( "ui-body-" + ( contentTheme ) );
        }

        // Add ARIA role
        $this.attr( "role", "main" );
    }
});
});

})( jQuery );

编辑:

这是我创建的过渡

function myTransitionHandler( name, reverse, $to, $from ) {

var deferred = new $.Deferred(),
    reverseClass = reverse ? " reverse" : "",
    viewportClass = "ui-mobile-viewport-transitioning viewport-" + name,
    doneFunc = function() {

        $to.add( $from ).removeClass( "out in reverse " + name );

        if ( $from && $from[ 0 ] !== $to[ 0 ] ) {
            $from.removeClass( $.mobile.activePageClass );
        }

        $to.parent().removeClass( viewportClass );

        deferred.resolve( name, reverse, $to, $from );
    };

$to.animationComplete( doneFunc );

$to.parent().addClass( viewportClass );

if ( $from ) {
    $from.addClass( name + " out" + reverseClass );
}
$to.addClass( $.mobile.activePageClass + " " + name + " in" + reverseClass );

return deferred.promise();
}

// Make our transition handler public.
$.mobile.transitionHandlers["myTransition"] = myTransitionHandler;

是否有任何代码可以添加,以便在单击后退按钮时屏幕始终从左向右移动?

1 个答案:

答案 0 :(得分:0)

jQuery mobile似乎只允许您指定转换(seen here),然后在指定data-direction="reverse"属性时对其进行编程。

我也遇到过这种行为,即后退按钮(甚至是浏览器的后退按钮)有时会触发“向前动作”过渡。我不是怎么会改变这个/迫使一个按钮总是朝着同一个方向过渡。

但是,setting up your own transitions可能会有一些承诺。如果你这样做,(我没有),那么你可能能够应用一个只转向一个方向的过渡,无论jQuery mobile认为它是向前还是反向,而且不管data-direction属性。

.my.slide.out {
    -webkit-transform: translateX(-100%);
    -moz-transform: translateX(-100%);
    -webkit-animation-name: slideouttoleft;
    -moz-animation-name: slideouttoleft;
}
.my.slide.in {
    -webkit-transform: translateX(-100%);
    -moz-transform: translateX(-100%);
    -webkit-animation-name: slideouttoleft;
    -moz-animation-name: slideouttoleft;
}

然后将这些过渡应用到后退按钮。