jQuery封装animate.css

animate.css是一个有趣的,跨浏览器的css3动画库。

一、首先引入animate css文件

<link rel="stylesheet" href="animate.css">

二、给指定的元素加上指定的动画样式名

<div id="box" class="animated bounce"></div>

这里包括两个class名,第一个是基本的,必须添加的样式名,任何想实现的元素都得添加这个。第二个是指定的动画样式名。


三、如果说想给某个元素动态添加动画样式,可以通过jquery来实现

给动画对象添加类,然后监听动画结束事件,一旦监听到动画结束,立即移除前面添加的类。

官方给出了jQuery的封装:

//扩展$对象,添加方法animateCss
    $.fn.extend({
        animateCss: function (animationName) {
            var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
            $(this).addClass('animated ' + animationName).one(animationEnd, function() {
                $(this).removeClass('animated ' + animationName);
            });
    }
});

//调用示例:
$('#box').animateCss('bounce');
posted @ 2022-07-20 18:16  猫老板的豆  阅读(125)  评论(0编辑  收藏  举报