简体   繁体   English

垂直滚动不适用于 HammerJS 和 Angular2

[英]Vertical scroll is not working with HammerJS and Angular2

I'm having a problem using the HammerJS with Angular2.我在使用带有 Angular2 的 HammerJS 时遇到问题。 I have a carousel (based on the bootstrap carousel with Angular2 event handlers) where I'm listening to the swipe left and swipe right events.我有一个轮播(基于带有 Angular2 事件处理程序的引导轮播),我在那里监听向左滑动向右滑动事件。 The swipe itself works perfectly.滑动本身完美无缺。 The problem is that since I use the HammerJS I can not scroll up/down over my carousel component and since it's a full viewport sized item it's a huge issue.问题是,由于我使用 HammerJS,我无法在我的轮播组件上向上/向下滚动,而且由于它是一个完整的视口大小的项目,这是一个大问题。

How can this issue be solved?如何解决这个问题?

Platform:平台:
Angular2 2.1.2 Angular2 2.1.2
Samsung Galaxy S2 with Android 5.1.1三星 Galaxy S2 与 Android 5.1.1
Google Chrome for Android: 54.0.2840.85安卓版谷歌浏览器:54.0.2840.85

Got it!知道了!

In your AppModule:在您的 AppModule 中:

import { HAMMER_GESTURE_CONFIG, HammerGestureConfig } from '@angular/platform-browser';

export class MyHammerConfig extends HammerGestureConfig {
    overrides = <any> {
        'pinch': { enable: false },
        'rotate': { enable: false }
    }
}

@NgModule({
    declarations: [
        // ...
    ],
    imports: [
        // ...
    ],
    providers: [
        // ...
        {
            provide: HAMMER_GESTURE_CONFIG,
            useClass: MyHammerConfig
        }
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule {}

Now vertical scrolling works, after disabling pinch and rotate .现在垂直滚动工作,在禁用pinchrotate Couldn't find any other way so far.到目前为止找不到任何其他方法。 I'm not sure what happens to the pinch and rotate events (I mean they would be disabled, I think).我不确定pinchrotate事件会发生什么(我的意思是它们会被禁用,我认为)。 But even without this config, attaching a (pinch)="onPinch($event)" - didn't do anything anyway.但即使没有这个配置,附加一个(pinch)="onPinch($event)" - 无论如何也没有做任何事情。

Angular version in my project: 2.4.1我的项目中的 Angular 版本:2.4.1

Tested on:测试:

  • Chrome for Windows (on a Surface, so real touchscreen - not just emulated) v 55.0.2883.87 m (64-bit)适用于 Windows 的 Chrome(在 Surface 上,如此真实的触摸屏 - 不仅仅是模拟)v 55.0.2883.87 m(64 位)
  • Chrome for Android - v 55.0.2883.91适用于 Android 的 Chrome - v 55.0.2883.91

This solution works for my Chrome 66 (Angular 6 app).此解决方案适用于我的 Chrome 66(Angular 6 应用程序)。 Scrolling is enabled, swipe right/left works also:启用滚动,向右/向左滑动也有效:

import {
  HammerGestureConfig,
  HAMMER_GESTURE_CONFIG
} from '@angular/platform-browser';
import * as Hammer from 'hammerjs';

export class MyHammerConfig extends HammerGestureConfig {
  buildHammer(element: HTMLElement) {
    const mc = new Hammer(element, {
      touchAction: 'pan-y'
    });

    return mc;
  }
}

@NgModule({
    declarations: [
        // ...
    ],
    imports: [
        // ...
    ],
    providers: [
        // ...
        {
            provide: HAMMER_GESTURE_CONFIG,
            useClass: MyHammerConfig
        }
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule {}

Like a few other answers have mentioned in就像在中提到的其他一些答案一样

npm install hammerjs --save

maint.ts维护文件

import 'hammerjs';

app.module应用模块

import { BrowserModule, HammerGestureConfig, HAMMER_GESTURE_CONFIG } 
from '@angular/platform-browser';
...
export class HammerConfig extends HammerGestureConfig {
  overrides = <any> {
      'pinch': { enable: false },
      'rotate': { enable: false }
  }
}
...
  providers: [
    {
      provide: HAMMER_GESTURE_CONFIG,
      useClass: HammerConfig
    }],
...

I tried a million variations of the configuration and scrolling still did not work when I tested in chrome, I dont know if its the version or what but it did not work.我尝试了 100 万种配置变化,当我在 chrome 中测试时滚动仍然不起作用,我不知道它的版本还是什么,但它不起作用。 When I tested in an actual mobile phone scrolling was working!当我在实际的手机中测试滚动是否有效!

ionic with angular 9 do not forget to add in app.module.ts带有 angular 9 的 ionic不要忘记在 app.module.ts 中添加

import { HammerModule } from '@angular/platform-browser';
imports: [
    ...,
    HammerModule,
  ],

After 2 days of research and frustration I've found the only working solution for me:经过 2 天的研究和挫折,我找到了唯一适合我的解决方案:

import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';

export class CustomHammerConfig extends HammerGestureConfig  {
    overrides = <any>{
        'pinch': { enable: false },
        'rotate': { enable: false }
    }
}


@NgModule({
// ... declarations, imports,
providers: [
        // ...
        {
            provide: HAMMER_GESTURE_CONFIG,
            useClass: CustomHammerConfig
        }
    ],
bootstrap: [ AppComponent ]
})
export class AppModule {
}


taken from here取自这里

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

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