如何在多个 jquery 移动页面之间滑动? [英] How to swipe between several jquery mobile pages?

查看:13
本文介绍了如何在多个 jquery 移动页面之间滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是适用于 2 页的代码摘录:

Here is the code extract which works well with 2 pages:

<script>
  $(document).ready(function() {
    window.now = 1;

    $('#device1').live("swipeleft", function(){
        window.now++
        $.mobile.changePage("#device"+window.now, "slide", false, true);
    });
    $('#device2').live("swiperight", function(){
        window.now--;
        $.mobile.changePage("#device"+window.now, "slide", true, true);
    });    

  });
</script> 

...
<div data-role="page" id="device1">
...
</div><!-- /page -->
<div data-role="page" id="device2">
...
</div><!-- /page -->

如何让大量页面的工作变得更通用?

How can I make it more universal to work with huge number of pages?

推荐答案

这似乎是你想要的

<script>
$(document).ready(function() {

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

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

    $('.ui-slider-handle').live('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').live('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"]').live("swipeleft", turnPage); 
        $('div[data-role="page"]').live("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);
        }
    }
</script>

更新:我用 iPhone 模拟器和 Android 模拟器测试了这个,它在两者中都按预期工作.

更新:已更改答案以解决用户关于使用滑块导致向左/向右滑动的评论.

UPDATE: I tested this with the iPhone emulator and the Android emulator and it worked as expected in both.

UPDATE: Changed answer to address the user's comment about using a slider causing swipe left/right.

这篇关于如何在多个 jquery 移动页面之间滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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