简体   繁体   English

如何处理angular移动版中的长按事件?

[英]How to handle long click event in angular for mobile?

I want to achieve a simple long click (long press) event with angular, working on mobile devices also. 我想用角度实现一个简单的长按(长按)事件,也可以在移动设备上使用。

Live code 现场代码

WORKING ON 继续工作

  1. Chrome - desktop Chrome-桌面
  2. Chrome mobile inspector Chrome移动检查器

NOT WORKING ON 不工作

  1. iOS ( chrome / safari ) - real device iOS(chrome /苹果浏览器)- 真实设备
  2. Android ( chrome ) - real device Android(Chrome)- 真实设备

HTML 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 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 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 */
}

You need to use touchstart and touchend instead Long Press in JavaScript? 您需要使用touchstarttouchend来代替JavaScript中的Long Press吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM