本文介绍了HammerJS 滑动不适用于具有溢出 css 属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT:该代码段在嵌入问题中时可以正常工作,但在编辑时则不行;这让我意识到这与可以滚动的底层容器有关...我在手机上测试了这个,锤子在没有 overflow 属性的小区域上工作正常.
我更新了代码片段以反映这一点.

EDIT: The snippet works fine when embedded in the question, but not when edited; which made me realize it had to do with underlying containers that can scroll... I tested this on my phone and hammer works fine on the small areas that don't have the overflow property.
I updated the snippet to reflect this.

问题是:如何让它在需要垂直滚动的容器上工作?

The question is: how to get it to work on containers that need to scroll vertically?

我正在 Angular PWA 中构建滑动导航.我让它在 Chrome 桌面上运行,但它在我的手机上不起作用.当我在 Chrome devtools 中尝试触摸模拟器时,根据方向/设备,它变得非常疯狂,大多数情况下根本没有检测到任何东西.panleftpanright 稍微好一点,但由于某种原因完全破坏了锤子(可能是因为我没有对它进行去抖动,它只是在短时间内停止检测而没有任何错误).

I am building in swipe navigation in an Angular PWA. I had it working on Chrome desktop but it did not work on my phone. When I tried the touch emulator in Chrome devtools it went all crazy depending on the orientation / device, mostly not detecting anything at all. panleft and panright work marginally better but for some reason break hammer completely (possibly because I did not debounce it, it just stops detecting without any errors after a short while).

我做错了什么?

当与 Chrome devtools 中的设备模拟器一起使用时,以下代码段的行为类似

The snippet below behaves similar when used with device emulators in Chrome devtools

const hammer = new Hammer(document.documentElement);
hammer.on('swipeleft swiperight', event => {
  console.log(event.type);
});
body,
html {
  height: 100%;
}

.content {
  margin-top: 20%;
  width: 100%;
  height: 80%;
  background: orange;
  overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
Can touch this
<div class="content">Can't touch this (on mobile / emulator)</div>

实际代码

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  constructor(
    private gameService: GameService,
    private routerService: RouterService
  ) {}

  ngOnInit() {
    this.gameService.gameInit();

    const hammer = new Hammer(document.documentElement);
    hammer.on('swipeleft swiperight', event => {
      if (event.type === 'swiperight') {
        this.routerService.navNext();
      } else if (event.type === 'swipeleft') {
        this.routerService.navPrev();
      }
    });
  }
}

推荐答案

正确的答案是将 touch-action: pan-y !important 粘贴到您的班级中,并带有溢出属性

the correct answer is just paste touch-action: pan-y !important in your class with overflow property

这篇关于HammerJS 滑动不适用于具有溢出 css 属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:57