swiper里面几个有用的参数

概述

这是我本身用swiper和看别人官网源码用swiper总结出来的,供之后开发时参考,相信对其余人也有用。javascript

observeParents

有时咱们会改变swiper的父级元素,好比页面的resize方法;也有时咱们会动态给swiper添加一些子元素。这个时候,须要以下设置swiper,才能正常运行:java

<script language="javascript">
var mySwiper = new Swiper('.swiper-container',{
pagination : '.swiper-pagination',
observer:true,
observeParents:true,
})
</script>

paginationBulletRender回调函数

这个回调函数用于彻底自定义分页器的指示点,接受指示点索引和指示点类名做为参数。经常使用于分页器里面要填充一些文字内容的情形。app

<script>
var swiper = new Swiper('.swiper-container', {
  pagination: '.swiper-pagination',
  paginationClickable: true,
  paginationBulletRender: function (swiper, index, className) {
      return '<span class="' + className + '">' + (index + 1) + '</span>';
  }
});  
</script>

lazyLoading懒加载

swiper有一个参数是preloadImages,它的默认值为true,默认会加载全部图片。若是想懒加载图片,就可使用lazyLoading参数开启图片的懒加载。ide

这个时候须要将图片img标签的src改写成data-src,而且增长类名swiper-lazy。背景图的延迟加载则增长属性data-background。而且给div增长一个swiper-lazy-preloader类。实例以下:函数

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">
            <img data-src="path/to/picture-1.jpg" class="swiper-lazy">
            <div class="swiper-lazy-preloader"></div>
        </div>
        <div class="swiper-slide">
            <img data-src="path/to/picture-2.jpg" class="swiper-lazy">
            <div class="swiper-lazy-preloader"></div>
        </div>
        <div class="swiper-slide">
            <div data-background="path/to/picture-3.jpg" class="swiper-lazy">slide3</div>
        </div>
    </div>
</div> 
<script> 
    var mySwiper = new Swiper('.swiper-container',{
    lazyLoading : true,
})
</script>

fade效果

swiper还支持各类切换效果,好比"fade"(淡入)"cube"(方块)"coverflow"(3d流)"flip"(3d翻转)。实例以下:spa

<script language="javascript"> 
var mySwiper = new Swiper('#swiper-container1',{
effect : 'fade',
})
var mySwiper2 = new Swiper('#swiper-container2',{
effect : 'cube',
})
var mySwiper3 = new Swiper('#swiper-container3',{
effect : 'coverflow',
slidesPerView: 3,
centeredSlides: true,
})
var mySwiper4 = new Swiper('#swiper-container4',{
effect : 'flip',
})
</script>

superslide

在PC端建议使用更加人性化的superslide插件,由于它支持hover切换插件

相关文章
相关标签/搜索