下面用循序渐进的方式,告诉你如何用 jQuery 插件做动画,从“直接用 jQuery 自带动画”到“使用第三方动画插件”。
jQuery 本身就支持动画,不需要插件也能做。
$("#box").hide(500);
$("#box").show(500);
$("#box").toggle(500);
$("#box").slideDown(500);
$("#box").slideUp(500);
$("#box").slideToggle(500);
$("#box").fadeIn(500);
$("#box").fadeOut(500);
$("#box").animate({
width: "300px",
opacity: 0.5,
marginLeft: "100px"
}, 1000);
适合:复杂动画、缓动、拖拽
引入:
<link rel="stylesheet" href="jquery-ui.css">
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
使用:
$("#box").effect("shake", { times: 3 }, 500);
$("#box").animate({
width: 300
}, {
duration: 1000,
easing: "easeOutBounce"
});
适合:简单炫酷动画
引入:
<link rel="stylesheet" href="animate.min.css">
使用:
$("#box")
.addClass("animate__animated animate__bounce")
.one("animationend", function () {
$(this).removeClass("animate__bounce");
});
适合:大量动画、性能好
引入:
<script src="jquery.js"></script>
<script src="velocity.min.js"></script>
使用:
$("#box").velocity({
translateX: 200,
opacity: 0.5
}, {
duration: 800,
easing: "easeOutQuad"
});
适合:页面滚动出现动画
<script src="wow.min.js"></script>
<link rel="stylesheet" href="animate.min.css">
new WOW().init();
<div class="wow fadeInUp">内容</div>
(function ($) {
$.fn.myFade = function (speed) {
return this.each(function () {
$(this).animate({ opacity: 0.2 }, speed);
});
};
})(jQuery);
使用:
$("#box").myFade(1000);
| 需求 | 推荐 |
|---|---|
| 简单动画 | jQuery 原生 |
| UI 交互 | jQuery UI |
| 炫酷效果 | Animate.css |
| 性能要求高 | Velocity |
| 滚动动画 | WOW.js |
如果你愿意,可以告诉我:
我可以直接给你可运行的完整示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。