Animate.css的安装与使用

npm安装Animate.css

npm install animate.css --save

在main.js中引入

import 'animate.css';

在项目中使用: 使用格式为"animate__animated  animate__xxxxxxxx",必须要写animate__animated,否则不展示动画。

<div>
    <a-image
        :width="150"
        class="animate__animated animate__fadeInLeft"
        :src="src"
        :preview="false"
    />
</div>

需求:在长页面中,滚动条滚动到相应位置时呈现出相应动画效果。

思路:设置一个变量值为false,用v-show判断这个变量的值来控制需要动画效果的模块是否显示,动画开始前该模块不显示,动画开始时模块才会显示

<!-- productOne值为false,动画开始前不展示该模块 -->
<div v-show="productOne" class="productOne" >     
    <a-image
        :width="100"
        :src="src"
        :preview="false"
    />
    <h1 class="productTitle">标题一</h1>
</div>

        js监听滚动条。当滚动条滚动到大于该位置时,触发该动画。为防止滚动到大于该位置后动画重复展示,所以还要设置一个变量值为true,当滚动到大于该位置时,判断该变量的值为true时开始动画,然后设置该变量值为false,再向下滚动时,由于该变量的值为false,所以不能展示动画,避免了重复展示。当向上滚动到小于该位置时,设置该变量值为true,当第二次滚动到该位置下时,仍然能展示动画,同时,要设置该需要动画效果的模块不显示。

const animateCSS = (element, animation, time ,prefix = 'animate__') =>
    new Promise((resolve, reject) => {
        const animationName = `${prefix}${animation}`;
        const node = document.querySelector(element);
        node.style.setProperty('--animate-duration', time);
        node.classList.add(`${prefix}animated`, animationName);
        function handleAnimationEnd(event) {
            event.stopPropagation();
            node.classList.remove(`${prefix}animated`, animationName);
            resolve('Animation ended');
        }
        node.addEventListener('animationend', handleAnimationEnd, {once: true});
    });
window.addEventListener("scroll", ()=> {
    let currentScroll = 0
    currentScroll = window.pageYOffset
    // 这个高度你看着调
    if(currentScroll<345)
        //当页面小于345时,设置需要动画的模块不展示
        state.productOne=false
        //页面小于345时,设置值为true,下一次滚动到大于345时,仍然能触发动画效果
        state.productOnce =true
    }
    if (currentScroll>345){      
        //state.productOnce值为true,动画效果开始以后值为false,再向下滚动时,值虽然大于345,但是不触发动画,避免重复展示动画。
        if(state.productOnce){       
           // 第一个参数用于选取想要添加特效的dom,第二个参数是特效类名,第三个为特效持续时间
           animateCSS('.productOne','slideInUp','0.3s')
           state.productOne=true
           state.productOnce =false
        }
    } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值