77 lines
1.9 KiB
HTML
77 lines
1.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>swiper</title>
|
|
<script src="./swiper.js"></script>
|
|
<link rel="stylesheet" href="./swiper.css">
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.swiper-slide img {
|
|
width: 100%;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="swiper-container">
|
|
<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>
|
|
|
|
<button>去第二张轮播图</button>
|
|
</body>
|
|
|
|
<script>
|
|
// ajax请求后端获得的轮播图的数据
|
|
var SwiperList = [
|
|
{ id: 1, title: '轮播1', img: 'https://st-cn.meishij.net/p2/20201022/20201022113526_224.jpg' },
|
|
{ id: 2, title: '轮播2', img: 'https://st-cn.meishij.net/p2/20201023/20201023101908_914.jpg' },
|
|
{ id: 3, title: '轮播3', img: 'https://st-cn.meishij.net/p2/20201019/20201019165100_268.jpg' },
|
|
];
|
|
|
|
// 将后端给的数据渲染到页面上
|
|
function RenderSlide() {
|
|
SwiperList.forEach((v)=>{
|
|
document.querySelector(".swiper-wrapper").innerHTML +=
|
|
`
|
|
<div class="swiper-slide">
|
|
<img src="${v.img}" alt="${v.title}">
|
|
</div>
|
|
`
|
|
})
|
|
}
|
|
|
|
RenderSlide()
|
|
|
|
|
|
|
|
var ddd = new swiper('.swiper-container', {
|
|
time: 3,
|
|
auto: false
|
|
})
|
|
|
|
// 调用 slideTo 方法,让轮播图切换到指定位置,参数是下标
|
|
document.querySelector("button").onclick = function(){
|
|
ddd.slideTo(2)
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
</html> |