【问题标题】:How to swipe between several jquery mobile pages?如何在几个jquery移动页面之间滑动?
【发布时间】:2011-09-23 19:19:34
【问题描述】:

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

<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 -->

如何使处理大量页面变得更加通用?

【问题讨论】:

    标签: jquery-mobile


    【解决方案1】:

    此代码也适用于滑动。

    <script>
    
    $('div.ui-page').live("swipeleft", function () {
        var nextpage = $(this).next('div[data-role="page"]');
        if (nextpage.length > 0) {
            $.mobile.changePage(nextpage, "slide", false, true);
        }
    });
    $('div.ui-page').live("swiperight", function () {
        var prevpage = $(this).prev('div[data-role="page"]');
        if (prevpage.length > 0) {
            $.mobile.changePage(prevpage, {
                transition: "slide",
                reverse: true
            }, true, true);
        }
    });
    
    </script>
    

    【讨论】:

      【解决方案2】:

      这似乎做你想做的事

      <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 模拟器对此进行了测试,两者都按预期工作。

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

      【讨论】:

      • 谢谢。这就是我一直在寻找的,而且效果很好。但是,我遇到了问题 - 当我将滑块(位于同一页面上)向左移动时,也会发生 swipeleft。如何避免?
      • 哇,这是一个 PITA 来弄清楚。我认为这会奏效。 cmets中的解释
      • 谢谢,杰森。有用。但看起来这不是很好的方法(它在 PC 上不起作用;如果您没有触摸滑块并错误地触摸背景,它会更改页面)。似乎我需要为这些手势创建特殊区域。试图弄清楚如何创建它以占用剩余空间。
      • 哦,我没有意识到它也需要在 PC 上工作。我刚刚再次更新了我的答案来处理这个问题。对我来说,它在 Chrome 中效果很好。
      • 有人可以设置一个jsfiddle吗?我无法让它工作>__
      【解决方案3】:

      我正在使用swipeview 来处理大量页面。来自 iScroll 的创建者。

      【讨论】:

      • 请展示您如何使用 jQuery Mobile 页面转换的 swipeview 代码。 swipeview 上的文档不存在。
      • 对不起 - 我最终放弃了 swipeview 以适应我的特殊情况。我需要重新审视这个问题......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多