`
ariyue
  • 浏览: 345292 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

jquery源码解读:动画设计

阅读更多
/*
* author:prk date:2008-08-07 comment:analyse the fx of jQuery.
*
*/
jQuery.fn.extend({

// show(speed,[callback])
// 以优雅的动画隐藏所有匹配的元素,并在显示完成后可选地触发一个回调函数。
    // 可以根据指定的速度动态地改变每个匹配元素的高度、宽度和不透明度。
// 显示隐藏的匹配元素 show()
show: function(speed,callback){
return speed ?
this.animate({
height: "show", width: "show", opacity: "show"
}, speed, callback) :

this.filter(":hidden").each(function(){
this.style.display = this.oldblock || "";
if ( jQuery.css(this,"display") == "none" ) {
var elem = jQuery("<" + this.tagName + " />").appendTo("body");
this.style.display = elem.css("display");// 默认的显示的display
// handle an edge condition where css is - div {
// display:none; } or similar
if (this.style.display == "none")// 处理显式地设定了该tag不显示,只好采用b
this.style.display = "block";
elem.remove();// 上面这些的处理有没有必要呢?
}
}).end();// 回到前一个jQuery对象
},

    // 与show相反
hide: function(speed,callback){
return speed ?
this.animate({
height: "hide", width: "hide", opacity: "hide"
}, speed, callback) :

this.filter(":visible").each(function(){
this.oldblock = this.oldblock || jQuery.css(this,"display");
this.style.display = "none";
}).end();
},

// Save the old toggle function
_toggle: jQuery.fn.toggle,
    
// 切换元素的可见状态。
    // 如果元素是可见的,切换为隐藏的;如果元素是隐藏的,切换为可见的。

// 每次点击后依次调用函数。
    // 如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数,
    // 如果有更多函数,则再次触发,直到最后一个。随后的每次点击都重复对这几个函数的轮番调用。
    // 可以使用unbind("click")来删除。
toggle: function( fn, fn2 ){
return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
this._toggle.apply( this, arguments ) :// 原来的toggle
(fn ?
   this.animate({height: "toggle", width: "toggle", opacity: "toggle"}, fn, fn2)
:  this.each(function(){jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();}
// 对每个元素都调用show,或hide函数。
)
);
},

// 把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,并在动画完成后可选地触发一个回调函数。
     // 这个动画只调整元素的不透明度,也就是说所有匹配的元素的高度和宽度不会发生变化。
fadeTo: function(speed,to,callback){
return this.animate({opacity: to}, speed, callback);
},

/**
* 用于创建自定义动画的函数。
* 这个函数的关键在于指定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如“height”、“top”或“opacity”)。
* 注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left.
* 而每个属性的值表示这个样式属性到多少时动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。
* 在 jQuery 1.2 中,你可以使用 em 和 % 单位。另外,在 jQuery 1.2 中,你可以通过在属性值前面指定 "+=" 或
* "-=" 来让元素做相对运动。
*
* params (Options) : 一组包含作为动画属性和终值的样式属性和及其值的集合 。 duration (String,Number)
*可选) 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)
* easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".
* callback (Function) : (可选) 在动画完成时执行的函数
*/
animate: function( prop, speed, easing, callback ) {
var optall = jQuery.speed(speed, easing, callback);

return this[ optall.queue === false ? "each" : "queue" ](function(){// 执行each或queue方法
var opt = jQuery.extend({}, optall), p,
hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),// 是元素节点且是隐藏的
self = this;// 当前的元素

for ( p in prop ) {
if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )// 已经是完成的状态
return opt.complete.call(this);// 调用complate函数,在用户的callback加上队列的处理

if ( ( p == "height" || p == "width" ) && this.style ) {// style中高度,宽度
opt.display = jQuery.css(this, "display");// 保存当前元素的display
opt.overflow = this.style.overflow;// 保证没有暗中进行的
}
}
if ( opt.overflow != null )// 超出部分不见
this.style.overflow = "hidden";

opt.curAnim = jQuery.extend({}, prop);
  
jQuery.each( prop, function(name, val){// 对当前元素的给定的属性进行变化的操作
var e = new jQuery.fx( self, opt, name );

if ( /toggle|show|hide/.test(val) )// 传参的属性可以用toggle,show,hide,其它
// 调用当前e.show,e.hide,e.val的方法,jQuery.fx.prototype
e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
else {// 支持"+=" 或 "-=" 来让元素做相对运动。
var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
start = e.cur(true) || 0;// 当前的大小或0

if ( parts ) {
var end = parseFloat(parts[2]),// 值
unit = parts[3] || "px";// 单位

if ( unit != "px" ) {// 计算开始的值=(end/cur)*start
self.style[ name ] = (end || 1) + unit;
start = ((end || 1) / e.cur(true)) * start;
self.style[ name ] = start + unit;
}

if ( parts[1] )// +=/-=,做相对运行
end = ((parts[1] == "-=" ? -1 : 1) * end) + start;

e.custom( start, end, unit );// 动画
} else
e.custom( start, val, "" );// 动画
}
});
// For JS strict compliance
return true;
});
},

// 实现队列操作,为jQuery对象中的每个元素都加type的属性,值为fn.
queue: function(type, fn){
// 可能看出支持一个fn的参数的形式,支持fn的array集形式
if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) {
fn = type;
type = "fx";
}
// type不存在,空字符等,无参数,type:string,fn不存在。肯定不是函数,也不是数组
if ( !type || (typeof type == "string" && !fn) )
return queue( this[0], type );

return this.each(function(){
if ( fn.constructor == Array )// 数组的形式
queue(this, type, fn);// 存储在元素的type属性中
else {
queue(this, type).push( fn );// 加上一个

if ( queue(this, type).length == 1 )
fn.call(this);
}
});
},

stop: function(clearQueue, gotoEnd){
var timers = jQuery.timers;

if (clearQueue)
this.queue([]);// 清除

this.each(function(){
// go in reverse order so anything added to the queue during the
// loop is ignored
for ( var i = timers.length - 1; i >= 0; i-- )
if ( timers[i].elem == this ) {
if (gotoEnd)
// force the next step to be the last
timers[i](true);
timers.splice(i, 1);
}
});

// start the next in the queue if the last step wasn't forced
if (!gotoEnd)
this.dequeue();

return this;
}

});

// Generate shortcuts for custom animations
jQuery.each({
slideDown: { height:"show" },
slideUp: { height: "hide" },
slideToggle: { height: "toggle" },
fadeIn: { opacity: "show" },
fadeOut: { opacity: "hide" }
}, function( name, props ){
jQuery.fn[ name ] = function( speed, callback ){
return this.animate( props, speed, callback );
};
});

// 为元素加上type的array的属性,或返回取到elem上type的值
var queue = function( elem, type, array ) {
if ( elem ){
type = type || "fx";
var q = jQuery.data( elem, type + "queue" );
if ( !q || array )
q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );
}
return q;
};
// 出列,根据type
jQuery.fn.dequeue = function(type){
type = type || "fx";
return this.each(function(){
var q = queue(this, type);// 得取type的的值
q.shift();// 移出一个
if ( q.length )
q[0].call( this );
});
};

jQuery.extend({
     // 主要用于辅助性的工作
speed: function(speed, easing, fn) {
var opt = speed && speed.constructor == Object ? speed : {// 采用紧凑型方式
complete: fn || !fn && easing ||
jQuery.isFunction( speed ) && speed,// coplete是至多三个参数的最后一个,当然是Fn.
duration: speed,// 持继的时间
easing: fn && easing || easing && easing.constructor != Function && easing// 不是Fn
};

opt.duration = (opt.duration && (opt.duration.constructor == Number ?
opt.duration : jQuery.fx.speeds[opt.duration])) // 存在,不是数值,转找
|| jQuery.fx.speeds._default;// 默认的

// Queueing
opt.old = opt.complete;
opt.complete = function(){// 排队的处理
if ( opt.queue !== false )
jQuery(this).dequeue();
if ( jQuery.isFunction( opt.old ) )
opt.old.call( this );
};

return opt;
},

// 擦除效果
easing: {
linear: function( p, n, firstNum, diff ) {
return firstNum + diff * p;
},
swing: function( p, n, firstNum, diff ) {
return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
}
},

timers: [],// jQuery.timers
timerId: null,
   
// 根据参数构成一个对象
fx: function( elem, options, prop ){
this.options = options;
this.elem = elem;
this.prop = prop;

if ( !options.orig )
options.orig = {};
}

});

jQuery.fx.prototype = {

// 为元素设值,更新显示
update: function(){
if ( this.options.step )
this.options.step.call( this.elem, this.now, this );

(jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );// 为元素的设值,fx.prop

// 对于高度和宽度,采用display=block的形式。
if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )
this.elem.style.display = "block";
},

// 当前元素当前属性的值
cur: function(force){
if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )
return this.elem[ this.prop ];

var r = parseFloat(jQuery.css(this.elem, this.prop, force));
return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
},

// 开动一个动画
custom: function(from, to, unit){
this.startTime = now();
this.start = from;
this.end = to;
this.unit = unit || this.unit || "px";
this.now = this.start;
this.pos = this.state = 0;
this.update();

var self = this;
function t(gotoEnd){
return self.step(gotoEnd);// 调用step(gotoEnd)//本对象的
}
t.elem = this.elem;
jQuery.timers.push(t);
        
// 第一次,或上一次已经把jQuery.timers全部执行完了。
if ( jQuery.timerId == null ) {
jQuery.timerId = setInterval(function(){
var timers = jQuery.timers;
for ( var i = 0; i < timers.length; i++ )// 执行timers中所有
if ( !timers[i]() )// 执行了一次,就从timers中除去
timers.splice(i--, 1);

if ( !timers.length ) {// length==0,取消Interval
clearInterval( jQuery.timerId );
jQuery.timerId = null;
}
}, 13);
}
},

// Simple 'show' function
show: function(){
// 保存当前的,以被修改之后能得到初始的值
this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
this.options.show = true;

this.custom(0, this.cur());

// Make sure that we start at a small width/height to avoid any
// flash of content
if ( this.prop == "width" || this.prop == "height" )
this.elem.style[this.prop] = "1px";

// 开始显示元素
jQuery(this.elem).show();
},

// 隐藏
hide: function(){
// 保存当前的,以被修改之后能得到初始的值
this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
this.options.hide = true;

this.custom(this.cur(), 0);
},

// 动画的每一个步骤
step: function(gotoEnd){
var t = now();
         // 结束或当前时间大于startTime+duration
if ( gotoEnd || t > this.options.duration + this.startTime ) {
this.now = this.end;
this.pos = this.state = 1;
this.update();

this.options.curAnim[ this.prop ] = true;

var done = true;
for ( var i in this.options.curAnim )
if ( this.options.curAnim[i] !== true )
done = false;

if ( done ) {// 重置所有的设定
if ( this.options.display != null ) {// Reset the overflow
this.elem.style.overflow = this.options.overflow;
// Reset the display
this.elem.style.display = this.options.display;
if ( jQuery.css(this.elem, "display") == "none" )
this.elem.style.display = "block";
}

// Hide the element if the "hide" operation was done
if ( this.options.hide )
this.elem.style.display = "none";

// Reset the properties, if the item has been hidden or shown
if ( this.options.hide || this.options.show )
for ( var p in this.options.curAnim )
jQuery.attr(this.elem.style, p, this.options.orig[p]);
}

if ( done )// 运行complete的回调函数
this.options.complete.call( this.elem );

return false;
} else {
var n = t - this.startTime;
this.state = n / this.options.duration;

// Perform the easing function, defaults to swing
this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
this.now = this.start + ((this.end - this.start) * this.pos);

// Perform the next step of the animation
this.update();
}

return true;
}

};

jQuery.extend( jQuery.fx, {
// 动画的速度
speeds:{
slow: 600,
fast: 200,
// Default speed
_default: 400
},

// 为元素设值
step: {
opacity: function(fx){// 为元素CSS设opacity为fx
jQuery.attr(fx.elem.style, "opacity", fx.now);
},

_default: function(fx){
if( fx.prop in fx.elem ) // 对于元素
fx.elem[ fx.prop ] = fx.now;
else if( fx.elem.style )// 对于style
fx.elem.style[ fx.prop ] = fx.now + fx.unit;
}
}
});
分享到:
评论

相关推荐

    jQuery源码解读

    总结来说,jQuery源码解读涉及了JavaScript的闭包、对象构造、原型链、方法扩展、浏览器兼容性和自定义扩展等多个核心知识点。理解这些原理有助于我们更深入地使用和定制jQuery,提升JavaScript编程效率。

    jquery源码框架解读

    7. **源码结构分析**:jQuery源码采用模块化设计,分为核心、选择器、遍历、DOM操作等多个部分。理解其模块划分和依赖关系,有助于我们更好地理解代码结构和逻辑。 8. **学习资源**:除了这份源码解读,还有其他...

    jQuery-, jQuery源码解读 -- jQuery v1.10.2.zip

    《jQuery源码解读——深入理解jQuery v1.10.2》 jQuery,这个轻量级的JavaScript库,自2006年发布以来,以其简洁的API和强大的功能深受开发者喜爱,成为Web开发领域中不可或缺的一部分。本文将对jQuery v1.10.2的...

    jquery 源码分析

    《jQuery源码分析详解》 jQuery,作为一款广泛使用的JavaScript库,极大地简化了网页的DOM操作、事件处理、动画制作以及Ajax交互。深入理解jQuery的源码,不仅可以提升我们的编程技巧,更能帮助我们优化代码,提高...

    Jquery 源码解读

    ### Jquery 源码解读 #### 一、概述 jQuery 是一款优秀的 JavaScript 库,以其简洁、高效著称,极大地简化了 HTML 文档遍历、事件处理、动画以及 Ajax 交互等操作。本文旨在深入解析 jQuery 的核心部分,帮助读者...

    jQuery技术内幕 深入解析jQuery架构设计与实现原理

    《jquery技术内幕:深入解析jquery架构设计与实现原理》由阿里巴巴资深前端开发工程师撰写,从源代码角度全面而系统地解读了jquery的17个模块的架构设计理念和内部实现原理,旨在帮助读者参透jquery中的实现技巧和...

    jQuery源码分析系列

    jQuery源码分析系列涉及了对jQuery库内部实现的详细解读,jQuery作为前端开发中最常用的JavaScript库之一,它简化了DOM操作、事件处理、动画效果和AJAX交互等操作。通过深入分析jQuery的源码,开发者可以学习到先进...

    jQuery:jQuery 源码解读 -- jQuery v1.10.2

    以下是对jQuery源码的深度解读。 ### 1. 兼容性处理 jQuery从诞生之初就致力于解决JavaScript在不同浏览器之间的兼容问题。在v1.10.2中,jQuery通过`jQuery.fn.init`初始化函数处理了各种浏览器差异,比如`...

    锋利的jquery源码

    《锋利的jQuery源码》是一本深入剖析jQuery库源码的专业书籍,旨在帮助开发者理解jQuery的核心机制,提升JavaScript编程技巧。这本书通过系统的章节划分,详细解读了jQuery库的各个重要部分,使得读者能够逐步掌握这...

    jQuery核心源码中文注释解读

    以下是对jQuery核心源码的中文注释解读: 首先,jQuery的源码通常以一个立即执行的匿名函数开始,这样做是为了避免命名冲突。JavaScript中没有严格的命名空间,因此将所有代码包裹在一个匿名函数内,可以限制其作用...

    jquery源码 详细中文注释

    本文将对一份包含详细中文注释的jQuery源码进行解读,帮助读者更好地理解其内部实现机制。 #### 二、源码注释的重要性 对于开发者而言,阅读和理解高质量的源码是非常重要的。一方面,这有助于我们深入学习相关技术...

    jquery源码_详细中文注释.pdf

    ### 关于《jquery源码_详细中文注释.pdf》的知识点解析 #### 一、文档简介及背景 《jquery源码_详细中文注释.pdf》是一份由Auscar lin(林辉华)编写的关于jQuery源码的中文注释文档。此文档的主要目的是帮助中文...

    jquery源码_详细中文注释.pdf.zip_jquery_js pdf 批注_pdf注释

    这份源码解读将帮助我们理解jQuery如何实现这一目标。 首先,我们关注jQuery的构造函数`jQuery()`,它是整个库的入口点。在这里,它处理了各种输入类型,如字符串(选择器)、元素、HTML片段等,并创建了jQuery对象...

    jQuery源码解读之extend()与工具方法、实例方法详解

    本文实例讲述了jQuery源码解读之extend()与工具方法、实例方法。分享给大家供大家参考,具体如下: 使用jQuery的时候会发现,jQuery中有的函数是这样使用的: $.get(); $.post(); $.getJSON(); 有些函数是这样使用...

    JQuery王兴魁源码

    《JQuery王兴魁源码》是一份关于jQuery库的深度学习资料,它包含了著名前端开发者王兴魁对于jQuery核心代码的解读与分析。jQuery是一个高效、简洁且易用的JavaScript库,它极大地简化了HTML文档遍历、事件处理、动画...

    jQuery源码解读之hasClass()方法分析

    文章末尾提到,希望本文的内容能对大家在使用jQuery进行程序设计时有所帮助。这表明了解析源码的重要性,它不仅可以帮助我们深入理解库的内部工作原理,还可以在遇到问题时快速定位和解决。 最后,文章中也提到,...

    网页模板——jquery带缩略图和多种过渡动画效果的轮播图特效源码.zip

    本资源“jquery带缩略图和多种过渡动画效果的轮播图特效源码.zip”提供了一种使用jQuery实现的轮播图解决方案,具有缩略图导航和丰富的过渡动画效果。以下是对这一技术的详细解读: 1. **jQuery**:jQuery 是一个...

    jQuery源码解读之addClass()方法分析

    jQuery是广泛使用的JavaScript库,它简化了HTML文档遍历、事件处理、动画和Ajax交互。addClass()是jQuery提供的一个方法,用于...对jQuery源码的深入研究有助于我们更好地掌握其工作机制,并在此基础上进行扩展和优化。

    Jquery数据绑定分页源码

    在压缩包中,"JqueryDatabind_87745874-3a30-4614-a095-891ce974a341"可能是一个包含源码的文件,而"源码必读.pdf"可能是对源码的解读或使用指南。通过阅读这两个文件,你可以更深入地理解如何在jQuery项目中实现...

    锋利的JQuery第二版的源码

    《锋利的jQuery第二版》是一本深受开发者喜爱的jQuery技术书籍,它的源码提供了深入理解jQuery库实际运作的宝贵资源。...对于那些想要成为前端开发专家的人来说,理解和分析jQuery源码是必不可少的步骤。

Global site tag (gtag.js) - Google Analytics