简体   繁体   English

具有mousedown事件处理程序的AngularJS指令但是click事件永远不会触发

[英]AngularJS directive with mousedown event handler but click event never fires

I have created a custom directive that wires up the mousedown/touchstart and mouseup/touchend events that add/removes a css3 transform (press/release effect) to the element. 我创建了一个自定义指令,用于连接mousedown / touchstart和mouseup / touchend事件,这些事件可以向元素添加/删除css3转换(按下/释放效果)。

It works great in desktop Chrome and IE, iOS in iOS7 and 6, but Android 4.4.2 it displays the transform but the click event never fires to actually navigate. 它适用于桌面Chrome和IE,iOS7和iOS中的iOS,但Android 4.4.2会显示转换,但点击事件永远不会触发实际导航。

The only way I can get it to work is to click/tap very very fast on the element. 我能让它工作的唯一方法是在元素上非常快速地点击/点击。 It is almost as if the click event is getting swallowed or cancelled when using a normal tap speed. 当使用正常的敲击速度时,几乎就像点击事件被吞下或取消一样。

I apply the directive to divs within and divs with an ng-click directive - neither one works. 我将指令应用于div内部和div使用ng-click指令 - 两者都不起作用。

EDIT: There is a scenario where it does not work in any browser - if the div I add the directive to also has ng-click attached. 编辑:有一种情况,它在任何浏览器中都不起作用 - 如果div我添加指令也附加了ng-click。 If I add it one level down in the DOM by adding a nested div and apply it to that div it works fine. 如果我通过添加一个嵌套的div将它添加到DOM中的一个级别并将其应用于该div它可以正常工作。

Here is an example div wrapped in a link 这是一个包含在链接中的示例div

<a ng-href="#/concept/{{conceptOfDay.id}}">
  <div class="contentBox" touch-tilt>
      <i class="fa fa-2x icon fa-calendar-o"></i>
      <div class="itemTitle">concept of the day</div>
      <div class="itemSubTitle">{{conceptOfDay.title}}</div>
  </div>
</a>

And the directive 和指令

angular.module('app').directive('touchTilt', ['$document', function ($document) {

    var directive = {
        restrict: 'A',
        link: function (scope, element, attr) {

            function mousedown(event) {
                var translateString = "perspective( 800px ) rotateX( 0deg ) rotateY( 0deg ) translateZ( -30px )";
                element.css('transform', translateString);
                $document.on('touchend mouseup', mouseup);
            }

            function mouseup(event) {
                var idleCss = "perspective( 800px ) rotateX( 0deg ) rotateY( 0deg ) translateZ( 0px )";
                element.css('transform', idleCss);
                $document.off('touchend mouseup', mouseup);
            }

            element.on('touchstart mousedown', mousedown);
            element.click(function () { alert('click event handled'); })
        }
    };
    return directive;
}]);

Typically you use the ngTouch module when moving to Android or touch devices. 通常,您在迁移到Android或触摸设备时使用ngTouch模块。 There is a delay on touch devices as noted by this documentation: 如本文档所述,触摸设备有延迟:

https://docs.angularjs.org/api/ngTouch https://docs.angularjs.org/api/ngTouch

Some elements are missing from your example, or you just do it differently then I do, so I won't comment on that in particular (I tend to use ngClick explicitly). 你的例子中缺少一些元素,或者你只是做了不同的事情,所以我不会特别评论(我倾向于明确地使用ngClick)。

Anyway, you should be able to use the ngClick directive in ngTouch to get touch screens to work better. 无论如何,您应该能够在ngTouch中使用ngClick指令来使触摸屏更好地工作。 I had a similar problem and including angular-touch solved quite a few. 我有一个类似的问题,包括角度触摸解决了不少。

You didn't note what libraries you include so if I'm being redundant that's why. 你没有注意到你包含哪些库,所以如果我是多余的,这就是原因。

Inside the link function you are just declaring two handler functions mousedown and mouseup, but you are never setting them up to be fired. 在链接函数中,您只是声明了两个处理函数mousedown和mouseup,但是您永远不会将它们设置为触发。 You need to either add ng-click in the markup or bind the element to the event listeners in the link function. 您需要在标记中添加ng-click或将元素绑定到链接函数中的事件侦听器。

// (Inside of link function)
element.bind('mousedown', function(){
    console.log('clicked on document');
  });

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

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