Good morning, I’m making a site using VueJS for the frontend, I have installed SwiperJS via npm for Vue to make sliders. 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 )
This is my code:
slider.vue
JavaScript
x
109
109
1
<template>
2
<swiper
3
:slides-per-view="4"
4
:space-between="20"
5
6
>
7
<swiper-slide v-for="posts in APIData" :key="posts.slug">
8
<img class="card-img-top" v-bind:src=posts.image alt="Card image cap">
9
<hgroup class="text">
10
<router-link to="/single"> <h3>{{posts.title}}</h3> </router-link>
11
<p>{{posts.desc.substring(0,80)+".." }}</p>
12
</hgroup>
13
</swiper-slide>
14
</swiper>
15
</template>
16
17
<script>
18
// Import Swiper Vue.js components
19
import SwiperCore, { Navigation, Pagination, Autoplay } from 'swiper';
20
import { Swiper, SwiperSlide } from 'swiper/vue';
21
22
// Import Swiper styles
23
import 'swiper/swiper.scss';
24
import 'swiper/components/pagination/pagination.scss';
25
import 'swiper/components/navigation/navigation.scss';
26
import { getAPI } from '../../api'
27
// Swiper Plugins
28
SwiperCore.use([Navigation, Pagination, Autoplay]);
29
30
export default {
31
data () {
32
return { // Retourn the API Dates
33
APIData: [],
34
swiperOptions: {
35
breakpoints: {
36
320: {
37
slidesPerView: 1,
38
spaceBetween: 10
39
},
40
770: {
41
slidesPerView: 2,
42
spaceBetween: 50
43
},
44
45
771: {
46
slidesPerView: 4,
47
spaceBetween: 30
48
}
49
}
50
}
51
}
52
},
53
54
components: {
55
Swiper,
56
SwiperSlide,
57
},
58
59
methods: {
60
onSwiper(swiper) {
61
console.log(swiper);
62
},
63
onSlideChange() {
64
console.log('slide change');
65
},
66
},
67
// Connect to API
68
created () {
69
getAPI.get('blog/api/list/last',)
70
.then(response => {
71
console.log('Post API has recieved data')
72
this.APIData = response.data
73
})
74
.catch(err => {
75
console.log(err)
76
})
77
},
78
};
79
</script>
80
81
<style lang="sass" scoped>
82
83
.swiper-container
84
background: linear-gradient(to top left, #BF092D, #8D0923)
85
height: 80vh
86
padding: 3rem
87
.read-more
88
padding: 20px
89
color: #fff
90
float: right
91
.swiper-wrapper
92
.swiper-slide
93
background-color: #fff
94
border-radius: 5px
95
.text
96
padding: 1rem
97
h3
98
background: linear-gradient(to top left, #BF092D, #8D0923)
99
-webkit-background-clip: text
100
-webkit-text-fill-color: transparent
101
img
102
margin: auto
103
display: block
104
width: 100%
105
height: 45%
106
border-radius: 5px 5px 0px 0px
107
</style>
108
109
Advertisement
Answer
You need to pass the options object to the Swiper like this:
JavaScript
1
4
1
<swiper
2
:breakpoints="swiperOptions.breakpoints"
3
>
4
Also for other properties: https://swiperjs.com/vue.
Vue component takes properties one at a time. This can also be checked using devtools: