依旧是来自慕课网jQuery源码解析,本身的整理。目前看来全部的文章其实仍是有点乱,后面完了我会好好再整理整理/(ㄒoㄒ)/~~。css
给一个HTML元素设置css属性,如:html
var head= document.getElementById("head"); head.style.width = "20px"; head.style.height = "10px"; head.style.display = "block";
存在的问题:css3
1.每次只能设置一个,并且每设置一次浏览器要绘制一次(高级的浏览器可能会合并style的次数)
2.style接口只能针对 行类样式,link引入的样式没法获取。
3.样式属性名的兼容问题,好比驼峰,保留字float。Tip:style的内部属性命名采用的驼峰形式,好比 background-image(backgroundImage),比较特殊的就是float,由于是保留字就换成了cssFloat,IE:styleFloat。对于width、hight这些处理都最好要有一个量度单位。web
解决方法:
1.合并cssText。浏览器
var head= document.getElementById("head"); head.style.cssText="width:20px;height:10px;display:bolck";
cssText很快捷且全部浏览器都支持。批量操做样式时,cssText只需一次 reflow,提升了页面渲染性能。dom
2.利用文档碎片。
缺点是样式被总体覆盖,处理的时候要先获取须要保留的样式再拼接。函数
参考文章:https://www.w3cmm.com/dom/ins...性能
//使用CSSRules去得到样式(非内联的也能够得到) //添加新的CSS function addCSSRule(key, value) { //获取最后一个样式表 var css = document.styleSheets[document.styleSheets.length - 1]; //若是浏览器支持InsertRule方法则使用这个插入,不然是IE,则使用addRule css.cssRules ? (css.insertRule(key + "{" + value + "}", css.cssRules.length)) : (css.addRule(key, value)); } //移除CSS function removeCSSRule(key) { for (var i = 0; i < document.styleSheets.length; i++) { var css = document.styleSheets[i]; css.cssRules ? (function() { for (var j = 0; j < css.cssRules.length; j++) { if (css.cssRules[j].selectorText == key) { css.deleteRule(j); } } })() : (css.removeRule(key)); } } //调用示例 addCSSRule("#a","color:red;background:yellow") removeCSSRule("#a")
$.cssHooks 对象提供了一种方法经过定义函数来获取和设置特定的CSS值的方法
jQuery提供一个 API 来调用用户自定义的函数,用于扩展,以便获取和设置特定属性值。在 .attr,.prop(),.val()和 .css()的操做中都会引入钩子,钩子都有类似的结构。
var someHook = { get: function(elem) { // obtain and return a value return "something"; }, set: function(elem, value) { // do something with value } }
钩子用来作啥?
作css3浏览器兼容的时候,须要特定的前缀,好比:this
Webkit 内核浏览器:-webkit-border-radius Firefox 内核浏览器:-moz-border-radius
你能够只传一个border-radius,而后CSS hook能够自动帮你把这些前缀都加上。firefox
咱们平作浏览器兼容,通常都是:
if(webkit){ ........................ }else if(firefox){ ........................... }else if(...)
若是咱们换成hook的话:
$.cssHooks.borderRadius = { get: function( elem, computed, extra ) { return $.css( elem, borderRadius ); }, set: function( elem, value) { elem.style[ borderRadius ] = value; } };
jQuery操做样式的接口jQuery.fn.css
与jQuery.css
jQuery.style( elem, name, value ) //设置值 jQuery.css( elem, name ) //获取值
如下主要集中讲解关于获取值的过程:
origName = jQuery.camelCase( name );
name = jQuery.cssProps[origName] || (jQuery.cssProps[origName] = vendorPropName(elem.style, origName));
每个特殊的状况都有对应的处理,好比width:
cssHooks = { width:{ get:function(){}, set:function(){} } }
用户在获取width的时候,可能会有如下几种状况:
//获取最终属性 var getStyles = function(elem) { return elem.ownerDocument.defaultView.getComputedStyle(elem, null); }; //返回一个css属性映射到一个浏览器商前缀的属性 function vendorPropName(style, name) { // 不用处理前缀的属性直接返回 if (name in style) { return name; } //检查前缀,capName把第一个字母大写 var capName = name[0].toUpperCase() + name.slice(1), origName = name, i = cssPrefixes.length; //加上CSS前缀 while (i--) { name = cssPrefixes[i] + capName; if (name in style) { return name; } } return origName; } //获取当前样式 function curCSS(elem, name, computed) { var width, minWidth, maxWidth, ret, style = elem.style; computed = computed || getStyles(elem); if (computed) { ret = computed.getPropertyValue(name) || computed[name]; } return ret; } //jQuery.css获取css值 function css(elem, name, extra, styles) { var val, num, hooks, //转成驼峰写法(如:background-color -> backgroundColor) origName = jQuery.camelCase(name); //若是是特殊属性float转成cssFloat,以及带有浏览器前缀处理 name = jQuery.cssProps[origName] || (jQuery.cssProps[origName] = vendorPropName(elem.style, origName)); //处理获取width、height等尺寸属性的状况 hooks = jQuery.cssHooks[name] || jQuery.cssHooks[origName]; if (hooks && "get" in hooks) { val = hooks.get(elem, true, extra); } //经过computed接口直接获取样式 if (val === undefined) { val = curCSS(elem, name, styles); } return val; }
另外在jQuery原型上的css方法,也就是咱们直接调用的那个css()是这样的:
//name:设置的属性名,value:设置的属性值 css: function( name, value ) { return access( this, function( elem, name, value ) { var styles, len, map = {}, i = 0; if ( jQuery.isArray( name ) ) { styles = getStyles( elem ); len = name.length; for ( ; i < len; i++ ) { map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles ); } return map; } //若是value是undefined说明是获取值,不然是设置值 return value !== undefined ? jQuery.style( elem, name, value ) : jQuery.css( elem, name ); }, name, value, arguments.length > 1 ); }