jquery移动滑动功能仅在页面刷新后有效

时间:2013-07-31 09:47:19

标签: javascript jquery html

我在ipad上使用jquery mobile进行侧擦手势。

以下代码位于我的html文件中引用的文件中。

我的html文件有:

<div data-role="page" id="device1">
<!--content for this part of html page -->
</div>
<!--more divs with incrementing id -->
<div data-role="page" id="device4">
<!--content for this part of html page -->
</div>

此格式用于多个html文件。

我使用此代码(在stackoverflow上找到) - 并不想在旧线程上发布。

    $(document).ready(function() {

    $('.ui-slider-handle').on('touchstart', function(){
        // When user touches the slider handle, temporarily unbind the page turn handlers
        doUnbind();
    });

    $('.ui-slider-handle').on('mousedown', function(){
        // When user touches the slider handle, temporarily unbind the page turn handlers
        doUnbind();
    });

    $('.ui-slider-handle').on('touchend', function(){
        //When the user let's go of the handle, rebind the controls for page turn
        // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered
        setTimeout( function() {doBind();}, 100 );
    });

    $('.ui-slider-handle').on('mouseup', function(){
        //When the user let's go of the handle, rebind the controls for page turn
        // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered
        setTimeout( function() {doBind();}, 100 );
    });

    // Set the initial window (assuming it will always be #1
    window.now = 1;

    //get an Array of all of the pages and count
    windowMax = $('div[data-role="page"]').length; 

   doBind();
});


// Functions for binding swipe events to named handlers
function doBind() {
    $('div[data-role="page"]').on("swipeleft", turnPage); 
    $('div[data-role="page"]').on("swiperight", turnPageBack);
}

function doUnbind() {
    $('div[data-role="page"]').die("swipeleft", turnPage);
    $('div[data-role="page"]').die("swiperight", turnPageBack);
}

// Named handlers for binding page turn controls
function turnPage(){
    // Check to see if we are already at the highest numbers page            
    if (window.now < windowMax) {
        window.now++
        $.mobile.changePage("#device"+window.now, "slide", false, true);
    }
}

function turnPageBack(){
    // Check to see if we are already at the lowest numbered page
    if (window.now != 1) {
        window.now--;
        $.mobile.changePage("#device"+window.now, "slide", true, true);
    }
}

// Named handlers for binding page turn controls
function navigate_without_swipe(page){
    // Check to see if we are already at the highest numbers page            
    $.mobile.changePage("#device"+page, "slide");
}

请告诉我为什么我需要重新加载此javascript工作的页面

1 个答案:

答案 0 :(得分:0)

因为您使用的是$(document).ready

这是一个JQuery事件。

Jquery Mobile有自己的加载事件,因为JQM使用AJAX加载页面,这意味着不会触发事件。

我想您可能希望在pageinit中执行此操作,但请查看文档以了解您的情况是否存在更合适的事件。

JQuery Mobile documentation