监听-Listen

监听DOM事件

引入

import { listen } from 'popmotion';

使用

listen(document, 'mousemove')
  .start((e) => console.log(e));

监听多个事件

listen(document, 'touchstart touchend')

链式操作

const onMultitouch = listen(document, 'touchstart')
  .filter(({ touches }) => touches.length > 1);

onMultitouch.start((e) => ...);

上面的例子只有触摸点超过两个的时候才会触发

配置选项

listen的第三个参数允许做一些配置,具体参数说明可以参考HTML的原生监听事件

type EventOpts = boolean | {
  capture?: boolean;
  passive?: boolean;
  once?: boolean;
};

listen(element: Element, eventNames: string, opts?: EventOpts): Action

方法

action

实例方法

stop

action