0

我在 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 工作

4

1 回答 1

0

因为您正在使用 $(document).ready

那是一个 JQuery 事件。

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

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

jQuery 移动文档

于 2013-07-31T10:07:11.500 回答