Giter VIP home page Giter VIP logo

mtouch's Introduction

mTouch

mTouch移动端 ( 兼容pc端) 手势操作库

支持的事件:

  • tap 单击
  • doubletap 双击
  • longtap 长按
  • swipestart 滑动开始
  • swipeend 滑动结束
  • swiping 滑动
  • swipeleft 向左划
  • swiperight 向右划
  • swipeup 向上划
  • swipedown 向下划

提供的接口

####1、mTouch.config(配置项)

  mTouch.config({
    tapMaxDistance: 10,		//单击事件允许的滑动距离
    doubleTapDelay: 200,	//双击事件的延时时长(两次单击的最大时间间隔)
    longTapDelay: 700,		//长按事件的最小时长
    swipeMinDistance: 20,	//触发方向滑动的最小距离
    swipeTime: 300			//触发方向滑动允许的最长时长
  })

以上是默认值,可根据具体使用场景自行配制配置项,但需要注意每个配置项之间的约束关系,比如longTapDelay不能比doubleTapDelay小...

####2、.on(eventType, [proxyStr], handler(event)) 绑定事件方法,使用方式类似jQueryon方法,支持链式调用,支持事件委托,回调函数返回false阻止冒泡及默认行为,同样可以用原生的e.stoPropagation()e.preventDefault()

注:回调函数中被注入的参数event是拓展了的原生事件对象, 添加了属性event.mTouchEvent

mTouchEvent = {
  type: string,
  target: dom,
  pageX: number,
  pageY: number,
  startX: number,
  startY: number,
  moveX: number,
  moveY: number
}
其中 startX、startY、moveX、moveY 只有 swiping 事件才有

使用方法

mTouch('.btn').on('tap', function (e) {
  //...
}).on('doubletap', function (e) {
  //...
})
.on('longtap', function (e) {
  //...
});

mTouch('.btn-group').on('tap', '.btn', function (e) {
  //...
});

####3、.off(eventType, proxyStr, handler) 取消绑定事件方法,使用方式类似jQueryoff,有一点需要注意,通过事件委托绑定的事件必须得由实际绑定事件的节点取消绑定,如:

mTouch('.btn-group').on('tap', '.btn', function (e) {
  //...
});

.btn的tap事件委托到.btn-group,要取消该tap事件,要这样做:
mTouch('.btn-group').off('tap', '.btn');

暂没有实现这种方式:
mTouch('.btn').off('tap'); //错误的方式

更多用法请查看 demo 为了你的更佳体验,请用Chrome模拟mobile或手机打开

vue 版本

<body>
  ...
  <div class="demo">
    <span v-touch:tap="onTap">点我</span>
  </div>
  ...
  <script>
    Vue.use('mTouch');
    var vue = new Vue({
      el: '.demo',
      data: {},
      methods: {
        onTap: function () {
          console.log('你点击了一下');
        }
      }
    })
  </script>
</body>

感谢您的阅读!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.