ALI JAMALI
ALI JAMALI

Reputation: 50

Install swiper slider on Laravel project

how can I install the swiper slider package on my Laravel 9 project?

Package link : swiperjs.com

Upvotes: 3

Views: 11037

Answers (3)

Tirso Lecointere
Tirso Lecointere

Reputation: 76

Using Laravel 9 with Vite and Tailwind. Follow these steps to use Swiper with all its modules:

  1. Install Swiper

    npm install swiper

  2. Add Swiper styles to your app.css

    @import 'swiper/css/bundle';

    Note: If you are using Tailwind you should import Swiper before the Tailwind assets.

  3. Add Swiper to your app.js

    import Swiper from 'swiper/bundle';

Upvotes: 5

Hooman Limouee
Hooman Limouee

Reputation: 1423

Well I guess this is the only answer on the internet that properly installs swiperjs in laravel (v9.x) and works.

Caution : According to the comment and provided link by @ktscript, the following approach is a bad practice which is recommended not to use by swiper official website.

But still I won't delete my answer in case you don't have any other options on the table.

If you've found it useless just let me know in the comments so I'll delete my answer.

Install swiperjs :

npm install swiper

Then inside the file resources/sass/app.scss :

@import 'node_modules/swiper/swiper';

Notice: You can't import the styles with a ~ at the beginning.

And inside the file /resources/js/app.js :

const Swiper = require('swiper').default;

The .default is necessary after require().

UPDATE : Now I found out that I can import swiper like below.

import {Swiper, Navigation, Pagination} from 'swiper';

Tested with Laravel v9.x and swiperjs v8.x.

Upvotes: 2

ktscript
ktscript

Reputation: 413

The correct version of integration into Laravel 8/9 using webpack that does not contradict the swiper documentation may look like this:

Install swiperjs :

npm install swiper

Then inside the file resources/sass/app.scss :

@import 'node_modules/swiper/swiper';

if you need navigation/pagination or another modules add lines:

@import 'node_modules/swiper/modules/navigation/navigation';
@import 'node_modules/swiper/modules/pagination/pagination';

And inside the file /resources/js/app.js :

import { Autoplay, Navigation, Pagination } from "swiper";
import Swiper from "swiper";
Swiper.use([Autoplay, Navigation, Pagination]);

(in the example above, I added Autoplay property). And after then where to add the code (for example in blades):

<div class="swiper mySwiperClass">
    <div class="swiper-wrapper">
        <!-- Slides -->
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        ...
    </div>
    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>
    <!-- If we need controls 
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    -->
</div>

<script>
    window.onload = function(){
        const swiper = new Swiper('.mySwiperClass', {
            spaceBetween: 15,
            slidesPerView: 3, 
            pagination: {
              el: ".swiper-pagination",
              clickable: true
            },
            speed: 1000,
            loop: true,
            autoplay: {
              delay: 2500,
              disableOnInteraction: false,
            }
        }
    }
</script>

Upvotes: 3

Related Questions