如何在移动端处理Angular的长按事件?

3

我想用Angular实现一个简单的长按事件,在移动设备上也能正常工作。

演示代码

适用于:

  1. Chrome - 桌面版
  2. Chrome 移动端调试工具

不适用于:

  1. iOS(chrome/safari)- 真实设备
  2. Android(chrome)- 真实设备

HTML

<div class="container-fluid" ng-app="app" ng-controller="MainCtrl">
  <center>
    <h3 ng-bind="test"></h3>
    <div class="box noselect"
         on-long-press="itemOnLongPress()"
         on-touch-end="itemOnTouchEnd()">
      <span>Document</span>
    </div>
  </center>
</div>

JAVASCRIPT

angular.module('app',[])
  .directive('onLongPress', function($timeout) {
    return {
      restrict: 'A',
      link: function($scope, $elm, $attrs) {
        $elm.bind('mousedown', function(evt) {
          // Locally scoped variable that will keep track of the long press
        $scope.longPress = true;

        // We'll set a timeout for 600 ms for a long press
        $timeout(function() {
          if ($scope.longPress) {
            // If the touchend event hasn't fired,
            // apply the function given in on the element's on-long-press attribute
            $scope.$apply(function() {
              $scope.$eval($attrs.onLongPress)
                    });
                }
            }, 600);
        });

        $elm.bind('mouseup', function(evt) {
          // Prevent the onLongPress event from firing
          $scope.longPress = false;
          // If there is an on-touch-end function attached to this element, apply it
          if ($attrs.onTouchEnd) {
            $scope.$apply(function() {
              $scope.$eval($attrs.onTouchEnd)
                });
            }
      });
    }
  };
})

.controller('MainCtrl', function($scope){
  $scope.test = 'Angular 1.4.7';
  $scope.itemOnLongPress = function(){
    $scope.test = "Long pressed";
  };
  $scope.itemOnTouchEnd = function(){
    $scope.test = "Touch end";
  };
});

CSS

.box{
  width:300px;
  height:300px;
  background:black;
  color:#fff;
  font-size:20px;
}
.noselect {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none;   /* Chrome/Safari/Opera */
  -khtml-user-select: none;    /* Konqueror */
  -moz-user-select: none;      /* Firefox */
  -ms-user-select: none;       /* Internet Explorer/Edge */
  user-select: none;           /* Non-prefixed version, currently
                              not supported by any browser */
}

你需要使用touchstarttouchend代替mousedownmouseup - gevorg
1个回答

6

您需要使用 touchstarttouchend 代替 JavaScript中的长按操作?

$elm.bind('touchstart', function(evt) {
    // Locally scoped variable that will keep track of the long press
    $scope.longPress = true;

    // We'll set a timeout for 600 ms for a long press
    $timeout(function() {
        if ($scope.longPress) {
            // If the touchend event hasn't fired,
            // apply the function given in on the element's on-long-press attribute
            $scope.$apply(function() {
                $scope.$eval($attrs.onLongPress)
            });
        }
    }, 600);
});

$elm.bind('touchend', function(evt) {
    // Prevent the onLongPress event from firing
    $scope.longPress = false;

1
非常感谢你,Gevorg!! - que1326

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接