1、cdn引入
DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Swiper demotitle>
<meta
name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"
/>
<link rel="stylesheet" href="https://unpkg.com/swiper@8/swiper-bundle.css">
<script src="https://unpkg.com/swiper@8/swiper-bundle.js"> script>
<style>
html,
body {
position: relative;
margin: 0;
padding: 0;
}
body {
background: #eee;
}
.swiper {
width: 100%;
height: 300px;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
}
.swiper-slide {
width: 60%;
}
style>
head>
<body>
<div class="swiper mySwiper">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1div>
<div class="swiper-slide">Slide 2div>
<div class="swiper-slide">Slide 3div>
<div class="swiper-slide">Slide 4div>
<div class="swiper-slide">Slide 5div>
<div class="swiper-slide">Slide 6div>
<div class="swiper-slide">Slide 7div>
<div class="swiper-slide">Slide 8div>
<div class="swiper-slide">Slide 9div>
div>
<div class="swiper-pagination">div>
div>
<script>
var swiper = new Swiper(".mySwiper", {
slidesPerView: "auto",
centeredSlides: true,
spaceBetween: 30,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
});
script>
body>
html>
2、vue2版本
7以上版本不支持vue2
npm install swiper@5.4.5
<template>
<div class="box-wrap">
<div class="swiper mySwiper">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item,index) in swipeList" :key="index" :style="{background:item.bg}">
{{item.title}}
div>
div>
<div class="swiper-pagination">div>
div>
div>
template>
<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css';
export default {
data(){
return{
swipeList:[
{
id:1,
title:11111,
bg:'#cfc'
},
{
id:2,
title:2222,
bg:'#f00',
},
{
id:3,
title:3333,
bg:'#cfc'
},
{
id:4,
title:4444,
bg:'#ffc'
},
],
}
},
mounted(){
setTimeout(()=>{
this.initSwiper()
})
},
methods:{
initSwiper(){
var swiper = new Swiper(".mySwiper", {
slidesPerView: "auto",
centeredSlides: true,
spaceBetween: 30,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
});
},
}
}
script>
<style>
.box-wrap{
position: relative;
height: 100vh;
background: #eee;
}
.swiper {
width: 100%;
height: 300px;
}
.swiper-slide {
width: 60%;
height: 300px;
}
style>
3、vue3版本
版本8+
npm install swiper
文档地址https://swiperjs.com/vue
<template>
<div class="box-wrap">
<swiper
:modules="modules"
slidesPerView="auto"
centeredSlides
:space-between="30"
:pagination="{ el:'.swiper-pagination',clickable: true }"
@swiper="onSwiper"
@slideChange="onSlideChange"
class="swiper"
>
<swiper-slide class="swiper-slide" v-for="(item,index) in swipeList" :key="index" :style="{background:item.bg}">
{{item.title}}
swiper-slide>
...
swiper>
<div class="swiper-pagination aaa111">div>
div>
template>
<script>
// Import Swiper Vue.js components
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
import 'swiper/css';
import 'swiper/css/pagination';
export default {
components: {
Swiper,
SwiperSlide,
},
setup() {
const onSwiper = (swiper) => {
console.log(swiper);
};
const onSlideChange = () => {
console.log('slide change');
};
return {
onSwiper,
onSlideChange,
modules: [Pagination],
swipeList:[
{
id:1,
title:11111,
bg:'#cfc'
},
{
id:2,
title:2222,
bg:'#f00',
},
{
id:3,
title:3333,
bg:'#cfc'
},
{
id:4,
title:4444,
bg:'#ffc'
},
],
};
},
};
script>
<style scoped>
.box-wrap{
position: relative;
background: #eee;
width: 80vw;
height: 80vh;
}
.swiper{
height: 300px;
}
.swiper-slide{
width: 60vw;
}
style>
重点
//css
.swiper-slide {
width: 60%;
}
//js
<script>
var swiper = new Swiper(".mySwiper", {
slidesPerView: "auto",
centeredSlides: true,
spaceBetween: 30,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
});
script>