Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使hammer.js滑动事件在父元素上触发,而不是在常规子元素上触发?_Javascript_Hammer.js - Fatal编程技术网

Javascript 如何使hammer.js滑动事件在父元素上触发,而不是在常规子元素上触发?

Javascript 如何使hammer.js滑动事件在父元素上触发,而不是在常规子元素上触发?,javascript,hammer.js,Javascript,Hammer.js,我正在使用hammer.js2.0 我有一个容器,需要有刷卡事件。但是,该容器有一个元素,它是一个旋转木马,包含自己的滑动通风口。除了旋转木马,我怎么能让刷卡活动到处都着火 var self = this, hammerContainer = document.querySelector('section.content-wrapper'), carousel = document.querySelector('.image-gallery-carousel'), ham

我正在使用hammer.js2.0

我有一个容器,需要有刷卡事件。但是,该容器有一个元素,它是一个旋转木马,包含自己的滑动通风口。除了旋转木马,我怎么能让刷卡活动到处都着火

var self = this,
    hammerContainer = document.querySelector('section.content-wrapper'),
    carousel = document.querySelector('.image-gallery-carousel'),
    hammerOpts = {
      threshold: 4,
      velocity: .3
    },
    hammer = new Hammer(hammerContainer, hammerOpts);

Hammer.off(carousel, 'swipeleft swiperight', function () {
});

hammer.on('swipeleft', function (e) {
// do some other things
});
.off
方法在我的转盘上似乎不起作用。锤击事件还在传播?起泡


它需要的不仅仅是旋转木马,还有它可能拥有的任何孩子

我发现唯一可行的方法是让jQuery参与进来。我觉得自己不是一个更好的人

  • 抓取
    事件。目标
  • 使用jQuery的
    .parents()
    方法获取目标的所有属于给定类名的父类
  • 使用
    if
    语句检查这些父项的长度,如果长度为0,则执行我的滑动
  • 使用属性选择器检查单词carousel作为类名的所有实例和变体,例如:
    [class*=“carousel”]

  • 我觉得事件传播应该是我能做的事情。比如,调用
    e.stopPropagation()

    有人,有人,请告诉我一个更好的方法

    var self = this,
      hammerContainer = document.querySelector('section.content-wrapper'),
      hammerOpts = {
        threshold: 4,
        velocity: .3
      },
      hammer = new Hammer(hammerContainer, hammerOpts);
    
      hammer.on('swiperight', function (e) {
        if (!$(e.target).parents('[class*="carousel"]').length) {
         //do swipey things   
        }
      });