export function swiper(el: any, options: any) {
  //设置开关，监听move事件
  let isMove: any = false
  // 设置手指触摸开始的坐标
  let startX: any = 0
  let startY: any = 0
  // 设置手指移动的坐标
  let moveX: any = 0
  let moveY: any = 0
  //设置指针距离元素的边框的距离
  let disX: any = 0
  let disY: any = 0
  // 如果用户未传入参数2，自己定义一个
  let fun: any = function() {
  }
  let data: any = {
    swipeLeft: fun,
    swipeRight: fun,
    swipeDown: fun,
    swipeUp: fun,
    drag: fun
  }
  // 判断是否传入参数2，有的话覆盖默认值
  Object.assign(data, options)
  // 给元素绑定三个事件
  el.addEventListener('touchstart', function(e: any) {
    //获取手指开始的坐标
    startX = e.touches[0].pageX
    startY = e.touches[0].pageY
    //计算指针距离元素边框的位置
    disX = 0
    disY = 0
  }, { passive: true })
  el.addEventListener('touchmove', function(e: any) {
    //如果触发了move事件，打开开关
    isMove = true
    // 获取移动时的坐标
    moveX = e.touches[0].pageX
    moveY = e.touches[0].pageY
    e.mation = {
      startX: startX,
      startY: startY,
      moveX: moveX,
      moveY: moveY,
      disX: disX,
      disY: disY
    }
    data.drag.call(el, e)
    // 判断是否触发了move事件
    if (isMove) {
      // 计算水平边长
      let absX: any = Math.abs(moveX - startX)
      // 计算垂直边长
      let absY: any = Math.abs(moveY - startY)
      // 判断垂直还是水平滑动
      if (absX > absY) {
        // console.log('水平滑动');
        //再判断是左滑右滑
        if (moveX - startX > 0) {
          data.swipeRight.call(el, e)
        } else {
          data.swipeLeft.call(el, e)
        }
      } else {
        // console.log('垂直滑动');
        //判断是上滑还是下滑
        if (moveY - startX > 0) {  //  下滑
          data.swipeDown.call(el, e)
        } else {
          data.swipeUp.call(el, e)
        }
      }
    }
  }, { passive: true })
  el.addEventListener('touchend', function(e: any) {
    isMove = false
  })
}
