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

Get visible elements currently in viewport without groupCells #638

Open
yesh opened this issue Sep 20, 2017 · 10 comments
Open

Get visible elements currently in viewport without groupCells #638

yesh opened this issue Sep 20, 2017 · 10 comments

Comments

@yesh
Copy link

yesh commented Sep 20, 2017

Hi @desandro ,

don't know if it is supported in some way, but I wanted to ask help with a issue I'm trying to solve:

I have a carousel with multiple visible elements and wanted to get the current visible elements. in the flickity-viewport.

I can achieve this using groupCells and using the .is-selected class attached to the element in the group.

the problem is that I need the slider to move by one element at a time, and not, for example, in steps of 3 elements using the groupCells option.

Test case: https://codepen.io/anon/pen/oGLjqy

is there a way to do it in a simple way using your plugin's tools or have I to integrate it with something else?

@yaodingyd
Copy link

@yesh you mean something like this?

@yesh
Copy link
Author

yesh commented Sep 20, 2017

@yaodingyd this could be a good solution, also for the performance part.
but it's a lil' bit hardcoded (I have multiple carousels in a page with different numbers of items visible), and it does not work with wrapAround.
thank you, however, for giving me a possible solution if nothing comes out!

@yaodingyd
Copy link

@yesh No problem. You can check out this demo too (dynamically add class previous and next to the is-selected slide) and change the numbers of is-previous and is-next based on carousel cell's width.

@desandro
Copy link
Member

Thanks for opening this feature request. Currently, there is no easy API to get which cells are visible within the viewport. It would require measuring the cells around the selected cell. Definitely doable, but there may be a simpler solution, similar to @yaodingyd's technique of hard-coding how many cells should be able to fit.

Add a 👍 reaction to this issue if you would like to see this feature or code added. Do not add +1 comments — They will be deleted.

@desandro desandro changed the title support/feature request: get elements currently visible without using "groupCells" Get elements currently in viewport without groupCells Mar 22, 2018
@AanchalKapoor15
Copy link

Hi @desandro . I have a similar requirement. I'm trying to use flickity with my Angular 5 app. I need to get the cells currently in viewport so that I can perform an action on them. For that I too grouped the cells together and got the is-selected class. Now what is not desirable is that the page dots are mapped to the cell groups instead of being mapped to individual cells.
What I need is:

  1. Page dots mapped to individual cells
  2. Typescript to be informed of which cells are currently in viewport.

Can you please help?

@desandro
Copy link
Member

@AanchalKapoor15 for Page dots mapped to individual cells, you can create your own custom navigation UI, except use selectCell to select cells instead of slide groups. As for your second request, that's a bigger feature I'm still debating building out.

@desandro desandro changed the title Get elements currently in viewport without groupCells Get visible elements currently in viewport without groupCells Jun 27, 2018
@vadim-on-github
Copy link

vadim-on-github commented Jul 9, 2018

even with groupCells: true it cannot be achieved relying on .is-selected if contain: true and we're on the last slide without a full amount of cells in that slide.

This is a much needed API property :)
Thanks for the good work!

@pooriamo
Copy link

I created a pull request (#1032) which can handle this.

@gavin310
Copy link

Here's some jQuery I came up with to add an is-visible class to slides that are currently within the viewport (full or partially). Maybe it'll help someone:

$( '.slider' ).flickity( {
	cellAlign: 'left',
	prevNextButtons: false,
	contain: true,
	groupCells: true,
	on: {
		scroll: function() {
			var flkty = $( this.element ),
				slides = $( '.slider-slide', flkty );

			var viewport = $( '.flickity-viewport', flkty ),
				viewportBoundsLeft = viewport.offset().left,
				viewportBoundsRight = viewportBoundsLeft + viewport.width();

			slides.each( function() {
				var slide = $( this ),
					slideWidth = slide.width(),
					slideBoundsLeft = slide.offset().left;

				var insideViewportLeft = ( slideBoundsLeft + slideWidth > viewportBoundsLeft ),
					insideViewportRight = ( slideBoundsLeft < viewportBoundsRight );

				slide.toggleClass( 'is-visible', insideViewportLeft && insideViewportRight );
			} );
		},
	},
} );

@stifboy
Copy link

stifboy commented Aug 18, 2022

Here's some jQuery I came up with to add an is-visible class to slides that are currently within the viewport (full or partially). Maybe it'll help someone:

$( '.slider' ).flickity( {
	cellAlign: 'left',
	prevNextButtons: false,
	contain: true,
	groupCells: true,
	on: {
		scroll: function() {
			var flkty = $( this.element ),
				slides = $( '.slider-slide', flkty );

			var viewport = $( '.flickity-viewport', flkty ),
				viewportBoundsLeft = viewport.offset().left,
				viewportBoundsRight = viewportBoundsLeft + viewport.width();

			slides.each( function() {
				var slide = $( this ),
					slideWidth = slide.width(),
					slideBoundsLeft = slide.offset().left;

				var insideViewportLeft = ( slideBoundsLeft + slideWidth > viewportBoundsLeft ),
					insideViewportRight = ( slideBoundsLeft < viewportBoundsRight );

				slide.toggleClass( 'is-visible', insideViewportLeft && insideViewportRight );
			} );
		},
	},
} );

Is it possible to do the same to only the slide at the center?

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

No branches or pull requests

8 participants