0

I'm using Hammerjs 2.0.8 with Nuxt, and I can't seem to make it detect swipe motion. Here's how I've done the setup. If I change the recognizer to [Hammer.Pan, { direction: Hammer.DIRECTION_HORIZONTAL }] and on pan event hammer.on('pan', this.handleTouchEvents); it runs this.handleTouchEvents method.

<template>
  <v-app>
    <v-main>
      <v-container id="touch-container" fluid>
        <div class="items">
          ...
        </div>
      </v-container>
    </v-main>
  </v-app>
</template>

<script>

export default {
  mounted () {
    const Hammer = require('hammerjs')
    const touchContainer = document.getElementById('touch-container')
    const hammer = new Hammer.Manager(touchContainer, {
      recognizers: [
        [Hammer.Swipe, { direction: Hammer.DIRECTION_HORIZONTAL }]
      ]
    })

    hammer.on('swipe', this.handleTouchEvents)
  },
  methods: {
    handleTouchEvents (e) {
      alert(e.type)
    }
  }
}
</script>

<style scoped>
#touch-container {
  height: 100%;
  min-width: 100%;
  overflow-x: hidden;
}

#items {
  touch-action: pan-y;
}
</style>

Any idea how to resolve this issue?

2
  • do you use SSR?
    – Ilijanovic
    Commented Jun 12, 2022 at 11:09
  • yes, it's using Nuxt's universal mode
    – Afiq Rosli
    Commented Jun 12, 2022 at 13:52

1 Answer 1

0

touchContainer is probably undefined.

2 Things:

  1. Check if its on client side with process.client
  2. Wait until Vue created the elements on the UI with this.$nextTick()

Here try this:

  async mounted () {
 
    if(process.client) {
       await this.$nextTick();
       const Hammer = require('hammerjs')
       const touchContainer = document.getElementById('touch-container')
       const hammer = new Hammer.Manager(touchContainer, {
         recognizers: [
           [Hammer.Swipe, { direction: Hammer.DIRECTION_HORIZONTAL }]
         ]
       })

       hammer.on('swipe', this.handleTouchEvents)
    }
  },
1
  • I've tried this, and it did not work. It does enters the if statement but still didn't recognized the swipe motion. It still only works for pan motion
    – Afiq Rosli
    Commented Jun 13, 2022 at 0:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.