Skip to content

cancel repeatedly x scrolling  #131

Open
@webspecialist

Description

@webspecialist

I saw a relative question here with that "problem" but it didn't helped me so much.

I am trying to create x scrolling (dynamic) slider. So here is my code working with Nuxt.

`

<div class="container">
  <div id="container" class="aligner slide-out" ref="slider">
    <div class="slide-in" >
      <div class="slide-col-4" v-for="o in myObj" :key="o.id" :ref="'r'+o.id">
        <!-- <button @click="displayRef('r'+o.id, o.id)">
             {{'r'+o.id}}
        </button> -->
      </div>
    </div>
  </div>
  <div class="myslide-btns aligner">
    <div class="the-center">
      <div class="left" @click="clickSlide(1)"><font-awesome-icon :icon="[ 'fas', 'chevron-left' ]" /></div>
      <div class="right" @click="clickSlide(2)" ref="right-arr"><font-awesome-icon :icon="[ 'fas', 'chevron-right' ]" /></div>
    </div>
  </div>
</div>

<script>
export default {
  data(){
      return {
          right: '',
          stopSlide: false,
          myObj: [
            {
              id: 0
            },
            {
              id: 1
            },
            {
              id: 2
            },
            {
              id: 3
            }
          ]
      }
  },
  methods: {
    clickSlide(way) {
      if(this.stopSlide == true){
        return
      }
    let VueScrollTo = require('vue-scrollto');
    let sliderPos = this.$refs.slider.scrollLeft
    let sizebox = (320 + 30)
    let sizeSlider = (320 + 30) * this.myObj.length
    let pos = this.myObj.length
    let afterPos = ((sizeSlider - sliderPos) / sizebox)
    let nextPos = pos - afterPos
    let nextDest = 0
    console.log(sliderPos)
      let prevDest = 0
      if(way == 2){
        nextDest = 1+Math.floor(nextPos)
        prevDest = Math.round(nextPos)
      }else{
        nextDest = -1 + Math.floor(nextPos)
        prevDest = Math.floor(nextPos)
      }
      console.log(prevDest)
      console.log(nextDest)
      if(nextDest < 0 && way == 1){
        return
      }
      let repeat = 0
      let element = this.$refs['r'+nextDest.toString()][0]
      let duration = 500
      
      let options = {
          container: '#container',
          easing: 'ease-in-out',
          offset: -20,
          force: true,
          cancelable: true,
          myThis: this,
          onStart: function(element) {
            // scrolling started
            console.log('start')
            console.log()
            options.myThis.turnTrue()
          },
          onDone: function(element) {
            console.log('done')
            options.myThis.turnTrue()
          },
          onCancel: function() {
            console.log('stop')
          },
          x: true,
          y: false
      }
      
        let cancelScroll = this.$scrollTo(element, duration, options)
      
      if(this.stopSlide == true){
        cancelScroll()
      }
    },
    turnTrue(){
      return this.stopSlide == true
    },
    turnFalse(){
      return this.stopSlide = false
    }
  }
}
</script>

The problem is when I repeatedly trying to click ex. the next button the animation flickers. If someone hit the button multiple times, I want to stop the scrolling if the transition is not completed.

How to do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    nuxtIssues related to nuxt.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions