touch.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import Hammer from 'hammerjs'
  2. export const HammerMixins = {
  3. data () {
  4. return {
  5. hammerManager: null,
  6. hasHammertap: false,
  7. hasHammerPan: false,
  8. hasHammerPinch: false
  9. }
  10. },
  11. methods: {
  12. initHammerTargrt (target, { tap, pan, pinch }) {
  13. this.hammerManager = new Hammer.Manager(target)
  14. const hammerTap = new Hammer.Tap({ pointers: 1 })
  15. const hammerPan = new Hammer.Pan({ pointers: 1 })
  16. const hammerPinch = new Hammer.Pinch({ pointers: 2 })
  17. let opts = []
  18. this.hasHammertap = tap
  19. this.hasHammerPan = pan
  20. this.hasHammerPinch = pinch
  21. if (tap) opts.push(hammerTap)
  22. if (pan) opts.push(hammerPan)
  23. if (pinch) opts.push(hammerPinch)
  24. this.hammerManager.add(opts)
  25. this.initHammerEvent()
  26. },
  27. initHammerEvent () {
  28. // 监听-tab
  29. if (this.hasHammertap) {
  30. this.hammerManager.on(`tap`, (ev) => {
  31. ev.preventDefault()
  32. this.visible = false
  33. })
  34. }
  35. // 监听-拖拽
  36. this.hammerManager.on(`panstart panmove panend tap, multitap`, (ev) => {
  37. ev.preventDefault()
  38. })
  39. // 监听-缩放
  40. this.hammerManager.on(`pinchmove pinchin pinchout pinchend`, (ev) => {
  41. ev.preventDefault()
  42. })
  43. }
  44. }
  45. }