关于自定义animate方法错误问题

一天要饭生活又开始啦 2013-04-25 03:50:34
代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type='text/css'>
#div1 { background:#aaa; width:188px; height:188px; position:absolute; top:10px; left:110px; }
#div2 { background:#aaa; width:188px; height:188px; position:absolute; top:310px; left:110px; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easing-1.3.pack.js"></script>
<script type="text/javascript">

</script>
</head>

<body>
<div id='div1'>我是一个div</div>
<div id='div2'>我是另一个div</div>
<button onclick="stop()">停止</button>
<button onclick="cont()">继续后面的动画</button>
<script type="text/javascript">
var timers = [];
var timerId;

$.fn.extend({
myAnimate: function(property, duration, easing, callback) {
var operate = jQuery.getOpt(duration, easing, callback);
$(this).queue(function() {
var elem = this;
$.each(property, function(name, value){
var fx = new FX(elem, operate, name);
var start = parseInt($(elem).css(name));
var end = value;
fx.custom(start, end);
})
})
return this;
}
})

function FX(elem, options, name){
this.elem = elem;
this.options = options;
this.name = name;
}

FX.prototype.custom = function(from, to){
this.startTime = jQuery.now();
this.start = from;
this.end = to;
timers.push(this);
FX.tick();
}

FX.prototype.step = function(){
var t = jQuery.now();
var nowPos;
if(t > this.startTime + this.options.duration){
nowPos = this.end;
this.options.callback.call(this.elem);
this.stop();
} else {
var n = t - this.startTime;
var state = n / this.options.duration;
var pos = jQuery.easing[this.options.easing](state, n,0,1,this.options.duration);
nowPos = this.start + ((this.end - this.start)*pos);
}
this.update(nowPos, this.name);
}

FX.prototype.stop = function(){
for(var i=timers.length - 1; i >=0; i--){
if(timers[i] === this){
timers.splice(i, 1);
}
}
}

FX.prototype.update = function(value, name){
this.elem.style[name] = value;
}

FX.tick = function(){
if(timerId) return;
var self = this;
timerId = setInterval(function(){
for(var i=0, c; c=timers[i++];){
c.step();
}
if(!timers.length){
FX.stop();
}
}, 13);
}

FX.stop = function(){
clearInterval(timerId);
timerId = null;
}

jQuery.getOpt = function(duration, easing, callback) {
var obj = {
'duration': duration,
'easing': easing
}
obj.callback = function() {
callback && callback();
$(this).dequeue();
}
return obj;
}

$.fn.stop = function(){
for (var i = timers.length - 1; i>=0; i--){
if(timers[i].elem === this[0]) {
timers[i].stop();
}
}
}

$('#div1').myAnimate({'top': 500}, 'swing').myAnimate({'top': 100}, 'swing').myAnimate({'left': 500}, 'swing').myAnimate({'top': 500}, 500, 'swing');
$('#div2').myAnimate({'left': 1000}, 1000, 'swing');
function stop() {
$('#div1').stop();
}

function cont() {
$('#div1').dequeue();
}

</script>
</body>
</html>
代码来源http://wenku.baidu.com/view/215b9af79e31433239689329.html
提示错误:TypeError: jQuery.easing[this.options.easing] is not a function
66行,请问是什么原因啊?
...全文
327 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 9 楼 showbo 的回复:
$('#div1').myAnimate({'top': 500}, 'swing').myAnimate({'top': 100}, 'swing').myAnimate({'left': 500}, 'swing').myAnimate({'top': 500}, 500, 'swing'); 其他代码不知道,你前面3个调用myAnimate没有少了duration参数,导致传递easing参数丢失,那么就是undefined,jQuery.easing当然没有undefined这种效果,报错了
大神能解释下特别是解释下jquery是如何实现queue的,实现的原理是什么
  • 打赏
  • 举报
回复
哪位大神能解释下啊,特别是解释下jquery是如何实现queue的,实现的原理是什么
Go 旅城通票 2013-05-08
  • 打赏
  • 举报
回复
$('#div1').myAnimate({'top': 500}, 'swing').myAnimate({'top': 100}, 'swing').myAnimate({'left': 500}, 'swing').myAnimate({'top': 500}, 500, 'swing'); 其他代码不知道,你前面3个调用myAnimate没有少了duration参数,导致传递easing参数丢失,那么就是undefined,jQuery.easing当然没有undefined这种效果,报错了
  • 打赏
  • 举报
回复
ding.....................
  • 打赏
  • 举报
回复
顶起起起起起起
  • 打赏
  • 举报
回复
引用 1 楼 zsx841021 的回复:
在本地运行需要jquery-easing-1.3.pack.js
我有这个插件
三石-gary 2013-04-25
  • 打赏
  • 举报
回复
在本地运行需要jquery-easing-1.3.pack.js

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧