FairyTalesss 2023-03-24 17:00 采纳率: 78.6%
浏览 21
已结题

Swiper Js slider自适应显示不同数量slide问题

使用swiper js建立slider时,想要在桌面端同时展示3个slide而在移动端展示1个slide,应该如何自适应?

Html代码

<body>
    <div class="swiper">
        
        <div class="swiper-wrapper">
          
          <div class="swiper-slide">
            <img src="./1.jpg" alt="">
            <p>This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1.</p>
            <h2>Slide 1</h2>
          </div>
          <div class="swiper-slide">
            <img src="./2.jpg" alt="">
            <p>This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2.</p>
            <h2>Slide 2</h2>
          </div>
          <div class="swiper-slide">
            <img src="./3.jpg" alt="">
            <p>This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3.</p>
            <h2>Slide 3</h2>
          </div>
          <div class="swiper-slide">
            <img src="./1.jpg" alt="">
            <p>This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1. This is testimonial 1.</p>
            <h2>Slide 1</h2>
          </div>
          <div class="swiper-slide">
            <img src="./2.jpg" alt="">
            <p>This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2. This is testimonial 2.</p>
            <h2>Slide 2</h2>
          </div>
          <div class="swiper-slide">
            <img src="./3.jpg" alt="">
            <p>This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3. This is testimonial 3.</p>
            <h2>Slide 3</h2>
          </div>
        </div>
        
        <div class="swiper-pagination"></div>
      
        
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
      
        
        
    </div>
</body>

Css代码

<style>
        .swiper-wrapper{
            width: 1500px;
        }

        .swiper-wrapper .swiper-slide{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background-color: #ededed;
            padding: 30px 0;
            border-radius: 15px;
        }

        .swiper-wrapper .swiper-slide img{
            width: 50px;
            height: auto;
            border-radius: 50%;
        }

        .swiper-wrapper .swiper-slide p{
            width: 80%;
            text-align: center;
        }

    </style>

Js代码:

<script>
    const swiper = new Swiper('.swiper', {
  // Optional parameters
        slidesPerView:3,
        spaceBetween : 30,
    

  // If we need pagination
  pagination: {
    el: '.swiper-pagination',
  },

  // Navigation arrows
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // And if we need scrollbar
  scrollbar: {
    el: '.swiper-scrollbar',
  },
});
</script>

  • 写回答

2条回答 默认 最新

  • 简效 2023-03-24 17:03
    关注

    swiper可以设置不同的屏幕下展示不同的

        breakpoints: { 
          320: {  //当屏幕宽度大于等于320
            slidesPerView: 2,
            spaceBetween: 10
          },
          768: {  //当屏幕宽度大于等于768 
            slidesPerView: 3,
            spaceBetween: 20
          },
          1280: {  //当屏幕宽度大于等于1280
            slidesPerView: 4,
            spaceBetween: 30
          }
        }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月1日
  • 已采纳回答 3月24日
  • 创建了问题 3月24日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分