user10631553
user10631553

Reputation:

The slider is not displayed well on mobile devices

I just implemented a slider with the swiper.js plugin, and everything works correctly. The only problem I have is that the provision of the slider on mobile devices (specifically using Chrome, because in Safari there are no problems) is broken. I think it comes more for the CSS side, but I'm not sure.

I leave you an image of what happens and the code about it.

That's how it looks on mobiles with Chrome:

mobile on Chrome

And so it should be seen:

expected result

HTML: (I only put the code of an image without the added texts, since they do not do to the problem)

  <!-- Swiper -->
  <div class="swiper-container">
    <div class="swiper-wrapper">
       <?php
                $url= Ruta::ctrShowURL();
                $slide = SlideController::ctrShowSlide();
                    echo '<div class="swiper-slide">
                        <img class="img-responsive" src="'.$url.$slide["img"].'">
                         </div>';
                }
            ?>      

    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </div>

CSS:

    .swiper-slide {
      width: auto;
      height: auto;

    }

    .swiper-container{
        position:relative;
        margin:auto;
        width:100%;
        overflow: hidden;
        padding:0;}

/*=============================================
Mobile
=============================================*/

@media (max-width:768px){

    .swiper-container img{
      width: 100%;
      height: 160px;
      object-fit: cover;
    }

}

JS

<!-- Initialize Swiper -->

var swiper = new Swiper('.swiper-container', {
            speed: 700,
  spaceBetween: 30,
  centeredSlides: true,
  autoplay: {
    delay: 4000,
    disableOnInteraction: false,
  },
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
});

Upvotes: 0

Views: 2750

Answers (1)

Rahul
Rahul

Reputation: 4364

you have to provide some height to your swiper-container like

.swiper-container{
   height: 300px;
}

you can even put this code on media query for mobile devices if you want

as by default it is taking 100% height thats the reason of your issue

enter image description here

Upvotes: 1

Related Questions