Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding .is-next and .is-previous classes to slides #847

Closed
YuraKolesnikov opened this issue Oct 3, 2018 · 5 comments
Closed

Adding .is-next and .is-previous classes to slides #847

YuraKolesnikov opened this issue Oct 3, 2018 · 5 comments

Comments

@YuraKolesnikov
Copy link

Trying this in my local Vue.js project and it didn't work. I'm not really sure where to add this code so I just added it at the end of the file. Pls help!


return Flickity;

}));

Flickity.createMethods.push('_createPrevNextCells');

Flickity.prototype._createPrevNextCells = function() {
  this.on( 'select', this.setPrevNextCells );
};

Flickity.prototype.setPrevNextCells = function() {
  // remove classes
  changeSlideClasses( this.previousSlide, 'remove', 'is-previous' );
  changeSlideClasses( this.nextSlide, 'remove', 'is-next' );
  // set slides
  this.previousSlide = this.slides[ this.selectedIndex - 1 ];
  this.nextSlide = this.slides[ this.selectedIndex + 1 ];
  // add classes
  changeSlideClasses( this.previousSlide, 'add', 'is-previous' );
  changeSlideClasses( this.nextSlide, 'add', 'is-next' );
};

function changeSlideClasses( slide, method, className ) {
  if ( !slide ) {
    return;
  }
  slide.getCellElements().forEach( function( cellElem ) {
    cellElem.classList[ method ]( className );
  });
}

https://codepen.io/desandro/pen/MagWrN

@desandro
Copy link
Member

desandro commented Oct 5, 2018

You'll need to add this code before you initialize Flickity.

@ghost
Copy link

ghost commented Apr 30, 2019

A looping variant could be done like this:

Flickity.createMethods.push('_createPrevNextCells')
Flickity.prototype._createPrevNextCells = function() {
  this.on( 'select', this.setPrevNextCells )
}
Flickity.prototype.setPrevNextCells = function() {
  // remove classes
  changeSlideClasses( this.previousSlide, 'remove', 'is-previous' )
  changeSlideClasses( this.nextSlide, 'remove', 'is-next' )
  // set slides
  if (this.selectedIndex - 1 < 0) {
    this.previousSlide = this.slides[ this.slides.length - 1 ]
  }
  else {
    this.previousSlide = this.slides[ this.selectedIndex - 1 ]
  }
  if (this.selectedIndex + 1 === this.slides.length) {
    this.nextSlide = this.slides[0]
  }
  else {
    this.nextSlide = this.slides[this.selectedIndex + 1]
  }
  // add classes
  changeSlideClasses( this.previousSlide, 'add', 'is-previous' )
  changeSlideClasses( this.nextSlide, 'add', 'is-next' )
}
function changeSlideClasses( slide, method, className ) {
  if (!slide) {
    return
  }
  slide.getCellElements().forEach( function( cellElem ) {
    cellElem.classList[ method ]( className )
  })
}

@ghost
Copy link

ghost commented Apr 30, 2019

And with Vue I did like this:

<script>
  let Flickity;
  if (process.browser) {
    Flickity = require('flickity');
    Flickity.createMethods.push('_createPrevNextCells')
    Flickity.prototype._createPrevNextCells = function() {
      this.on( 'select', this.setPrevNextCells )
    }
    Flickity.prototype.setPrevNextCells = function() {
      // remove classes
      changeSlideClasses( this.previousSlide, 'remove', 'is-previous' )
      changeSlideClasses( this.nextSlide, 'remove', 'is-next' )
      // set slides
      if (this.selectedIndex - 1 < 0) {
        this.previousSlide = this.slides[ this.slides.length - 1 ]
      }
      else {
        this.previousSlide = this.slides[ this.selectedIndex - 1 ]
      }
      if (this.selectedIndex + 1 === this.slides.length) {
        this.nextSlide = this.slides[0]
      }
      else {
        this.nextSlide = this.slides[this.selectedIndex + 1]
      }
      // add classes
      changeSlideClasses( this.previousSlide, 'add', 'is-previous' )
      changeSlideClasses( this.nextSlide, 'add', 'is-next' )
    }
    function changeSlideClasses( slide, method, className ) {
      if (!slide) {
        return
      }
      slide.getCellElements().forEach( function( cellElem ) {
        cellElem.classList[ method ]( className )
      })
    }
  }
  export default {
    name: 'Slide',
    data() {
      return {
        flickity: null,
        options: {
          prevNextButtons: false,
          pageDots: false,
          wrapAround: true,
          initialIndex: 1,
          cellAlign: 'center',
          dragThreshold: 10,
          selectedAttraction: 0.1,
          friction: 0.4
        }
      }
    },
    mounted: function() {
      this.init();
    },
    beforeDestroy() {
      if (this.flickity) {
        this.flickity.destroy()
        this.flickity = null
      }
    },
    methods: {
      init () {
        this.flickity = new Flickity(this.$refs.slide, this.options);
      }
    }
  }
</script>

<template>
  <div
    ref="slide"
    class="Slide">
    <slot/>
  </div>
</template>

@jagoncalves14
Copy link

jagoncalves14 commented Aug 7, 2019

Hi @desandro, this issue was closed, but I really think that this should be a default feature on Flickity.

@umairqazi523
Copy link

I did like this to get the is-previous-previous and is-next-next to get the effect of the stacked cards especially for wrapAround: true

`

Flickity.createMethods.push('_createPrevNextCells')

Flickity.prototype._createPrevNextCells = function () {
    this.on('select', this.setPrevNextCells)
}

Flickity.prototype.setPrevNextCells = function () {

    changeSlideClasses(this.previousSlide, 'remove', 'is-previous')
    changeSlideClasses(this.previousPrevSlide, 'remove', 'is-previous-previous')
    changeSlideClasses(this.nextSlide, 'remove', 'is-next')
    changeSlideClasses(this.nextNextSlide, 'remove', 'is-next-next')
    
    if (this.selectedIndex - 1 < 0) {
        this.previousSlide = this.slides[this.slides.length - 1]
        this.previousPrevSlide = this.slides[this.slides.length - 2]
    } else {
        this.previousSlide = this.slides[this.selectedIndex - 1]
        this.previousPrevSlide = this.slides[this.selectedIndex - 2]
    }

    if (this.selectedIndex === 1) {
        this.previousPrevSlide = this.slides[this.slides.length - 1]
    } else {}

    if (this.selectedIndex + 1 === this.slides.length) {
        this.nextSlide = this.slides[0]
        this.nextNextSlide = this.slides[1]
    } else {
        this.nextSlide = this.slides[this.selectedIndex + 1]
        this.nextNextSlide = this.slides[this.selectedIndex + 2]
    }

    if (this.selectedIndex + 2 === this.slides.length) {
        this.nextNextSlide = this.slides[0]
    } else {}

    changeSlideClasses(this.previousSlide, 'add', 'is-previous')
    changeSlideClasses(this.previousPrevSlide, 'add', 'is-previous-previous')
    changeSlideClasses(this.nextSlide, 'add', 'is-next')
    changeSlideClasses(this.nextNextSlide, 'add', 'is-next-next')
}
function changeSlideClasses(slide, method, className) {
    if (!slide) {
        return
    }
    slide.getCellElements().forEach(function (cellElem) {
        cellElem.classList[method](className)
    })
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants