Skip to content

Instantly share code, notes, and snippets.

@Elshaden
Last active December 18, 2023 05:36
Show Gist options
  • Save Elshaden/1288d56fb344cbfc89b50d99be0bd33c to your computer and use it in GitHub Desktop.
Save Elshaden/1288d56fb344cbfc89b50d99be0bd33c to your computer and use it in GitHub Desktop.
Flickity Laravel Livewire Implementation Example
@push('css')
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<style>
/* external css: flickity.css */
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.carousel {
background: transparent;
}
.carousel-cell {
width: 350px;
height: 200px;
/* flex-box, center image in cell */
display: -webkit-box;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-items: center;
align-items: center;
}
.carousel-cell img {
display: block;
max-width: 100%;
max-height: 100%;
/* dim unselected */
opacity: 0.7;
-webkit-transform: scale(0.85);
transform: scale(0.85);
-webkit-filter: blur(5px);
filter: blur(5px);
-webkit-transition: opacity 0.3s, -webkit-transform 0.3s, transform 0.3s, -webkit-filter 0.3s, filter 0.3s;
transition: opacity 0.3s, transform 0.3s, filter 0.3s;
}
/* brighten selected image */
.carousel-cell.is-selected img {
opacity: 1;
-webkit-transform: scale(1);
transform: scale(1);
-webkit-filter: none;
filter: none;
}
@media screen and ( min-width: 500px ) {
.carousel-cell {
height: 370px;
}
}
@media screen and ( min-width: 960px ) {
.carousel-cell {
width: 60%;
}
}
/* buttons, no circle */
.flickity-prev-next-button {
width: 60px;
height: 60px;
background: transparent;
opacity: 0.6;
}
.flickity-prev-next-button:hover {
background: transparent;
opacity: 1;
}
/* arrow color */
.flickity-prev-next-button .arrow {
fill: white;
}
.flickity-prev-next-button.no-svg {
color: white;
}
/* closer to edge */
.flickity-prev-next-button.previous {
left: 0;
}
.flickity-prev-next-button.next {
right: 0;
}
/* hide disabled button */
.flickity-prev-next-button:disabled {
display: none;
}
</style>
@endpush
<div class="carousel js-flickity " wire:ignore>
@foreach($Items as $item)
<div class="carousel-cell">
<x-component-anything >
:attribute="$variable "
background="https://i.imgur.com/kGkSg1v.png"
>
$item['content']
</x-component-anything>
</div>
@endforeach
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
<script type="text/javascript">
const flkty = new Flickity( '.carousel',{
// options
});
flkty.on( 'change', function( index ) {
livewire.emit('listener', index)
});
</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment