【问题标题】:Add swipe left/right to web page, but use default swipe up/down添加向左/向右滑动到网页,但使用默认向上/向下滑动
【发布时间】:2011-07-15 07:55:42
【问题描述】:

我正在使用 padilicious 来检测将在 iOS 和桌面上查看的网页的滑动手势。在我的网站的上一页和下一页向左/向右滑动非常有用。但是,向上/向下滑动时,它似乎覆盖了 iPhone/iPad 中的默认行为。我想要一个向上/向下滑动来滚动页面,当我没有 padilicious 运行时它会这样做。只是让代码忽略向上/向下滑动似乎不起作用。

我一直在做的一段精彩的代码

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        document.location = document.getElementById('nextPage').href;
    } else if ( swipeDirection == 'right' ) {
        document.location = document.getElementById('prevPage').href;
    } else if ( swipeDirection == 'up' ) {
        return;
    } else if ( swipeDirection == 'down' ) {
        return;
    }


}

【问题讨论】:

    标签: javascript html ios swipe


    【解决方案1】:

    从所有函数中删除event.preventDefault();。在函数processingRoutine() {} 中添加event.preventDefault(); 以获得所需的内容。

    function processingRoutine() {
        var swipedElement = document.getElementById(triggerElementID);
        if ( swipeDirection == 'left' ) {
            // REPLACE WITH YOUR ROUTINES
            //swipedElement.style.backgroundColor = 'orange';
            event.preventDefault();
        } else if ( swipeDirection == 'right' ) {
            // REPLACE WITH YOUR ROUTINES
            //swipedElement.style.backgroundColor = 'green';
            event.preventDefault();
        } else if ( swipeDirection == 'up' ) {
            // REPLACE WITH YOUR ROUTINES
            //swipedElement.style.backgroundColor = 'maroon';
        } else if ( swipeDirection == 'down' ) {
            // REPLACE WITH YOUR ROUTINES
            //swipedElement.style.backgroundColor = 'purple';
        }
    }
    

    【讨论】:

    • 这适用于安卓股票浏览器和火狐,但不适用于谷歌浏览器。另请注意:必须通过事件。
    【解决方案2】:

    有一个 jquery 库,可以完成这项工作(不提供向上/向下方法):http://plugins.jquery.com/project/Touchwipe-iPhone-iPad-wipe-gesture

    【讨论】:

    • 谢谢,这看起来是一个不错的 jquery 滑动选项。但是,它也有同样的问题。查看netcu.de/jquery-touchwipe-iphone-ipad-library。在图像 div 中向左/向右滑动会更改图片,但向上/向下滑动不会执行任何操作,包括滚动。当有人在我网站的内容 div 中向上/向下滑动时,我希望能够滚动,并在向左/向右滑动时转到下一个/后退。在内容 div 之外滑动可以正常工作(默认行为)。
    【解决方案3】:

    我不熟悉 padilicious,但请检查 ontouchmove="BlockMove(event);" 是否设置在任何地方,这会阻止页面像您描述的那样滚动,我不确定您如何让它保持垂直滚动但水平滑动。

    编辑:从那以后,我发现了一个非常有用的概述,用于制作“原生”感觉的 iOS 网络应用程序,它可能不是您想要的,但可以为您提供另一种途径解决您的问题的方法。看看吧:http://matt.might.net/articles/how-to-native-iphone-ipad-apps-in-javascript/

    【讨论】:

    • 代码不包括BlockMove(event),但我很高兴知道这一点,谢谢。
    • 我已经找到了一个您可能会觉得有帮助的链接,在我上面的回复中进行了编辑。
    • 谢谢!代码很有意思:var IsiPhone = navigator.userAgent.indexOf("iPhone") != -1 ; var IsiPod = navigator.userAgent.indexOf("iPod") != -1 ; var IsiPad = navigator.userAgent.indexOf("iPad") != -1 ; var IsiPhoneOS = IsiPhone || IsiPad || IsiPod ;
    【解决方案4】:

    Padilicious 似乎在所有情况下都防止默认。在所有情况下查看对 event.preventDefault() 的调用。

    function touchStart(event,passedName) {
      // disable the standard ability to select the touched object
      event.preventDefault();
    

    您必须更改 start、stop、... 处理程序,以便在 up 和 down 情况下不调用 preventDefault()。

    【讨论】:

      【解决方案5】:

      我改变了脚本,这个工作:

          // TOUCH-EVENTS SINGLE-FINGER SWIPE-SENSING JAVASCRIPT
      // Courtesy of PADILICIOUS.COM and MACOSXAUTOMATION.COM
      
      // this script can be used with one or more page elements to perform actions based on them being swiped with a single finger
      
      var triggerElementID = null; // this variable is used to identity the triggering element
      var fingerCount = 0;
      var startX = 0;
      var startY = 0;
      var curX = 0;
      var curY = 0;
      var deltaX = 0;
      var deltaY = 0;
      var horzDiff = 0;
      var vertDiff = 0;
      var minLength = 72; // the shortest distance the user may swipe
      var swipeLength = 0;
      var swipeAngle = null;
      var swipeDirection = null;
      
      // The 4 Touch Event Handlers
      
      // NOTE: the touchStart handler should also receive the ID of the triggering element
      // make sure its ID is passed in the event call placed in the element declaration, like:
      // <div id="picture-frame" ontouchstart="touchStart(event,'picture-frame');"  ontouchend="touchEnd(event);" ontouchmove="touchMove(event);" ontouchcancel="touchCancel(event);">
      
      function touchStart(event,passedName) {
          // disable the standard ability to select the touched object
          //event.preventDefault();
          // get the total number of fingers touching the screen
          fingerCount = event.touches.length;
          // since we're looking for a swipe (single finger) and not a gesture (multiple fingers),
          // check that only one finger was used
          if ( fingerCount == 1 ) {
              // get the coordinates of the touch
              startX = event.touches[0].pageX;
              startY = event.touches[0].pageY;
              // store the triggering element ID
              triggerElementID = passedName;
          } else {
              // more than one finger touched so cancel
              touchCancel(event);
          }
      }
      
      function touchMove(event) {
          //event.preventDefault();
          if ( event.touches.length == 1 ) {
              curX = event.touches[0].pageX;
              curY = event.touches[0].pageY;
          } else {
              touchCancel(event);
          }
      }
      
      function touchEnd(event) {
          //event.preventDefault();
          // check to see if more than one finger was used and that there is an ending coordinate
          if ( fingerCount == 1 && curX != 0 ) {
              // use the Distance Formula to determine the length of the swipe
              swipeLength = Math.round(Math.sqrt(Math.pow(curX - startX,2) + Math.pow(curY - startY,2)));
              // if the user swiped more than the minimum length, perform the appropriate action
              if ( swipeLength >= minLength ) {
                  caluculateAngle();
                  determineSwipeDirection();
                  processingRoutine();
                  touchCancel(event); // reset the variables
              } else {
                  touchCancel(event);
              }   
          } else {
              touchCancel(event);
          }
      }
      
      function touchCancel(event) {
          // reset the variables back to default values
          fingerCount = 0;
          startX = 0;
          startY = 0;
          curX = 0;
          curY = 0;
          deltaX = 0;
          deltaY = 0;
          horzDiff = 0;
          vertDiff = 0;
          swipeLength = 0;
          swipeAngle = null;
          swipeDirection = null;
          triggerElementID = null;
      }
      
      function caluculateAngle() {
          var X = startX-curX;
          var Y = curY-startY;
          var Z = Math.round(Math.sqrt(Math.pow(X,2)+Math.pow(Y,2))); //the distance - rounded - in pixels
          var r = Math.atan2(Y,X); //angle in radians (Cartesian system)
          swipeAngle = Math.round(r*180/Math.PI); //angle in degrees
          if ( swipeAngle < 0 ) { swipeAngle =  360 - Math.abs(swipeAngle); }
      }
      
      function determineSwipeDirection() {
          if ( (swipeAngle <= 45) && (swipeAngle >= 0) ) {
              swipeDirection = 'left';
          } else if ( (swipeAngle <= 360) && (swipeAngle >= 315) ) {
              swipeDirection = 'left';
          } else if ( (swipeAngle >= 135) && (swipeAngle <= 225) ) {
              swipeDirection = 'right';
          }
      
          /* else if ( (swipeAngle > 45) && (swipeAngle < 135) ) {
              swipeDirection = 'down';
          } else {
              swipeDirection = 'up';
          }*/
      }
      
      function processingRoutine() {
          var swipedElement = document.getElementById(triggerElementID);
          if ( swipeDirection == 'left' ) {
              // REPLACE WITH YOUR ROUTINES
              event.preventDefault();
              swipedElement.style.backgroundColor = 'orange';
          } else if ( swipeDirection == 'right' ) {
              // REPLACE WITH YOUR ROUTINES
              event.preventDefault();
              swipedElement.style.backgroundColor = 'green';
          } 
      
          /*else if ( swipeDirection == 'up' ) {
              // REPLACE WITH YOUR ROUTINES
              swipedElement.style.backgroundColor = 'maroon';
          } else if ( swipeDirection == 'down' ) {
              // REPLACE WITH YOUR ROUTINES
              swipedElement.style.backgroundColor = 'purple';
          }*/
      }
      

      【讨论】:

      • 这将不再起作用,因为它一开始就不会检测到滑动。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多