简体   繁体   English

如何在 Vue Js 中使用 swiper 断点

[英]How to use swiper breakpoints in Vue Js

Good morning, I'm making a site using VueJS for the frontend, I have installed SwiperJS via npm for Vue to make sliders.早上好,我正在制作一个使用 VueJS 作为前端的网站,我已经通过 npm 安装了 SwiperJS 用于 Vue 制作滑块。 It works fine but i'm not able to make functioning breakpoints to have a responsive number of slides.它工作正常,但我无法设置功能断点来获得响应数量的幻灯片。 I used this code to make a responsive post section that has all 4 posts showed in the PC view and only one at a time on the mobile view (and you can also scroll to the posts on mobile view )我使用此代码制作了一个响应式帖子部分,该部分在 PC 视图中显示了所有 4 个帖子,在移动视图中一次只显示一个(您也可以滚动到移动视图中的帖子)

This is my code:这是我的代码:

slider.vue slider.vue

<template>
  <swiper
    :slides-per-view="4"
    :space-between="20"

    >
    <swiper-slide v-for="posts in APIData" :key="posts.slug">
      <img class="card-img-top" v-bind:src=posts.image alt="Card image cap">
      <hgroup class="text">
        <router-link to="/single"> <h3>{{posts.title}}</h3> </router-link>
        <p>{{posts.desc.substring(0,80)+".." }}</p>
     </hgroup>
    </swiper-slide>
  </swiper>
</template>

<script>
  // Import Swiper Vue.js components
  import SwiperCore, { Navigation, Pagination, Autoplay } from 'swiper';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  // Import Swiper styles
  import 'swiper/swiper.scss';
  import 'swiper/components/pagination/pagination.scss';
  import 'swiper/components/navigation/navigation.scss';
  import { getAPI } from '../../api'
  // Swiper Plugins
  SwiperCore.use([Navigation, Pagination, Autoplay]);

  export default {
    data () {
      return { // Retourn the API Dates
        APIData: [],
        swiperOptions: {
          breakpoints: {       
      320: {       
         slidesPerView: 1,
         spaceBetween: 10     
      },          
      770: {       
         slidesPerView: 2,       
         spaceBetween: 50     
      },   
  
      771: {       
         slidesPerView: 4,       
         spaceBetween: 30     
      } 
   }   
        }
      }
    },

    components: {
      Swiper,
      SwiperSlide,
    },
    
    methods: {
      onSwiper(swiper) {
        console.log(swiper);
      },
      onSlideChange() {
        console.log('slide change');
      },
    },
  // Connect to API
   created () {
      getAPI.get('blog/api/list/last',)
        .then(response => {
          console.log('Post API has recieved data')
          this.APIData = response.data
        })
        .catch(err => {
          console.log(err)
        })
    },
  };
</script>

<style lang="sass" scoped>

.swiper-container
    background: linear-gradient(to top left, #BF092D, #8D0923)
    height: 80vh
    padding: 3rem
    .read-more
        padding: 20px
        color: #fff
        float: right
    .swiper-wrapper
        .swiper-slide
            background-color: #fff
            border-radius: 5px
            .text
                padding: 1rem
                h3
                    background: linear-gradient(to top left, #BF092D, #8D0923)
                    -webkit-background-clip: text
                    -webkit-text-fill-color: transparent
            img
                margin: auto
                display: block
                width: 100%
                height: 45% 
                border-radius: 5px 5px 0px 0px
</style>

You need to pass the options object to the Swiper like this:您需要将选项 object 传递给 Swiper,如下所示:

<swiper
    :breakpoints="swiperOptions.breakpoints"
>

Also for other properties: https://swiperjs.com/vue .也适用于其他属性: https://swiperjs.com/vue Vue component takes properties one at a time. Vue 组件一次获取一个属性。 This can also be checked using devtools:这也可以使用 devtools 进行检查: 在此处输入图像描述

Solution from mark is working but with using flexbox when manually resizing window (or after flipping mobile phone) it doesn't update.来自标记的解决方案有效,但在手动调整 window 的大小(或翻转手机后)时使用 flexbox 不会更新。 Just on first load, some bug of vue-swiper?只是在第一次加载时,vue-swiper 的一些错误?

I'm using:我在用着:

<swiper
    :breakpoints="swiperOptions.breakpoints">

But I also added if somebody need:但如果有人需要,我还补充说:

 <script setup>   
   ...swiper loadings...
    const swiperUpdate = () => {
        swiperInstance.value.update();
        console.log('swiperInstanceUpdate')
    };
    onMounted(() => {
        window.addEventListener('resize', swiperUpdate);
    })
    onBeforeUnmount(() => {
        window.removeEventListener('resize', swiperUpdate);
    })
 </script>

Just add this inside of "Swiper" element :breakpoints="{ 600:{ slidesPerView:3 }, 900:{ slidesPerView:4, } }"只需将其添加到“Swiper”元素中:breakpoints="{ 600:{ slidesPerView:3 }, 900:{ slidesPerView:4, } }"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM