通过阅读源码可以发现css是jq的实例方法。而在内部调用jq的工具方法access来实现的,对该方法不了解的朋友请点击 -> jquery工具方法access详解

在access的回调中做了一个判断,value不等于undefined则调用jq的工具方法style,否则调用jq的工具方法css

可以看出,style是设置,css是获取。也就是说要搞懂jq的实例方法css,必须先要搞懂jq的工具方法style和css

jQuery.fn.extend({
css: function( name, value ) {
return jQuery.access( this, function( elem, name, value ) {
return value !== undefined ?
jQuery.style( elem, name, value ) :
jQuery.css( elem, name );
}, name, value, arguments.length > 1 );
}, ............................... });

jQuery.css有4个参数,前面2个一看就明白,elem是元素,name是样式名称,后面2个是用于把样式值转为数字类型
比如 : $(selector).css('width') //100px
         $(selector).width() //100
其实$(selector).width()就是调用了jQuery.css并传递了后面2个参数,把带px的转成数值类型。这个我们稍后再分析,接着往下看。

jQuery.extend({

    .......................

    css: function( elem, name, numeric, extra ) {
var val, num, hooks,
origName = jQuery.camelCase( name ); // Make sure that we're working with the right name
name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( elem.style, origName ) ); // gets hook for the prefixed version
// followed by the unprefixed version
hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ]; // If a hook was provided get the computed value from there
if ( hooks && "get" in hooks ) {
val = hooks.get( elem, true, extra );
} // Otherwise, if a way to get the computed value exists, use that
if ( val === undefined ) {
val = curCSS( elem, name );
} //convert "normal" to computed value
if ( val === "normal" && name in cssNormalTransform ) {
val = cssNormalTransform[ name ];
} // Return, converting to number if forced or a qualifier was provided and val looks numeric
if ( numeric || extra !== undefined ) {
num = parseFloat( val );
return numeric || jQuery.isNumeric( num ) ? num || 0 : val;
}
return val;
}, ................... });

我们接着往下分析:
origName = jQuery.camelCase( name );    这句代码是把样式名称转驼峰式的写法,比如:background-color转成backgroundColor
我们看一下camelCase源码:

var rmsPrefix = /^-ms-/,

    rdashAlpha = /-([\da-z])/gi,

    fcamelCase = function( all, letter ) {
return ( letter + "" ).toUpperCase();
}; jQuery.extend({ .............................. camelCase: function( string ) {
return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
}, .............................. });

继续往下走。。。。
这句代码是对name的赋值操作,当jQuery.cssProps的属性包含origName则赋值给name,否则调用vendorPropName方法并且动态生成一个jQuery.cssProps的属性且赋值给name
cssProps里面没有则通过vendorPropName生成,以后就直接往cssProps里面取,应该就是这样的思想。
我们看一下cssProps和vendorPropName这俩家伙是什么东西。

name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( elem.style, origName ) );

cssProps默认定义了一个float的属性,对应的值为cssFloat与styleFloat
因为float是js的保留字,所以不能在样式中直接用,就跟class一样的道理,需要改成className。

cssProps: {
// normalize float css property
"float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat"
},

vendorPropName主要是针对css3的样式名称进行处理,我们知道css3样式都是有前缀的,比如moz,ms,webkit等
而我们使用jq是不需要写前缀的,比如:$(selector).css('tranfroms')

好了,大致了解了vendorPropName作用我们分析源码
vendorPropName内部分为3块,下面我们一个个分析。

function vendorPropName( style, name ) {

    // shortcut for names that are not vendor prefixed
if ( name in style ) {
return name;
} // check for vendor prefixed names
var capName = name.charAt(0).toUpperCase() + name.slice(1),
origName = name,
i = cssPrefixes.length; while ( i-- ) {
name = cssPrefixes[ i ] + capName;
if ( name in style ) {
return name;
}
} return origName;
}

首先做一个判断,如果在style中找到了name则直接返回name,说明name已经是有前缀的样式名称了,比如:$(selector).css('MozTranfroms'),否则走下面的代码。

if ( name in style ) {
return name;
}

把name的第一个字母转大写赋值给capName,capName再赋值给origName,i等于cssPrefixes的长度。

var capName = name.charAt(0).toUpperCase() + name.slice(1),
origName = name,
i = cssPrefixes.length;
cssPrefixes = [ "Webkit", "O", "Moz", "ms" ],

遍历i,把带前缀的样式名称赋值给name,再一次name in style

while ( i-- ) {
name = cssPrefixes[ i ] + capName;
if ( name in style ) {
return name;
}
}

分析完了vendorPropName我们继续看css的代码
这里是做了hooks的处理,如透明度、宽度等默认都是空,利用hooks转为数值0

hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];

// If a hook was provided get the computed value from there
if ( hooks && "get" in hooks ) {
val = hooks.get( elem, true, extra );
}

经过了上面的各种处理,调用curCSS获取到样式的值

if ( val === undefined ) {
val = curCSS( elem, name );
}

某些样式默认值是normal,调用cssNormalTransform做了处理

if ( val === "normal" && name in cssNormalTransform ) {
val = cssNormalTransform[ name ];
}
cssNormalTransform = {
letterSpacing: 0,
fontWeight: 400
},

这里就是css后面2个参数起作用的地方,把样式的值转为数值类型

if ( numeric || extra !== undefined ) {
num = parseFloat( val );
return numeric || jQuery.isNumeric( num ) ? num || 0 : val;
}

最终返回val

jquery的css详解(一)的更多相关文章

  1. jquery的css详解(二)

    jq的工具方法style用于设置样式,jq的实例方法css在设置样式时就是调用的它,接下来分析一下源码. jQuery.extend({ ............................ st ...

  2. jQuery.hasClass() 函数详解

    jQuery.hasClass() 函数详解 hasClass()函数用于指示当前jQuery对象所匹配的元素是否含有指定的css类名. 该函数属于jQuery对象(实例). 语法 JavaScrip ...

  3. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  4. CSS详解

    Web前端开发css基础样式总结 颜色和单位的使用 颜色 用颜色的名字表示颜色,比如:red 用16进制表示演示 比如:#FF0000 用rgb数值表示颜色,rgb(红,绿,蓝),每个值都在0-255 ...

  5. jQuery 事件用法详解

    jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...

  6. DIV+CSS详解

    DIV+CSS详解 ✪DIV+CSS"这种叫法其实是一种不准确的叫法 在做笔记的最前面必须先给大家纠正一个错误,就是"DIV+CSS"这种叫法其实是一种不准确的叫法,是国 ...

  7. jQuery.ready() 函数详解

    jQuery.ready() 函数详解 ready()函数用于在当前文档结构载入完毕后立即执行指定的函数. 该函数的作用相当于window.onload事件. 你可以多次调用该函数,从而绑定多个函数, ...

  8. jquery inArray()函数详解

    jquery inarray()函数详解 jquery.inarray(value,array)确定第一个参数在数组中的位置(如果没有找到则返回 -1 ). determine the index o ...

  9. jQuery extend方法详解

    先说个概念的东西: jQuery为开发插件提拱了两个方法,分别是: $.fn.extend(item):为每一个实例添加一个实例方法item.($("#btn1") 会生成一个 j ...

随机推荐

  1. hdu-1179-二分图最大匹配

    Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  2. Java--缓存热点数据,最近最少使用算法

    1.最近最少使用算法LRU (Least recently used,最近最少使用) [实现]:最常见的是使用一个链表保存缓存数据 1.新数据插入到链表头部: 2.每当缓存命中(即缓存数据被访问),将 ...

  3. python征程1.4(初识python)

    1.列表解析. (1)这是一个,让人听起来十分欣喜的术语,代表着你可以通过一个循环将所有值放到一个列表中.python列表解析属于python的迭代中的一种,相比python for循环速度会快很多. ...

  4. Nginx配置文件详解

    Nginx是一款面向性能设计的HTTP服务器,相较于Apache.lighttpd具有占有内存少,稳定性高等优势. ######Nginx配置文件nginx.conf中文详解##### #定义Ngin ...

  5. nodejs 命令行、自定义

    一.必备插件 1. babel:es6语法支持,需要babel-perset-es2015(转换成es5执行).babel.babel-core(程序执行) 2. commander:自定义命令插件, ...

  6. Jquery使用小技巧

    1. 禁止右键使用 view plaincopy to clipboardprint? 1.       $(document).ready(function(){ 2.           $(do ...

  7. SqlServer灾备方案(本地)

    如果你曾经有那么一个不经意的心跳来自于数据库数据损坏:错误的新增.更新.删除 .那么下面的方案一定能抚平你的创伤! 对于一个数据库小白来说,数据库的任何闪失带来的打击可说都是致命的.最初,我们让一个叫 ...

  8. js 模块化的一些理解和es6模块化学习

    模块化 1 IIFE 2 commonjs 3 浏览器中js的模块化 4 简单理解模块加载器的原理  5 es6 之前在参加百度前端技术学院做的小题目的时候,自己写模块的时候 都是写成立即调用表达式( ...

  9. JavaScript区分click事件和mousedown(mouseup、mousemove)方法

    在前端开发工作中,会遇到这样问题:针对同一个dom元素,即希望为它绑定click事件,又想该元素可以允许拖拽的效果.而使用拖拽的效果,我们一般就会用到mousedown,mousemove和mouse ...

  10. iOS BUG: Unbalanced calls to begin/end appearance transitions for <XXXViewController: 0x7fcea3730650>.

    自定义TabBarController Push下一级Controller时 会报这样的错误:Unbalanced calls to begin/end appearance transitions ...