jquery的css详解(一)
通过阅读源码可以发现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详解(一)的更多相关文章
- jquery的css详解(二)
jq的工具方法style用于设置样式,jq的实例方法css在设置样式时就是调用的它,接下来分析一下源码. jQuery.extend({ ............................ st ...
- jQuery.hasClass() 函数详解
jQuery.hasClass() 函数详解 hasClass()函数用于指示当前jQuery对象所匹配的元素是否含有指定的css类名. 该函数属于jQuery对象(实例). 语法 JavaScrip ...
- jQuery.attr() 函数详解
一,jQuery.attr() 函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...
- CSS详解
Web前端开发css基础样式总结 颜色和单位的使用 颜色 用颜色的名字表示颜色,比如:red 用16进制表示演示 比如:#FF0000 用rgb数值表示颜色,rgb(红,绿,蓝),每个值都在0-255 ...
- jQuery 事件用法详解
jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...
- DIV+CSS详解
DIV+CSS详解 ✪DIV+CSS"这种叫法其实是一种不准确的叫法 在做笔记的最前面必须先给大家纠正一个错误,就是"DIV+CSS"这种叫法其实是一种不准确的叫法,是国 ...
- jQuery.ready() 函数详解
jQuery.ready() 函数详解 ready()函数用于在当前文档结构载入完毕后立即执行指定的函数. 该函数的作用相当于window.onload事件. 你可以多次调用该函数,从而绑定多个函数, ...
- jquery inArray()函数详解
jquery inarray()函数详解 jquery.inarray(value,array)确定第一个参数在数组中的位置(如果没有找到则返回 -1 ). determine the index o ...
- jQuery extend方法详解
先说个概念的东西: jQuery为开发插件提拱了两个方法,分别是: $.fn.extend(item):为每一个实例添加一个实例方法item.($("#btn1") 会生成一个 j ...
随机推荐
- Java中,关于字符串类型、随机验证码、 时间类型
一.字符串类型:String类型 定义一个字符串 String a="Hello World"; String b= new String ("Hello World&q ...
- div+css背景渐变色代码示例
用CSS使DIV背景颜色渐变,适用于IE和Chrome等浏览器. 从黄到红示例:http://keleyi.com/keleyi/phtml/divcss/2.htm 代码: <style ty ...
- SVG简介
最近遇到SVG这个名词,于是查阅资料,做个笔记. 前言 图片的数字化.将图片存储为数据有两种方案. 位图.也被称为光栅图.即是以自然的光学的眼光将图片看成在平面上密集排布的点的集合.每个点发出的光有独 ...
- 一个有趣的CM
系统 : Windows xp 程序 : Crackme#3 - Self Destructed 程序下载地址 :http://pan.baidu.com/s/1kVxwlaZ 要求 : 注册机编写 ...
- PHP魔术常量
与J2E相比PHP没有九个内置对象,但他有八个魔术变量分别是: '__LINE__' 文件中的当前行号. '__FILE__ 文件的完整路径和文件名. '__DIR__' 文件所在的目录. '__FU ...
- iOS 整理笔记 获取手机信息(UIDevice、NSBundle、NSLocale)
/* iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发ap ...
- 【代码笔记】iOS-图片旋转
代码: RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewControl ...
- SQLite浅析
对于iOS工程师有一道常考的面试题,即iOS数据存储的方式 标答如下: Plist(NSArray\NSDictionary) Preference (偏好设置\NSUserDefaults) NSC ...
- 打开MySQL数据库远程访问的权限
说明:转自,http://www.cnblogs.com/ycsfwhh/archive/2012/08/07/2626597.html 本人亲测方法1有效,方法2待验证 下载GPL版本安装 M ...
- Linux命令学习总结:shutdown
命令简介: 该命令可以安全关闭或者重新启动系统.你没有看错,shutdown命令不仅可以关闭系统.也可以重启Linux系统. 命令语法: /sbin/shutdown [-t sec] [-ark ...