vueJS中wowjs、animate、swiper的使用

原文关注公众号
原文关注公众号

本文演示利用swiper纵向全屏滚动

npm 安装 wow.js,安装 wow.js后animate.css会自动安装;

npm install wowjs --save-dev

npm 安装 animate.css

animate.css文档:http://5kzx.cn/doc.html

npm install animate.css --save

安装 swiper
swiper文档:https://www.swiper.com.cn/usage/index.html

npm install vue-awesome-swiper --save

在main.js中加入

如果要在swiper使用动画需要引入 swiper.animate.min.js,在官网手动下载自行引入。

// 引入swiper
import VueAwesomeSwiper from 'vue-awesome-swiper';
import '@/assets/css/swiper.min.css';
// 引入wow
import "wowjs/css/libs/animate.css";
// 如果此处wowjs/css/libs/animate.css不生效,还需要单独再引入
import 'animate.css';
Vue.use(VueAwesomeSwiper);

源码:


<template>
  <div class="warp">
    <swiper :options="pullSwiperOptions" 
      @transitionStart="onSlideChangeStart"
      @transitionEnd="onSlideChangeEnd"
      @slideChange="onSlideChange"
      ref="fullSwiper">
      <swiper-slide>
        <h1 class="wow animate__animated animate__bounce" data-wow-delay="800ms" data-wow-duration="1.5s">
          <div>1:wowJS滚动动画</div> 
        </h1>
      </swiper-slide>
      <swiper-slide>
        <h1 class="wow animate__animated animate__flip" data-wow-delay="800ms" data-wow-duration="1.5s">
          <div>2:wowJS滚动动画</div> 
        </h1>
      </swiper-slide>
      <swiper-slide>
        <h1 class="wow animate__animated animate__bounceInLeft" data-wow-delay="800ms" data-wow-duration="1.5s">3:wowJS滚动动画
        </h1>
      </swiper-slide>
    </swiper>
  </div>
</template>
<script>
import { WOW } from "wowjs";
export default {
  name: "swiperHome",
  components: {},
  data() {
    return {
      // 全屏滚动
      pullSwiperOptions: {
        direction: "vertical",
        slidesPerView: 1,
        spaceBetween: 30,
        slidesPerView: 'auto',
        paginationClickable: true,
        keyboardControl: true,
        mousewheelControl: true,
        mousewheel: true,
        // effect : 'fade',
        // fadeEffect: {
        //   crossFade: true,
        // },
        speed: 1500,
        pagination: {
          el: ".pull-swiper-pagination",
          type: "bullets",
          clickable: true,
          // direction: 'vertical',
        },
        on: {
          slideChange: () => {
            const swiper = this.$refs.fullSwiper.swiper.activeIndex;
            const activeIndex = swiper.activeIndex;
            console.log('当前下标homeSwiperOption:', swiper);

          },
          transitionStart:function(){
            console.log("回调函数transitionStart:",this);

          },
          transitionEnd: (swiper) => {
            console.log("回调函数transitionEnd:",swiper);
          }
        },
      },
    };
  },
  mounted() {

  },
  methods: {
    wowInit(){
      this.$nextTick(() => {
        let wow=new WOW({
         // default
          boxClass: 'wow',
           // default
          animateClass: 'animated',
           /*
           如果html、body元素标签高度设置了100%, 这里的offset
           需要设置为负数,否则页面滚动到了可视区域内容会不显示
           */
          offset: -6000,
           // default
          mobile: true,
          live: false,

          // live为true时,控制台会提示:MutationObserver is not supported by your browser. & WOW.js cannot detect dom mutations, please call .sync() after loading new content.

          callback: function (box) {
            console.log("WOW: animating <" + box.tagName.toLowerCase() + ">")
          }
        });
        wow.init();
      });
    },

    onSlideChangeStart(swiper, event){
      console.log("onSlideChangeStart:",);
    },
    onSlideChangeEnd(swiper, event) {
      console.log("回调函数onSlideChangeEnd:",swiper, event);

    },
    onSlideChange(){
      console.log("回调函数onSlideChange:",);
      this.wowInit();
    },
  },
  destroyed() {

  },
};
</script>
<style>
  html,
  body {
    height: 100% !important;
  }
  #app{
    height: calc(100% - 10.4rem);
  }
  .warp,.swiper-container,.swiper-wrapper,.swiper-slide{
    width: 100%;
    height: 100%;

  }
  h1{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 24px;
  }
</style>

注意:

如果html、body元素标签高度设置了100%, offset需要设置为负数,否则页面滚动到了可视区域内容会不显示

data-wow-duration(动画持续时间)
data-wow-delay(动画延迟时间)
data-wow-offset(元素的位置露出后距离底部多少像素执行)
data-wow-iteration(动画执行次数)

Vue 2中使用Swiper v4版本并添加动画效果,你需要安装Swiper库以及 swiper-animate 插件。首先,确保通过npm或yarn安装基础Swiper包: ```bash npm install swiper@4 # 或者 yarn add swiper@4 ``` 然后,安装swiper-animate插件: ```bash npm install swiper-animate # 或者 yarn add swiper-animate ``` 接下来,在项目里引入 Swiperswiper-animate,并在组件中配置Swiper实例,设置animate选项开启动画: ```javascript import { Swiper, SwiperSlide } from 'swiper'; import 'swiper/animate'; export default { components: { Swiper, SwiperSlide }, data() { return { swiperOptions: { // 其他常规配置... animate: true, // 开启动画 // 如果需要特定滑动动画,可以使用 swiperAnimateOptions 代替 animate // swiperAnimateOptions: { // duration: 500, // 动画持续时间,单位ms // easing: 'easeInOutQuart', // 动画缓动函数 // }, } }; }, mounted() { this.swiper = new Swiper('.swiper-container', this.swiperOptions); }, beforeDestroy() { this.swiper.destroy(); // 销毁Swiper实例时关闭动画 } }; ``` 在HTML模板中创建Swiper容器: ```html <div class="swiper-container"> <div class="swiper-wrapper"> <!-- 滑块内容 --> <swiper-slide>Slide 1</swiper-slide> <swiper-slide>Slide 2</swiper-slide> <!-- 更多滑块... --> </div> </div> ``` 现在,Swiper会应用默认的滑动动画效果。你可以根据需要调整`animate`选项或其他动画相关配置来自定义动画。如果你有特殊的需求,如自定义动画路径,可能需要结合 swiper-animate 的 API 来编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值