【发布时间】:2020-01-19 16:37:44
【问题描述】:
我正在尝试在我的应用程序的整个屏幕上捕捉滑动手势。这个想法是让其他服务可以侦听这些调度操作,在本例中是用于导航目的的路由器。
因为我只希望将这些侦听器放在一个地方,所以我尝试将它们附加到 body 或覆盖我的应用程序屏幕的 div 上。这两种方法都不能按我的意愿工作。
<div (swipeleft)="swipeLeft()" (swiperight)="swipeRight()" class="touch"></div>
swipeLeft() {
this.store.dispatch(UserActions.swipeLeft());
}
回想起来,触摸层的问题应该很明显:它覆盖了屏幕,从而覆盖了应用程序的其余部分。将pointer-events: none; 设置为到达应用程序的其余部分会破坏滑动检测。
const mc = new Hammer(document.querySelector('body'));
mc.on('swipeleft swiperight', (ev) => {
console.log(ev.type);
// this.store.dispatch(UserActions.swipeLeft());
});
在此处附加它的问题在于,它似乎只在某些元素上注册滑动,例如 app-root 和我拥有但不在我的路由器插座上的状态栏,也不是底部包含一些按钮的工具栏。
那么,我如何在整个应用程序上捕获滑动?
我尝试创建一个 sn-p 来重现该问题,但没有 Angular 组件,它的行为几乎与我希望我的应用程序一样。尽管尝试从 sn-p 中的按钮边距周围滑动时,可以观察到与我的问题类似的行为。
const touchLayer = document.getElementById('touch');
const body = document.querySelector('body');
const mc = new Hammer(body);
mc.on("swipeleft swiperight", function(ev) {
touchLayer.textContent = ev.type + " gesture detected.";
});
#touch,
#myApp {
position: fixed;
height: 300px;
width: 100%;
}
#myApp {
background: repeating-linear-gradient(-55deg, #666, #666 10px, #333 10px, #333 20px);
opacity: .5;
}
#touch {
background: cyan;
text-align: center;
font: 30px/300px Helvetica, Arial, sans-serif;
opacity: .5;
}
.card {
width: 200px;
height: 100px;
background: red;
position: relative;
margin: 1em;
}
button {
margin: 2em;
right: 0;
bottom: 0;
position: absolute;
}
<script src="https://hammerjs.github.io/dist/hammer.js"></script>
<!-- Tried putting this on top, blocking the screen -->
<div id="touch"></div>
<div id="myApp">
<div class="card">
<button onclick="alert('test')">Button!</button>
</div>
<div class="card">
<button onclick="alert('test')">Button!</button>
</div>
</div>
【问题讨论】:
标签: angular angular-material2 ngrx gesture hammer.js