第一步先引入swiper组件
<link rel="stylesheet" type="text/css" href="static/css/swiper-4.3.3.min.css"> <script type="text/javascript" src="static/js/swiper-4.3.3.min.js"></script> //以轮播图为例 <div class="swiper-container banner"> <div class="swiper-wrapper"> </div> <div class="swiper-pagination"></div> </div>
<script> //$.post进行请求 $.post("https://xiaozhangnongming/api.html",{method:"website.pcposter"}, function(res){ var swiperList=[] swiperList = res.data var sliderHtml="" //这里使用$.each遍历数据 $.each(swiperList,function(k,v){ //这里对数据进行拼接 sliderHtml+='<div class="swiper-slide">'+'<a href="javascript:void(0)">'+'<img src='+swiperList[k].img+'>'+'</a>'+'</div>' }) //这里通过append方法将数据添加到页面 $(".banner .swiper-wrapper").append(sliderHtml) //这里是swiper里面的方法最好写在这里请求成功的时候,否则会有滑不动的BUG var mySwiper1 = new Swiper('.banner', { loop: true, pagination: { el: '.swiper-pagination', }, autoplay: { disableOnInteraction: false, }, }) } ) </script>