export default function dragFun(dragBox, binding, call) {
  dragBox.onmousedown = eDown => {
    // 算出鼠标相对元素的位置
    const disX = eDown.clientX - dragBox.offsetLeft
    const disY = eDown.clientY - dragBox.offsetTop
    let getPosition = {}
    let isMove = false
    document.onmousemove = e => {
      // 用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
      const left = e.clientX - disX
      const top = e.clientY - disY
      const right = binding.clientWidth - dragBox.clientWidth
      const bottom = binding.clientHeight - dragBox.clientHeight
      if (left < 0 || top < 0 || right < left || bottom < top) {
        return
      }
      dragBox.style.left = left + 'px'
      dragBox.style.top = top + 'px'
      getPosition = {
        top: top,
        left: left
      }
      isMove = true
    }
    document.onmouseup = () => {
      // 鼠标弹起来的时候不再移动
      document.onmousemove = null
      // 预防鼠标弹起来后还会循环（即预防鼠标放上去的时候还会移动）
      document.onmouseup = null
      if (isMove) {
        isMove = false
        call(getPosition)
      }
    }
  }
}
