jquery.easing.js缓动函数插件的用法

jQuery 提供了一些诸如 show、hide、slideUp、fadeIn 等等动画方法,可以方便的切换元素的显隐。更有强大的自定义动画方法 animate ,可以实现很多动画效果。为了让动画有好的过渡变化过程,官方为这些方法设置 easing 属性,但是官方没有给出很多过渡效果。

jquery.easing.js 这个插件,增加了很多过渡效果,引入之后可以让动画过渡过程更加多样化。

官方也有案例:打开官方实例

如何使用 jquery.easing.js

第一步 引入插件

jQuery 插件嘛,当然要先引入 jQuery,然后再引入 jquery.easing.js  。

<span style="font-family:Arial;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span style="white-space:pre">	</span><html xmlns="http://www.w3.org/1999/xhtml">
<span style="white-space:pre">	</span><head>
<span style="white-space:pre">	</span><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<span style="white-space:pre">	</span><title>无标题文档</title>
<span style="white-space:pre">	</span><script src="jquery 2.1.1.js" type="text/javascript"></script>
<span style="white-space:pre">	</span><script src="jquery.easing.1.3.mine.js" type="text/javascript"></script></span>

第二步 启用插件

首先先假设使用 animate 方法把网页上的 class 为 aa 的 div 的宽度,从原本的 300px 变成 600px。按照 animate 的写法,加上 easing 。

<span style="font-family:Arial;">$("#toggle2").click(function(e){
		$('#example2').animate(
			{height:200}, 
			{duration: 1000, easing: 'easeInOutCirc'}
		)
		.animate(
			{height:100}, 
			{duration: 1000, easing: 'easeOutBounce'}
		);</span>

这样,就对 toggle2对象,先启用了一个 easeInOutCirc 过渡效果,后启用了一个easeOutBounce的过渡效果,在 1000毫秒 内先变成200px,再变成100px。

插件的所有函数曲线图和效果见:http://easings.net/zh-cn

可以应用的动画方法

不仅仅支持 animate 方法,还支持 hide、show、slideDown、slideUp、fadeIn、fadeOut等等支持 easing 的动画方法。只需要按照官方对应的格式去写就可以,这个插件相当于扩充了官方的过渡效果样式

插件的参数

这个插件有三个参数:durationeasingcomplete

duration 参数

用来指定动画变化的时间,以毫秒为单位。

easing 参数

指定这个动画要使用何种过渡样式。具体的过渡样式名和效果,需要在http://easings.net/zh-cn这里,选择找到自己想要的效果。

complete 参数

设置一个回调函数,当动画完成之后,执行这个函数。

其他注意事项

使用 slideUp 动画方法

slideUp 这类的动画方法,要比 animate 简单一些,不需要复杂的属性参数,所以可以直接这样写:

<span style="font-family:Arial;">$(element).slideUp(1000, method, callback});
        $(element).slideUp({ duration: 1000, easing: method, complete: callback});</span>

其他的 hide 、show 之类的方法,自行类比。

指定默认的 easing 样式

在使用中 easing 参数是可以省略的,省略之后,就会调用默认的过渡样式。可以使用下面一句代码,指定默认的动画过渡样式。

jQuery.easing.def = "过渡样式名,例如 easeInOutCirc";

用起来挺简单的,但是有了更和谐的变化效果,可以增强用户体验。

下面是一个完整的应用案例:

<span style="font-family:Arial;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span style="white-space:pre">	</span><html xmlns="http://www.w3.org/1999/xhtml">
<span style="white-space:pre">	</span><head>
<span style="white-space:pre">	</span><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<span style="white-space:pre">	</span><title>无标题文档</title>
<span style="white-space:pre">	</span><style>
<span style="white-space:pre">	</span>.big {
   <span style="white-space:pre">	</span> background: none repeat scroll 0 0 #EEEEEE;
   <span style="white-space:pre">	</span> height: 100px;
	<span style="white-space:pre">	</span>position:absolute;
	<span style="white-space:pre">	</span>left:0;
	<span style="white-space:pre">	</span>width:200px;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>.big2{
	<span style="white-space:pre">	</span>top:200px;
	}

<span style="white-space:pre">	</span>.big a {
   <span style="white-space:pre">		</span> display: block;
   <span style="white-space:pre">		</span> font-size: 2em;
   <span style="white-space:pre">		</span> padding: 5px 0 0 10px;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span></style>
<span style="white-space:pre">	</span><script src="jquery 2.1.1.js" type="text/javascript"></script>
<span style="white-space:pre">	</span><script src="jquery.easing.1.3.mine.js" type="text/javascript"></script>
<span style="white-space:pre">	</span><script type="text/javascript">
<span style="white-space:pre">	</span>$(document).ready(function() {
	<span style="white-space:pre">	</span>jQuery.easing.def = 'easeOutBounce';
	<span style="white-space:pre">	</span>$('#example').animate({left:"200px"}, {duration: 1000, easing: 'easeInOutCirc'}).animate({left:"0px"}, {duration: 1000, easing: 'e<span style="white-space:pre">	</span>aseOutBounce'});
	<span style="white-space:pre">	</span>$("#toggle2").click(function(e){
		<span style="white-space:pre">	</span>$('#example2').animate(
			<span style="white-space:pre">	</span>{height:200}, 
			<span style="white-space:pre">	</span>{duration: 1000, easing: 'easeInOutCirc'}
		<span style="white-space:pre">	</span>)
		<span style="white-space:pre">	</span>.animate(
			<span style="white-space:pre">	</span>{height:100}, 
			<span style="white-space:pre">	</span>{duration: 1000, easing: 'easeOutBounce'}
		<span style="white-space:pre">	</span>);
	<span style="white-space:pre">	</span>});
<span style="white-space:pre">	</span>});
<span style="white-space:pre">	</span></script>
<span style="white-space:pre">	</span></head>
<span style="white-space:pre">	</span><body>
<span style="white-space:pre">	</span><p id="example" class="big"><a href="#example" id="toggle">onload</a></p>
<span style="white-space:pre">	</span><p id="example2" class="big big2"><a href="#example" id="toggle2">The Clicker</a></p>
<span style="white-space:pre">	</span></body>
<span style="white-space:pre">	</span></html></span><span style="font-family:Microsoft YaHei, Arial, sans-serif;">
</span>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值