滑动并点击 Hammer js 链接

发布于 2025-01-17 19:47:44 字数 1500 浏览 3 评论 0原文

我有 Angular 12 应用程序。

有带有图像的轮播,它们是链接。 我按照预期集成了 Hammer JS:

import * as Hammer from 'hammerjs';

export class HammerConfig extends HammerGestureConfig {
  overrides = {
    swipe: { direction: Hammer.DIRECTION_ALL },
  };
}

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

我在 Carousel 包装器 元素上应用 Hammer 事件处理程序,如下所示:

<div
  class="slides"
  (swipeleft)="swipeLeft($event)"
  (swiperight)="swipeRight($event)"
  (tap)="tap($event)"
  [style.touch-action]="'pan-y'"
>

问题来了。尝试滑动链接会导致默认的拖动行为。 该图像也会导致类似的默认拖动行为。 解决此问题的方法是为 dom 元素添加 css 规则:

img, a {
  pointer-events: none;
}

用于滑动轮播窗格的滑动功能正在运行 但现在 click/tap 事件不起作用,因为 pointer-events: none;

我知道 Hammer 配置具有以下界面:

class HammerGestureConfig {
  events: string[]
  overrides: {...}
  options?: {...}
  buildHammer(element: HTMLElement): HammerInstance
}

我的问题是:是吗通过应用特定的配置和 CSS 规则可以实现 在轮播的链接窗格上滑动和单击/点击?

先感谢您。

I have Angular 12 application.

There is carousel with images, which are links.
I integrated Hammer JS as expected:

import * as Hammer from 'hammerjs';

export class HammerConfig extends HammerGestureConfig {
  overrides = {
    swipe: { direction: Hammer.DIRECTION_ALL },
  };
}

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

I apply Hammer event handlers on the Carousel wrapper element like this:

<div
  class="slides"
  (swipeleft)="swipeLeft($event)"
  (swiperight)="swipeRight($event)"
  (tap)="tap($event)"
  [style.touch-action]="'pan-y'"
>

And here comes the issue. Trying to swipe a link leads to default dragging behavior.
The image is also causing similar default dragging behavior.
A fix for that is to add css rule for both <img> and <a> dom elements:

img, a {
  pointer-events: none;
}

The swipe functionality for swiping the Carousel panes is working
but now the click/tap event is not working because of pointer-events: none;

I know that there is Hammer configuration with the following interface:

class HammerGestureConfig {
  events: string[]
  overrides: {...}
  options?: {...}
  buildHammer(element: HTMLElement): HammerInstance
}

My questions is: is it possible by applying specific configuration and css rules
to have both swiping and clicking/tapping over link panes of the Carousel?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕烟庭风 2025-01-24 19:47:45

也许它不是那么重要,但是它仍然是解决这种情况的解决方案。

因此,作为案例的快速摘要:

-1-我有一个图像旋转木马。
-2-我想水平滑动。
-3-我想卷轴如果我将“手指”放在旋转木马上,则垂直事件。
-4-我想单击/点击一些轮播窗格并导航链接专用链接。

问题 4-th 点。
为了使链接/images 滑动,我必须应用css规则:

img, a {
  pointer-events: none;
}

这是在单击窗格/链接/图像时阻止导航的。

因此,一种解决方案是在Angular中触发程序化导航使用:

this.router.navigate(['some_route'])
// or
this.router.navigateByUrl('/some_route')

当然,该信息是通过点击事件获得的 event.target信息。
当然,可能还有其他更智能的解决方案。但是,至少这种方法以非常一致的方式起作用。

maybe it is not so relevant, but still it is a solution for this case.

So, as quick summary of the case:

-1- I have an image carousel.
-2- I want to swipe the carousel horizontally.
-3- I want to scroll the page vertically event if I place the "finger" over the carousel.
-4- I want to click/tap over some carousel pane and navigate the link dedicated link.

The issue was with the 4-th point.
In order to have swipe over link/images, I had to apply css rule:

img, a {
  pointer-events: none;
}

And that was preventing the navigation when the pane/link/image is clicked.

So, one solution is to trigger programmatic navigation in angular using:

this.router.navigate(['some_route'])
// or
this.router.navigateByUrl('/some_route')

Of course, the information is obtained on tap event with event.target information.
Of course there might be other, more smart solutions. But, at least, this approach is working in very consistent way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文