Неправильно работает Swiper slidesPerView="auto" в Vue

Рейтинг: 0Ответов: 2Опубликовано: 17.03.2023

Бьюсь уже несколько часов, но ничего не выходит. Мой слайдер неправильно рассчитывает ширину слайда. Искал решение и везде все решается установкой slidePerView="auto" и присвоение swiper-slide статичной ширины, но в моем случае так не сработало...

При загрузке страницы переопределяется стиль width:1045px :

devtool

Мой компонент:

<template>
   <swiper :observer="true"
     :slidesPerView="auto"
     :spaceBetween="40"
     :navigation="true"
     :loop="true"
     class="product__info-slider product__info-slider_privilege"
   >
      <swiper-slide class="product__info-slider-item">
        <div class="info-slider-item__container">
          <div class="info-slider-item__title">123</div>
          <div class="info-slider-item__text">321</div>
        </div>
      </swiper-slide>
      <swiper-slide class="product__info-slider-item">
        <div class="info-slider-item__container">
          <div class="info-slider-item__title">123</div>
          <div class="info-slider-item__text">321</div>
        </div>
      </swiper-slide>
      <swiper-slide class="product__info-slider-item">
        <div class="info-slider-item__container">
          <div class="info-slider-item__title">123</div>
          <div class="info-slider-item__text">321</div>
        </div>
     </swiper-slide>
     <swiper-slide class="product__info-slider-item">
       <div class="info-slider-item__container">
         <div class="info-slider-item__title">123</div>
         <div class="info-slider-item__text">321</div>
       </div>
     </swiper-slide>
   </swiper>
</template>

Стили:

.product__info-slider_privilege  {
    margin-top: 10px;
    width: calc(100% - 100px);
    transition: 0.7s;
    margin-left: unset;
    margin-right: unset;
}
    
.product__info-slider_privilege .slick-list.draggable:after{
    content: "";
    height: 100px;
    width: 80px;
    background: linear-gradient(270.4deg, #222222 29.26%, rgba(34, 34, 34, 0.84845) 45.73%, rgba(34, 34, 34, 0) 135.15%);
    position: absolute;
    right: 0;
    top: 0;
    transition: 0.3s;
}

.product__info-slider_privilege .product__info-slider-item{
    position: relative;
}

.product__info-slider_privilege .swiper-slide {
    width: 100px;
}

.product__info-slider_privilege .info-slider-item__title {
    position: absolute;
    top: 0;
    left: 15px;
    transform: translateY(-50%);
    background: #222222;
    color: rgba(255, 255, 255, 0.5);
    font-family: "TT Norms Bold";
    font-weight: 700;
    font-size: 14px;
    line-height: 17px;
    letter-spacing: 0.07em;
    padding: 0 5px;
    width: max-content;
}

.product__info-slider_privilege .info-slider-item__container {
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    padding: 22px 24px;
    position: relative;
    margin-top: 5px;
    width: max-content;

}

.product__info-slider_privilege .info-slider-item__text {
    font-family: "TT Norms Bold";
    font-weight: 700;
    font-size: 18px;
    line-height: 21px;
    display: flex;
    align-items: center;
    text-align: center;
    letter-spacing: 0.07em;
    color: #FFFFFF;
    width: max-content;
}

Ответы

▲ 0

Надеюсь, я смогу помочь кому-то, потому что я смог найти решение! Я обратился к официальным демо-слайдерам и заметил одно важное отличие.

Не правильно:

:slidesPerView="auto"

Правильно:

:slidesPerView="'auto'"

Все, что нужно было сделать - обернуть 'auto' в кавычки, чтоб передать строку!

▲ 0

Двоеточие впереди - это биндинг на переменную. Вместо:

:slidesPerView="auto"

Нужно:

slidesPerView="auto"