metafizzy / flickity

:leaves: Touch, responsive, flickable carousels
https://flickity.metafizzy.co
7.52k stars 605 forks source link

getAdjacentCellElements #69

Closed desandro closed 9 years ago

desandro commented 9 years ago

Originally posted by @wgstjf in #54 (now deleted, and moved here)


I am struggling with one thing though. I have a slider that effectively has 3 is-selected slides at a time, the rest are (through css) set to 0.5 opacity. I can target the is-selected slide wrapper to apply opacity of 1 but can't figure out how to effectively target the preceding and following slide to also make them full opaque. Any help would be greatly appreciated.

Did try the following to apply my own class of 'is-active' but firstly, doesn't seem to target the neighbouring slides correctly and secondly not sure how to make this seamless... :-)

$gallery.on( 'cellSelect', function() {
    var selectedSlide = flkty.selectedIndex;
    var previousSlide = selectedSlide-1;
    var nextSlide = selectedSlide+1;
    $('.applications-slider').removeClass('is-active');
    $( ".slide-wrapper" ).eq( previousSlide ).addClass('is-active');
    $( ".slide-wrapper" ).eq(selectedSlide).addClass('is-active');
  });

Cheers,

Will


desandro commented 9 years ago

Yes, this can be done with a bit of extra code. See demo http://codepen.io/desandro/pen/rarmjG

You'll need to add this bit of code

Flickity.prototype.getAdjacentCellElements = function( adjacentCount, index ) {
  adjacentCount = adjacentCount || 1;
  index = index === undefined ? this.selectedIndex : index;
  var startIndex = index - adjacentCount;
  var cellElems = []; 
  var len = this.cells.length;
  for ( var i = index - adjacentCount; i <= index + adjacentCount; i++ ) {
    var cellIndex = this.options.wrapAround ? fizzyUIUtils.modulo( i, len ) : i;
    var cell = this.cells[ cellIndex ];
    if ( cell ) {
      cellElems.push( cell.element );
    }
  }
  return cellElems;
};

This adds the getAdjacentCellElements method. This method returns an array of elements.

You can then use it like so:

$gallery.on( 'cellSelect', function() {
  $gallery.find('.gallery-cell.is-active').removeClass('is-active');
  var adjCellElems = $gallery.flickity('getAdjacentCellElements');
  $( adjCellElems ).addClass('is-active');
});

getAdjacentCellElements has two arguments

// jQuery
$gallery.flickity( 'getAdjacentCellElements', adjacentCount, index )
// vanilla JS
flkty.getAdjacentCellElements( adjacentCount, index )

getAdjacentCellElements will return wrapped cells, so if wrapAround: true and the index is the last cell, it will return cells 4, 5, and 1.


+1 or subscribe to this issue if you would like to see getAdjacentCellElements get added proper to Flickity

wgstjf commented 9 years ago

@desandro thanks for the truly amazing support. With your help I now have the slider working perfectly. I only wish I had found out about your slider sooner rather than the hours of struggling I have had trying to get slick to do what I wanted :-)

Cheers,

Will

Julix91 commented 5 years ago

4 year old post, so fair enough - but the code pen isn't working anymore. Screen Shot 2019-03-29 at 9 51 52 AM

desandro commented 5 years ago

@Julix91 Pen updated. getAdjacentCellElements has been an official method since Flickity v2.

flkty.getAdjacentCellElements( count, index )
Julix91 commented 5 years ago

Oh neat! I'm still becoming familiar with Flickity. There's a lot more to it than meets the eye! Thanks for your continued support & work on this project, @desandro ! - and congrats and best wishes on your new job.