静态的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="../lib/swiper.css" rel="stylesheet" />
<script src="../lib/swiper.js"></script>
<script src="../lib/vue.js"></script>
<style>
.swiper-container {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div class="swiper-container kerwin">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
<script>
new Swiper(".kerwin", {
loop: true,
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
},
})
</script>
</body>
</html>
动态swiper
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="../lib/swiper.css" rel="stylesheet" />
<script src="../lib/swiper.js"></script>
<script src="../lib/vue.js"></script>
<style>
.swiper-container {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div class="swiper-container kerwin">
<div class="swiper-wrapper">
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
<script>
setTimeout(()=>{
var list = ["qqq","vvvv","rrrr"]
var newList = list.map(item=>`<div class="swiper-slide">${item}</div>`)
console.log(newList)
var owrapper = document.querySelector(".swiper-wrapper")
owrapper.innerHTML = newList.join(' ')
})
new Swiper(".kerwin", {
loop: true,
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
},
})
</script>
</body>
</html>
swiper整合vue使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="../lib/swiper.css" rel="stylesheet" />
<script src="../lib/swiper.js"></script>
<script src="../lib/vue.js"></script>
<style>
.swiper-container {
width: 600px;
height: 300px;
}
.img-class {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div id="app">
<div class="swiper-container kerwin">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="item in dataList" :key="item">
<img :src="item" class="img-class" />
</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
</div>
<script>
new Vue({
el: "#app",
data: {
dataList: []
},
mounted() {
setTimeout(() => {
this.dataList = [
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F9vo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F86d6277f9e2f070859318101e024b899a901f2ff.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=aec5cfa4ab00f14ebc352c6c0cf74968",
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F06%2F20200706150844_KcVlG.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=91b49462bd22370160fd89c05306e566",
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F94o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F7a899e510fb30f24029b4702c195d143ad4b03a9.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=f03f024afb1ef595e5aa69217c4b5efc"
]
}, 2000)
},
updated() {
console.log(document.querySelectorAll(".swiper-slide").length)
new Swiper(".kerwin", {
loop: true,
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
},
})
}
})
</script>
</body>
</html>
swiper组件使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="../lib/swiper.css" rel="stylesheet" />
<script src="../lib/swiper.js"></script>
<script src="../lib/vue.js"></script>
<style>
.swiper-container {
width: 600px;
height: 300px;
}
.img-class {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div id="app">
<swiper :key="dataList.length" :loop="false">
<swiper-item v-for="data in dataList" :key="data">
<img :src="data" class="img-class" />
</swiper-item>
</swiper>
</div>
<script>
Vue.component("swiperItem",{
template:`<div class="swiper-slide">\
<slot></slot>
</div>`
})
Vue.component("swiper", {
props:{
loop:{
type:Boolean,
default:true
}
},
template:`<div class="swiper-container kerwin">
<div class="swiper-wrapper">
<slot></slot>
</div>
<!-- 如果需要分页器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要导航按钮 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- 如果需要滚动条-->
<div class="swiper-scrollbar"></div>
</div>`,
mounted(){
console.log(this.loop)
new Swiper(".kerwin", {
loop: this.loop,
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
},
})
}
})
new Vue({
el: "#app",
data: {
dataList: []
},
mounted() {
setTimeout(() => {
this.dataList = [
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F9vo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F86d6277f9e2f070859318101e024b899a901f2ff.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=aec5cfa4ab00f14ebc352c6c0cf74968",
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F06%2F20200706150844_KcVlG.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=91b49462bd22370160fd89c05306e566",
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F94o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F7a899e510fb30f24029b4702c195d143ad4b03a9.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649647326&t=f03f024afb1ef595e5aa69217c4b5efc"
]
}, 2000)
},
updated() {
console.log(document.querySelectorAll(".swiper-slide").length)
}
})
</script>
</body>
</html>
|