【问题标题】:Detect swipe from edge in jQuery Mobile在 jQuery Mobile 中检测从边缘滑动
【发布时间】:2013-11-20 23:40:42
【问题描述】:

我想在使用$("#panel").panel("open"); 打开面板之前检测用户是否从边缘滑动。这是代码

$("#main").on("swiperight",function(event){
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event,
    coords = [data.pageX, data.pageY];

console.log(coords);
});

但是coords 没有返回任何内容,因为我收到了错误:

未捕获的类型错误:无法读取未定义的属性 'touches'

  1. 那么有没有办法在滑动发生时获取坐标?

  2. 或者有没有一种简单的方法来检测凝视位置是否来自 而是左边缘?

谢谢。

【问题讨论】:

标签: javascript jquery jquery-mobile mobile touch


【解决方案1】:

在 jqm 1.4+ 中尝试以下,对我有用,相应地调整边缘修剪值,50 对我有好处

$( "div.ui-page" ).on( "swiperight", function( e ) {
    if ( e.swipestart.coords[0] <50) {
    // your logic 
    }
});

这是用于 x 坐标的类似,您可以从 coords[1] 获取 y

【讨论】:

    【解决方案2】:

    这适用于任何屏幕尺寸:

    $("#main").on("swiperight",function( event ){
       // take 15% of screen good for diffrent screen size
       var window_width_15p = $( window ).width() * 0.15;
       // check if the swipe right is from 15% of screen (coords[0] means X)
       if ( event.swipestart.coords[0] < window_width_15p) {
          // open your panel
          $("#panel").panel("open");
       }
    });
    

    【讨论】:

      【解决方案3】:

      你可以这样做。

      $('#main').on('touchstart', function(e) {
         var xPos = e.originalEvent.touches[0].pageX;
         if(xPos == 0) { //Keep in mind the margin of failure from 0, when user touches.
            //Handle edge swipe
         }
      });
      

      【讨论】:

      • touchstart 不会告诉我滑动是否发生以及滑动的时间。因为我还设置了$.event.special.swipe.horizontalDistanceThreshold = 120; 以确保滑动足够长。
      • 澄清一下,我回答了您提出的两个问题。要获得完整的解决方案,您需要结合自己的解决方案来检测 swiperight 事件和我的示例以查看触摸何时开始。如果触摸开始不在正确的边缘位置,您只是不处理边缘滑动。
      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 2016-02-25
      • 2014-03-30
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多