当前位置: 首页 > 工具软件 > cmx.js > 使用案例 >

utils.js常用的公共方法的整理

方英耀
2023-12-01

import moment from 'moment'

//时间过滤器

export function dateTime(data, format = 'yyyy-MM-DD HH:mm:ss') {

  if (!data) return ''

  return moment(data).format(format)

}

//利用递归-根据id返回对应的数据对象

export function getId(data, id) {

  var obj = {}

  data.forEach(function (item) {

    if (item.id == id) {

      obj = item

    } else if (item.goods && item.goods.length > 0) {

      obj = getId(item.goods, id)

    }

  })

  return obj

}        

//移动的盒子

export function handleMove(dom) {

  var cmX = 0

  var cmY = 0

  var mouseIsDown = false

  dom.onmousedown = (e) => {

    cmX = e.offsetX

    cmY = e.offsetY

    mouseIsDown = true

  }

  dom.onmousemove = (e) => {

    const mX = e.offsetX

    const mY = e.offsetY

    const dY = dom.offsetTop

    const dX = dom.offsetLeft

    if (mouseIsDown) {

      dom.style.left = dX + (mX - cmX) + 'px'

      dom.style.top = dY + (mY - cmY) + 'px'

    }

  }

  document.onmouseup = () => {

    mouseIsDown = false

  }

}

//监听鼠标事件

export function keyDown() {

  document.onkeydown = (e) => {

    let e1 = e || window.event || arguments.callee.caller.arguments[0]

    if (e1 && e1.keyCode == 13) {

      console.log('enter' + '', e1.keyCode)

    }

  }

}

 类似资料: