常见使用

Swiper Animate使用方法

  1. 使用Swiper Animate需要先加载swiper.animate.min.jsanimate.min.css
<!DOCTYPE html>
<html>
<head>
    ...
    <link rel="stylesheet" href="path/to/swiper.min.css">
    <link rel="stylesheet" href="path/to/animate.min.css">
</head>
<body>
    ...
    <script src="path/to/swiper.min.js"></script>
    <script src="path/to/swiper.animate.min.js"></script>
</body>
</html>

官方文件中方法没有导出无法引入,下面是导出后的文件:

export function swiperAnimateCache() {
    const allBoxes = window.document.documentElement.querySelectorAll('.ani')
    for (var i = 0; i < allBoxes.length; i++) {
      allBoxes[i].attributes['style']
        ? allBoxes[i].setAttribute('swiper-animate-style-cache', allBoxes[i].attributes['style'].value)
        : allBoxes[i].setAttribute('swiper-animate-style-cache', ' ')
      allBoxes[i].style.visibility = 'hidden'
    }
  }
  
  export function swiperAnimate(a) {
    clearSwiperAnimate()
    var b = a.slides[a.activeIndex].querySelectorAll('.ani')
    for (var i = 0; i < b.length; i++) {
      b[i].style.visibility = 'visible'
      const effect = b[i].attributes['swiper-animate-effect']
        ? b[i].attributes['swiper-animate-effect'].value
        : ''
      b[i].className = b[i].className + ' ' + effect + ' ' + 'animated'
      const duration = b[i].attributes['swiper-animate-duration']
        ? b[i].attributes['swiper-animate-duration'].value
        : ''
      // duration && style
      const delay = b[i].attributes['swiper-animate-delay']
        ? b[i].attributes['swiper-animate-delay'].value
        : ''
      const style = b[i].attributes['style'].value + 'animation-duration:' + duration + ';-webkit-animation-duration:' + duration + ';' + 'animation-delay:' + delay + ';-webkit-animation-delay:' + delay + ';'
      // delay && (style = style )
      b[i].setAttribute('style', style)
    }
  }
  
  export function clearSwiperAnimate() {
    var allBoxes = window.document.documentElement.querySelectorAll('.ani')
    for (var i = 0; i < allBoxes.length; i++) {
      allBoxes[i].attributes['swiper-animate-style-cache'] && allBoxes[i].setAttribute('style', allBoxes[i].attributes['swiper-animate-style-cache'].value)
      allBoxes[i].style.visibility = 'hidden'
      allBoxes[i].className = allBoxes[i].className.replace('animated', ' ')
      const effectValue = allBoxes[i].attributes['swiper-animate-effect'].value
      /* eslint-disable-next-line */
      allBoxes[i].attributes['swiper-animate-effect'] && (effectValue, allBoxes[i].className = allBoxes[i].className.replace(effectValue, ' '))
    }
  }

扩展阅读更多:

  1. 初始化时隐藏元素并在需要的时刻开始动画。
<script> 
//Swiper5
  var mySwiper = new Swiper ('.swiper-container', {
    on:{
      init: function(){
        swiperAnimateCache(this); //隐藏动画元素 
        swiperAnimate(this); //初始化完成开始动画
      }, 
      slideChangeTransitionEnd: function(){ 
        swiperAnimate(this); //每个slide切换结束时也运行当前slide动画
        //this.slides.eq(this.activeIndex).find('.ani').removeClass('ani'); 动画只展现一次,去除ani类名
      } 
    }
  }) 
  </script>
  1. 在需要运动的元素上面增加类名  ani   ,和其他的类似插件相同,Swiper Animate需要指定几个参数:

    • swiper-animate-effect:切换效果,例如 fadeInUp 
    • swiper-animate-duration:可选,动画持续时间(单位秒),例如 0.5s
    • swiper-animate-delay:可选,动画延迟时间(单位秒),例如 0.3s
<div class="swiper-slide">
<p class="ani" swiper-animate-effect="fadeInUp" swiper-animate-duration="0.5s" swiper-animate-delay="0.3s">内容</p>
</div>

常见问题

Swiper 初始化不成功

注意❗

需要DOM结构都准备好了,才使用 new Swiper 进行创建,否则获取不到实例。

$(document).ready(() => {
  this.initSwiper();
});
 
initSwiper() {
	this.swiper = new Swiper(`#${this.id}`, {
	  direction: 'vertical', // 垂直切换选项
	  slidesPerView: 'auto', //footer页自适应高度
	  observer: true,
	  observerParents: true,
	  mousewheel: true,
	  ...this.options
	})
}

强制宽度,隐藏时初始化

解决初始化之后,滚动无法生效问题,需要确定DOM初始化好后或高度固定之后再创建 Swiper ,否则会出现问题。

 $(document).ready(() =>{
	this.footer.init();
	this.mainSwiper.initSwiper(true);
	this.contentSwiper.initSwiper();
  })

效果参考

扩展阅读