animite.css学习教程

animite.css学习教程

参考https://github.com/daneden/animate.css
最近发现animite.css做动画比较省力,本着不重复做轮子的想法。学习了一下,简单快捷易上手。

下面通过demo直接演示。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>animate学习demo</title>
    <link rel="stylesheet" href="animate.css" type="text/css">
    <style>
        /*demo2*/
        .yourElement {
            /*动画持续时间*/
            animation-duration: 5s;
            /*页面渲染好后,动画开始的时间,即等待5s开始动画*/
            animation-delay: 2s;
            /*动画播放次数*/
            animation-iteration-count: infinite;
        }
    </style>
</head>
<body>
<!--demo1-->
    <h1  class="animated infinite bounce delay-2s">这是第一种使用方式</h1>
<!--demo2-->
    <h1  class="animated bounce yourElement">这还是第一种使用方式,但修改了默认属性</h1>
<!--demo3-->
    <h1 class="demo3">这是第二种使用方式,js引入</h1>
    <script>
        // 选取第一个元素,添加样式
        const demo3 =  document.querySelector('.demo3')
        demo3.classList.add('animated', 'bounceOutLeft','infinite')
    </script>
<!--demo4-->
    <h1 class="demo4">还可以设置监听函数,在动画结束后,执行回调函数</h1>
    <script>
        const demo4 =  document.querySelector('.demo4')
        demo4.classList.add('animated', 'bounceOutRight')
        //设置监听,执行回调函数
        demo4.addEventListener('animationend', function() { console.log('demo4动画结束,回调了!') })
    </script>
<!--demo5-->
    <h1 class="demo5">还可以设置js函数,用来添加和删除animations</h1>
    <script>
        // 自定义函数
        //element为要选择的元素,animationName为要添加的动画名称,callback为回调函数名
        function animateCSS(element, animationName, callback) {
            // 选择元素,并为元素添加动画
            const node = document.querySelector(element)
            node.classList.add('animated', animationName)

            // 这个函数用来移除动画样式,移除监听,然后执行回调函数
            function handleAnimationEnd() {
                node.classList.remove('animated', animationName)
                node.removeEventListener('animationend', handleAnimationEnd)

                if (typeof callback === 'function') callback()
            }

            // 设置监听,动画结束后,执行handleAnimationEnd
            node.addEventListener('animationend', handleAnimationEnd)
        }


        // animateCSS('.demo5', 'bounce')
        // or
        animateCSS('.demo5', 'bounce', function() {
            // Do something after animation
            console.log('这是demo5')
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值