In this article I will talk about the jQuery.cssHooks object, which by default is contained in jQuery. The extension of this object allows you to add new properties or values ​​that are written in the .css () method that are not initially supported by any particular browser. Perhaps for many, the presence of CSS hooks is not news, but for me it was a small discovery.$.cssHooks.chuckNorris = { get: function(elem) { // elem, value return value; }, set: function(elem, value) { // elem, value } } $(el).css('chuck-norris', Infinity); // $(el).css({'chuck-norris': Infinity}); alert($(el).css('chuck-norris')); //Infinity $('div').width('100%'); 

var Color = function(el){ // : // IE8-, , var ieDetect = (function(){ var ua = navigator.userAgent; var result = {} result.isOldIe = ~ua.indexOf('MSIE 6') || ~ua.indexOf('MSIE 7') || ~ua.indexOf('MSIE 8'); result.hasGradient = result.isOldIe && ~el.style.filter.toLowerCase().indexOf('gradient'); result.isGradientEnabled = result.hasGradient && !!el.filters.item("DXImageTransform.Microsoft.gradient").enabled; return result; })(); // var colorToHex = function (objColor) { var hex = {}; for (var i in objColor) { hex[i] = objColor[i]; if (i==='a') { hex.a = Math.round(hex.a*255); } hex[i] = hex[i].toString(16); // '0', hex[i] = hex[i].length == 2 ? hex[i] : '0' + hex[i]; } return hex; } // {r,g,b,a} var colorToString = function(objColor, format) { var hexColor = colorToHex(objColor); switch(format) { case 'rgb': return 'rgb(' + objColor.r + ',' + objColor.g + ',' + objColor.b + ')'; case 'rgba': return 'rgba(' + objColor.r + ',' + objColor.g + ',' + objColor.b +',' + objColor.a + ')'; case '#6': return '#' + hexColor.r + hexColor.g + hexColor.b; case '#8': return '#' + hexColor.a + hexColor.r + hexColor.g + hexColor.b; } } // var convertColor = function(color, format) { var colorObj = parseColor(color); return colorToString(colorObj, format) } // getComputedStyle // , if (!window.getComputedStyle) { window.getComputedStyle = function(el, pseudo) { this.el = el; this.getPropertyValue = function(prop) { var re = /(\-([az]){1})/g; if (re.test(prop)) { prop = prop.replace(re, function () { return arguments[2].toUpperCase(); }); } return el.currentStyle[prop] ? el.currentStyle[prop] : null; } return this; } } ... this.setBackgroundColor = function(color) { var newColor, newColorObj = parseColor(color); if(ieDetect.isOldIe) { // IE if(newColorObj.a < 1) { // el.style.backgroundColor = 'transparent'; newColor = colorToString(newColorObj, '#8'); if(ieDetect.hasGradient) { // el.filters.item("DXImageTransform.Microsoft.gradient").enabled = true; el.filters.item("DXImageTransform.Microsoft.gradient").startColorstr = newColor; el.filters.item("DXImageTransform.Microsoft.gradient").endColorstr = newColor; } else { // el.style.filter += "progid:DXImageTransform.Microsoft.gradient(enabled='true', startColorstr=" + newColor + ", endColorstr=" + newColor + ")"; } } else { // newColor = colorToString(newColorObj, '#6'); if(ieDetect.hasGradient) { // el.filters.item("DXImageTransform.Microsoft.gradient").enabled = false; el.style.backgroundColor = newColor; } else { // el.style.backgroundColor = newColor; } } } else { // IE if(newColorObj.a < 1) { // newColor = colorToString(newColorObj, 'rgba'); el.style.backgroundColor = newColor; } else { // newColor = colorToString(newColorObj, '#6'); el.style.backgroundColor = newColor; } } } this.getBackgroundColor = function() { var color; if(ieDetect.isGradientEnabled) { // IE color = el.filters.item("DXImageTransform.Microsoft.gradient").startColorstr; return convertColor(color, 'rgba'); } else { // : http://snipplr.com/view/13523/getcomputedstyle-for-ie/ // $(el).css('background-color'); // , color = el.style.BackgroundColor || window.getComputedStyle(el,null).getPropertyValue('background-color'); return color; } } $.cssHooks.backgroundColor = { get: function(elem) { var color = new Color(elem); return color.getBackgroundColor(); }, set: function( elem, value ) { var color = new Color(elem); color.setBackgroundColor(value); } } $.cssHooks.backgroundAlpha = { get: function(elem) { var color = new Color(elem); var colorStr = color.getBackgroundColor(); var colorObj = parseColor(colorStr); return colorObj.a; }, set: function(elem, value) { var color = new Color(elem); var colorStr = color.getBackgroundColor(); var colorObj = parseColor(colorStr); // , jQuery 'px'; colorObj.a = String(value).replace('px', ''); color.setBackgroundColor('rgba('+ colorObj.r+','+colorObj.g+','+colorObj.b+','+colorObj.a+')'); } } p{background-color: #991111; ...} div{background: url(...) ...} <div> <p>...</p> <p>...</p> </div> $('p').width('100%'); $('p:eq(0)').css({'background-color':'rgba(0,111,221,0.9)'}); $('p:eq(1)').css('background-alpha', 0.5); Source: https://habr.com/ru/post/123241/
All Articles